Class: SpudMenuItem

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/spud_menu_item.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.grouped(menu) ⇒ Object



27
28
29
# File 'app/models/spud_menu_item.rb', line 27

def self.grouped(menu)
	return menu.spud_menu_items_combined.group_by(&:parent_type)
end

.options_tree_for_item(menu, config = {}) ⇒ Object

Returns an array of pages in order of heirarchy :fitler Filters out a page by ID, and all of its children

:value Pick an attribute to be used in the value field, defaults to ID


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/models/spud_menu_item.rb', line 34

def self.options_tree_for_item(menu,config={})
	collection = config[:collection] || self.grouped(menu)
	level 		 = config[:level] 		 || 0
	parent_id  = config[:parent_id]  || nil
	parent_type  = config[:parent_type]  || 'SpudMenu'
	filter 		 = config[:filter] 		 || nil
	value      = config[:value]			 || :id
	list 			 = []
	if parent_type == 'SpudMenu' && collection[parent_type]
		item_collection = collection['SpudMenuItem'].group_by(&:parent_id) if collection['SpudMenuItem']
		collection[parent_type].each do |c|
			if filter.blank? || c.id != filter.id
				list << [level.times.collect{ '- ' }.join('') + c.name, c[value]]
				list += self.options_tree_for_item(menu,{:collection => item_collection, :parent_id => c.id, :level => level+1, :filter => filter,:parent_type => "SpudMenuItem"})
			end
		end
	else
		if collection[parent_id]
			collection[parent_id].each do |c|
				if filter.blank? || c.id != filter.id
					list << [level.times.collect{ '- ' }.join('') + c.name, c[value]]
					list += self.options_tree_for_item(menu,{:collection => collection, :parent_id => c.id, :level => level+1, :filter => filter,:parent_type => "SpudMenuItem"})
				end
			end
		end
	end
	
	return list
end

Instance Method Details

#options_tree(options, depth, current = nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/models/spud_menu_item.rb', line 15

def options_tree(options,depth,current = nil)
	sub_items = self.spud_menu_items
	sub_items = sub_items.where(["id != ?",current.id]) if !current.blank? && !current.id.blank?
    if(sub_items.blank?)
      return options
    end
    sub_items.each do |item|
      options << ["#{'-'*depth} #{item.name}",item.id]
      options = item.options_tree(options,depth+1,current)
    end
    return options
end