Class: Effective::Menu
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Effective::Menu
- Defined in:
- app/models/effective/menu.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#build(&block) ⇒ Object
This is the entry point to the DSL method for creating menu items.
- #contains?(obj) ⇒ Boolean
- #to_s ⇒ Object
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 |, attributes| = Effective::Menu.includes(:menu_items).find() ..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 .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 = ..find { || .lft == 1 } root_node ||= ..build(title: 'Home', url: '/', lft: 1, rgt: 2) root_node.rgt = right + 1 .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 = .build(title: 'Home', url: '/', lft: 1, rgt: 2) root.parent = true instance_exec(&block) # A call to dropdown or item root.rgt = .map(&:rgt).max self end |
#contains?(obj) ⇒ Boolean
53 54 55 |
# File 'app/models/effective/menu.rb', line 53 def contains?(obj) .find { || .url == obj || . == obj }.present? end |
#to_s ⇒ Object
49 50 51 |
# File 'app/models/effective/menu.rb', line 49 def to_s self[:title] || 'New Menu' end |