Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rails 3.27 KB | None | 0 0
  1. class ComponentsController < ApplicationController
  2.   skip_before_filter :verify_authenticity_token
  3.   before_action :check_admin
  4.  
  5.   layout "ka_application"
  6.  
  7.   def index
  8.   end
  9.  
  10.   def create
  11.     Component.create(name: params[:name])
  12.     redirect_to :back
  13.   end
  14.  
  15.   def edit
  16.     @component = Component.find(params[:id])
  17.   end
  18.  
  19.   def update
  20.     component = Component.find(params[:id])
  21.     component.update(name: params[:name])
  22.     redirect_to components_path
  23.   end
  24.  
  25.   def destroy
  26.  
  27.     if !TopicComponent.where(component_id: params[:id]).empty?
  28.     @top = TopicComponent.where(component_id: params[:id])
  29.     @top.find_each do |t|
  30.         t.destroy
  31.     end
  32.     end
  33.  
  34.     #TopicComponent.where("component_id = ?",params[:id]).destroy
  35.     Component.find(params[:id]).destroy
  36.     redirect_to :back
  37.   end
  38.  
  39.   def attach
  40.     mas = []
  41.     $out = []
  42.     mas.push(params[:topic_id].to_i)
  43.     limit = params[:rad].to_i
  44.     f = params[:flag].to_i
  45.     count = 0
  46.     if (f == 0)
  47.         while count < limit
  48.         for i in 0..mas.length - 1
  49.             KaTopic.where("id = ? OR parent_id = ?", mas[i], mas[i]).find_each do |top|
  50.                 if !TopicRelation.where(ka_topic_id: top.id, related_topic_id: top.parent_id, rel_type: params[:rel]).empty? || !TopicRelation.where(ka_topic_id: top.parent_id, related_topic_id: top.id, rel_type: params[:rel]).empty?
  51.                     mas.push(top.id)
  52.                     mas.push(top.parent_id)
  53.                     mas.uniq
  54.                 end
  55.                 end
  56.         end
  57.         count = count + 1
  58.         end
  59.        
  60.         mas = mas.uniq
  61.         for i in 0..mas.length
  62.             if TopicComponent.where(ka_topic_id: mas[i], component_id: params[:component_id]).empty? && !params[:component_id].nil?
  63.               TopicComponent.create(ka_topic_id: mas[i], component_id: params[:component_id])
  64.             end
  65.         end
  66.     else
  67.         while count < limit
  68.         for i in 0..mas.length - 1
  69.             TopicRelation.where("(ka_topic_id = ? OR related_topic_id = ?) AND rel_type = ?", mas[i], mas[i], params[:rel]).find_each do |top|
  70.                 mas.push(top.ka_topic_id)
  71.                 mas.push(top.related_topic_id)
  72.                 mas.uniq
  73.                 end
  74.         end
  75.         count = count + 1
  76.         end
  77.        
  78.         mas = mas.uniq
  79.         for i in 0..mas.length
  80.             if TopicComponent.where(ka_topic_id: mas[i], component_id: params[:component_id]).empty? && !params[:component_id].nil?
  81.               TopicComponent.create(ka_topic_id: mas[i], component_id: params[:component_id])
  82.             end
  83.         end
  84.     end
  85.  
  86.     for i in 0..mas.length - 1
  87.     topic = KaTopic.find(mas[i])
  88.     component = Component.find(params[:component_id])
  89.     $out.push({topic: topic.text, related_component: component.name, t_id: topic.id, c_id: component.id})
  90.     end
  91.     @r_id = params[:topic_id].to_i
  92.     render :show_all_relations
  93.  #   redirect_to :back
  94.   end
  95.  
  96.   def  show_all_relations
  97.    
  98.   end
  99.  
  100.   def detach_list
  101.     #Внимание: используется delete_all (т.к. у модели нет первичных ключей)
  102.     TopicComponent.delete_all(ka_topic_id: params[:t_id], component_id: params[:c_id])
  103.     $out.delete({t_id: :t_id, c_id: :c_id})
  104.     render :show_all_relations
  105.   end
  106.  
  107.   def detach
  108.     #Внимание: используется delete_all (т.к. у модели нет первичных ключей)
  109.     TopicComponent.delete_all(ka_topic_id: params[:t_id], component_id: params[:c_id])
  110.     redirect_to :back
  111.   end
  112. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement