Module: ModelMixins::TreeNodeClassMethods

Defined in:
lib/model_mixins/tree_node_class_methods.rb

Instance Method Summary collapse

Instance Method Details

#child_nodes(id) ⇒ Object



3
4
5
# File 'lib/model_mixins/tree_node_class_methods.rb', line 3

def child_nodes(id)
  where(:parent_node_id => id.to_i).order("position ASC")
end

#create_node(params) ⇒ Object



22
23
24
25
26
# File 'lib/model_mixins/tree_node_class_methods.rb', line 22

def create_node(params)
  node = self.create(:parent_node_id => params['id'].to_i, :position => params['position'].to_i, :name => params['title'])

  {:status => "ok", :id => node.id, "data-settings" => node.to_json}
end

#get_children(id) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/model_mixins/tree_node_class_methods.rb', line 8

def get_children(id)
  nodes = []
  self.child_nodes(id).each do |tree_node|
    tree_node_name = tree_node.name.blank? ? I18.t('name_missing') : tree_node.name
    tree_node_state = tree_node.has_children? ? "closed" : ""
    nodes << {:attr => {:id => tree_node.id, :rel => "default", "data-settings" => tree_node.to_json}, :data => tree_node_name, :state => tree_node_state}
  end
  nodes
end

#move_node(params) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/model_mixins/tree_node_class_methods.rb', line 45

def move_node(params)
  node = self.find(params['id'].to_i)

  # backup for control if something changed
  node.old_position = node.position
  node.old_parent_node_id = node.parent_node_id

  if params['ref'].to_i == 0
    #root
    node.parent_node_id = 0
  else
    new_parent_node = self.find(params['ref'].to_i)
    node.parent_node = new_parent_node
  end
  node.position = params['position'].to_i
  node.save

  {:status => "ok"}
end

#remove_node(id) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/model_mixins/tree_node_class_methods.rb', line 29

def remove_node(id)
  node = self.find(id)
  node.deleting_root = true
  node.remove_child_nodes

  {:status => "ok"}
end

#rename_node(params) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/model_mixins/tree_node_class_methods.rb', line 37

def rename_node(params)
  node = self.find(params['id'].to_i)
  node.name = params['title']
  node.save

  {:status => "ok"}
end

#search_nodeObject



18
19
20
# File 'lib/model_mixins/tree_node_class_methods.rb', line 18

def search_node

end