Module: MongoidSortableTreeController::CRUD
- Includes:
- DefineVariablesMethod
- Defined in:
- app/controllers/mongoid_sortable_tree_controller.rb
Instance Method Summary collapse
- #check ⇒ Object
- #copy_node(node, node_parent, info) ⇒ Object
- #create_node(node, node_parent, info) ⇒ Object
- #delete_node(node, node_parent, info) ⇒ Object
- #move_node(node, node_parent, info) ⇒ Object
- #rename_node(node, node_parent, info) ⇒ Object
Methods included from DefineVariablesMethod
Instance Method Details
#check ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'app/controllers/mongoid_sortable_tree_controller.rb', line 17 def check operation = params[:operation] node = params[:node] node_parent = params[:node_parent] info = params[:info] return render(nothing: true, status: 406) unless operation && node && node_parent variable, collection, klass = self.the_define_common_variables # variable = self.instance_variable_set(variable, klass.find(id)) validation = self.send(operation.to_sym, node, node_parent, info) return render json: {id: validation[:id], operation: operation, message: validation[:msg] }, status: validation[:status] end |
#copy_node(node, node_parent, info) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'app/controllers/mongoid_sortable_tree_controller.rb', line 76 def copy_node(node,node_parent,info) begin variable, collection, klass = self.the_define_common_variables variable = self.instance_variable_set(variable, klass.where(:id => node[:id]).first) parent = klass.where(:id => node_parent[:id]).first id_mapping = {} variable.traverse(:breadth_first).each_with_index do |node,index| copied_item = node.clone id_mapping[node.id] = copied_item.id if index == 0 copied_item.update_attribute(:parent_id,parent.try(:id)) elsif id_mapping[copied_item.parent_id] copied_item.update_attribute(:parent_id,id_mapping[copied_item.parent_id]) end copied_item.rearrange_children! copied_item.save end return {id: id_mapping.keys.map(&:to_s), msg: 'cpoied', status: 200} rescue => e return {id: variable.id.to_s, msg: 'Failed to copy', status: 406} end end |
#create_node(node, node_parent, info) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 |
# File 'app/controllers/mongoid_sortable_tree_controller.rb', line 31 def create_node(node,node_parent,info) variable, collection, klass = self.the_define_common_variables variable = self.instance_variable_set(variable, klass.new(text: node[:text])) variable.parent = klass.where(:id => node_parent[:id]).first if variable.valid? variable.save return {id: variable.id.to_s, msg: 'created', status: 200} else return {id: variable.id.to_s, msg: variable.errors., status: 406} end end |
#delete_node(node, node_parent, info) ⇒ Object
53 54 55 56 57 58 59 60 61 |
# File 'app/controllers/mongoid_sortable_tree_controller.rb', line 53 def delete_node(node,node_parent,info) variable, collection, klass = self.the_define_common_variables variable = self.instance_variable_set(variable, klass.where(:id => node[:id]).first) if variable && variable.destroy return {id: variable.id.to_s, msg: 'deleted', status: 200} else return {id: variable.id.to_s, msg: variable.errors., status: 406} end end |
#move_node(node, node_parent, info) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'app/controllers/mongoid_sortable_tree_controller.rb', line 63 def move_node(node,node_parent,info) variable, collection, klass = self.the_define_common_variables variable = self.instance_variable_set(variable, klass.where(:id => node[:id]).first) parent = klass.where(:id => node_parent[:id]).first if variable && variable.update_attribute(:parent_id,parent.try(:id)) variable.rearrange_children! variable.save return {id: variable.id.to_s, msg: 'moved', status: 200} else return {id: variable.id.to_s, msg: variable.errors., status: 406} end end |
#rename_node(node, node_parent, info) ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'app/controllers/mongoid_sortable_tree_controller.rb', line 43 def rename_node(node,node_parent,info) variable, collection, klass = self.the_define_common_variables variable = self.instance_variable_set(variable, klass.where(:id => node[:id]).first) if variable && variable.update_attributes(:text => info) return {id: variable.id.to_s, msg: 'updated', status: 200} else return {id: variable.try(:id).to_s, msg: variable.try(:errors).try(:full_messages), status: 406} end end |