Module: Lolita::Menu::NestedTree::ClassMethods

Defined in:
lib/lolita-menu/nested_tree.rb,
lib/lolita-menu/nested_tree/configuration.rb

Instance Method Summary collapse

Instance Method Details

#all_tree_item_idsObject



121
122
123
# File 'lib/lolita-menu/nested_tree.rb', line 121

def all_tree_item_ids
	self.all.map(&:id)
end

#build_empty_branch(attributes) ⇒ Object



105
106
107
108
109
110
111
# File 'lib/lolita-menu/nested_tree.rb', line 105

def build_empty_branch(attributes)
  if self.lolita_nested_tree.build_method && self.respond_to?(self.lolita_nested_tree.build_method)
    self.send(self.lolita_nested_tree.build_method, attributes.merge(with_tree_scope))
  else
    self.new(attributes)
  end
end

#create_root!Object



46
47
48
49
50
# File 'lib/lolita-menu/nested_tree.rb', line 46

def create_root!
	new_branch = self.build_empty_branch(default_root_position)
	new_branch.save!
	new_branch
end

#default_root_positionObject



64
65
66
67
68
69
70
71
# File 'lib/lolita-menu/nested_tree.rb', line 64

def default_root_position
	{
		:lft=>1,
		:rgt=>2,
		:depth=>0,
		:parent_id=>nil
	}
end

#find_or_create_root(attributes = {}) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/lolita-menu/nested_tree.rb', line 52

def find_or_create_root(attributes = {})
	if root = self.with_tree_scope(attributes).root
		root
	else
		root = nil
		self.with_tree_scope(attributes) do
			root = create_root!
		end
		root
	end
end

#lolita_nested_tree(options = {}) ⇒ Object

Allows to use tree structure relationship between model records

Usage:

lolita_nested_tree :scope => “User”

Configuration options:

  • :scope - model name (as String)



18
19
20
# File 'lib/lolita-menu/nested_tree/configuration.rb', line 18

def lolita_nested_tree(options = {})
	@lolita_nested_tree ||= Lolita::Menu::NestedTree::Configuration.new(self, options)
end

#only_childrenObject



125
126
127
# File 'lib/lolita-menu/nested_tree.rb', line 125

def only_children
  where("rgt-lft=1")
end

#paginate_nested_tree(page, per_page, options) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/lolita-menu/nested_tree.rb', line 38

def paginate_nested_tree(page,per_page,options)
	if scope_class = self.lolita_nested_tree.scope_classes.first
		scope_class.page(page).per(per_page)
	else
		where(nil)
	end
end

#remove_items(ids) ⇒ Object



117
118
119
# File 'lib/lolita-menu/nested_tree.rb', line 117

def remove_items(ids)
	self.where(:id => ids).delete_all
end

#rootObject



73
74
75
# File 'lib/lolita-menu/nested_tree.rb', line 73

def root
	where(with_tree_scope.merge(:parent_id=>nil)).where("lft>0").order("lft asc").first
end

#update_item(item) ⇒ Object



113
114
115
# File 'lib/lolita-menu/nested_tree.rb', line 113

def update_item(item)
	self.where(:id => item.value_for(:item_id)).update_all(item.attribute_value_pairs_hash)					
end

#update_whole_tree(items, params = {}) ⇒ Object



95
96
97
98
99
100
101
102
103
# File 'lib/lolita-menu/nested_tree.rb', line 95

def update_whole_tree(items, params = {})
	begin
		self.transaction do
			tree_builder = Lolita::Menu::NestedTree::TreeBuilder.new(self, items, params)
			tree_builder.update_items
			true
		end
	end
end

#with_tree_scope(record_or_hash = nil, &block) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/lolita-menu/nested_tree.rb', line 77

def with_tree_scope(record_or_hash=nil, &block)
	if record_or_hash
		scope_hash = record_or_hash.respond_to?(:to_scope_hash) ? record_or_hash.to_scope_hash : record_or_hash
		if block_given?
			begin
				@lolita_menu_nested_tree_scope = scope_hash
				instance_eval(&block)
			ensure
				@lolita_menu_nested_tree_scope = nil
			end
		else
			where(scope_hash)
		end
	else
		@lolita_menu_nested_tree_scope || {}
	end
end