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



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

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

      attributes[:menu_items_attributes].each do |_, atts|
        atts[:menuable_type] = 'Effective::Page' if atts[:menuable_type].blank?
        atts.delete(:id)
      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)
      right = attributes[:menu_items_attributes].map { |_, atts| atts[:rgt].to_i }.max

      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



52
53
54
55
56
57
58
59
60
# File 'app/models/effective/menu.rb', line 52

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)


47
48
49
# File 'app/models/effective/menu.rb', line 47

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