Class: Effective::Menu

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.update_from_effective_regions!(params) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/models/effective/menu.rb', line 22

def self.update_from_effective_regions!(params)
  Effective::Menu.transaction do
    (params || {}).each do |menu_id, attributes|
      menu = Effective::Menu.includes(:menu_items).find(menu_id)
      menu.menu_items.delete_all

      right = -1

      attributes[:menu_items_attributes].each do |_, atts|
        atts[:menuable_type] = 'Effective::Page' if atts[:menuable_type].blank?
        atts.delete(:id)
        right = [atts[:rgt].to_i, right].max
      end

      menu.attributes = attributes

      # So when we render the menu, we don't include the Root/Home item.
      # It has a left of 1 and a right of max(items.right)
      root_node = menu.menu_items.find { |menu_item| menu_item.lft == 1 }
      root_node ||= menu.menu_items.build(title: 'Home', url: '/', lft: 1, rgt: 2)
      root_node.rgt = right + 1

      menu.save!
    end
  end
end

Instance Method Details

#build(&block) ⇒ Object

This is the entry point to the DSL method for creating menu items



58
59
60
61
62
63
64
65
66
# File 'app/models/effective/menu.rb', line 58

def build(&block)
  raise 'build must be called with a block' if !block_given?

  root = menu_items.build(title: 'Home', url: '/', lft: 1, rgt: 2)
  root.parent = true
  instance_exec(&block) # A call to dropdown or item
  root.rgt = menu_items.map(&:rgt).max
  self
end

#contains?(obj) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
# File 'app/models/effective/menu.rb', line 53

def contains?(obj)
  menu_items.find { |menu_item| menu_item.url == obj || menu_item.menuable == obj }.present?
end

#to_sObject



49
50
51
# File 'app/models/effective/menu.rb', line 49

def to_s
  self[:title] || 'New Menu'
end