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



31
32
33
# File 'app/models/spud_menu_item.rb', line 31

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


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
63
64
# File 'app/models/spud_menu_item.rb', line 38

def self.options_tree_for_item(menu, config = {})
  collection = config[:collection] || 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 << [Array.new(level) { '- ' }.join('') + c.name, c[value]]
        list += options_tree_for_item(menu, collection: item_collection, parent_id: c.id, level: level + 1, filter: filter, parent_type: 'SpudMenuItem')
      end
    end
  else
    collection[parent_id]&.each do |c|
      if filter.blank? || c.id != filter.id
        list << [Array.new(level) { '- ' }.join('') + c.name, c[value]]
        list += options_tree_for_item(menu, collection: collection, parent_id: c.id, level: level + 1, filter: filter, parent_type: 'SpudMenuItem')
      end
    end
  end

  return list
end

Instance Method Details

#get_urlObject



12
13
14
15
16
17
18
# File 'app/models/spud_menu_item.rb', line 12

def get_url
  if !spud_page.blank?
    return spud_page.url_name
  else
    return url
  end
end

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



20
21
22
23
24
25
26
27
28
29
# File 'app/models/spud_menu_item.rb', line 20

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