Module: Lolita::Menu::NestedTree::InstanceMethods

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

Instance Method Summary collapse

Instance Method Details

#ancestorsObject



159
160
161
162
163
# File 'lib/lolita-menu/nested_tree.rb', line 159

def ancestors
	@ancestors ||= self.class.where("lft<:left AND rgt>:right AND parent_id IS NOT NULL",{
		:left => self.lft, :right => self.rgt
	}).with_tree_scope(self).order("lft asc")
end

#append(item) ⇒ Object

Raises:

  • (ArgumentError)


192
193
194
195
196
# File 'lib/lolita-menu/nested_tree.rb', line 192

def append(item)
	raise ArgumentError, "can't append itself" if self == item
	append_item(item)
	recalculate_positions_after(:append)
end

#childrenObject



143
144
145
# File 'lib/lolita-menu/nested_tree.rb', line 143

def children
	@children ||= self.class.where(:parent_id => self.id).with_tree_scope(self).order("lft asc")
end

#descendantsObject



153
154
155
156
157
# File 'lib/lolita-menu/nested_tree.rb', line 153

def descendants
	@descendants ||= self.class.where("lft>:left AND rgt<:right",{
		:left => self.lft, :right => self.rgt
	}).with_tree_scope(self).order("lft asc")
end

#only_childrenObject



180
181
182
# File 'lib/lolita-menu/nested_tree.rb', line 180

def only_children
	@only_children ||= self.class.with_tree_scope(self).order("lft asc")
end

#parentObject



135
136
137
# File 'lib/lolita-menu/nested_tree.rb', line 135

def parent
	@parent ||= self.class.find_by_id(self.parent_id)
end

#parent=(parent_item) ⇒ Object



139
140
141
# File 'lib/lolita-menu/nested_tree.rb', line 139

def parent=(parent_item)
	@parent = parent_item
end

#parentsObject



165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/lolita-menu/nested_tree.rb', line 165

def parents
	unless @parent
		current_parent_id = self.parent_id
		@parents = self.ancestors.reverse.inject([]){|results,item|
			if item.parent_id && item.id == current_parent_id
				current_parent_id = item.parent_id
				[item] + results
			else
				results
			end
		}
	end
	@parents
end

#rootObject



184
185
186
187
188
189
190
# File 'lib/lolita-menu/nested_tree.rb', line 184

def root
	if self.parent_id.nil?
		self
	else
		self.class.root
	end
end

#root?Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/lolita-menu/nested_tree.rb', line 131

def root?
	self.parent_id.nil?
end

#self_and_descendantsObject



147
148
149
150
151
# File 'lib/lolita-menu/nested_tree.rb', line 147

def self_and_descendants
	@self_and_descendants ||= self.class.where("lft>=:left AND rgt<=:right",{
		:left => self.lft,:right => self.rgt
	}).with_tree_scope(self).order("lft asc")
end

#to_scope_hashObject



198
199
200
201
202
203
# File 'lib/lolita-menu/nested_tree.rb', line 198

def to_scope_hash
	self.class.lolita_nested_tree.scope_keys.inject({}) do |result, key|
		result[key] = self.send(key)
		result
	end					
end