Class: Spree::Admin::Navigation::Builder
- Inherits:
-
Object
- Object
- Spree::Admin::Navigation::Builder
- Defined in:
- app/models/spree/admin/navigation/builder.rb
Instance Attribute Summary collapse
-
#parent_item ⇒ Object
readonly
Returns the value of attribute parent_item.
-
#registry ⇒ Object
readonly
Returns the value of attribute registry.
Instance Method Summary collapse
-
#add(key, **options, &block) ⇒ Object
Add a navigation item If parent_item is set, the item becomes a child.
-
#initialize(registry, parent_item = nil) ⇒ Builder
constructor
A new instance of Builder.
-
#insert_after(target_key, new_key, **options) ⇒ Object
Insert after another item.
-
#insert_before(target_key, new_key, **options) ⇒ Object
Insert before another item.
-
#move(key, **position_options) ⇒ Object
Move an item.
-
#remove(key) ⇒ Object
Remove an item.
-
#reorder(&block) ⇒ Object
Reorder items with a block.
-
#replace(key, **options, &block) ⇒ Object
Replace an item.
-
#section(key, label: nil, &block) ⇒ Object
Add a section (group of items).
-
#update(key, **options) ⇒ Object
Update an item.
Constructor Details
#initialize(registry, parent_item = nil) ⇒ Builder
Returns a new instance of Builder.
7 8 9 10 |
# File 'app/models/spree/admin/navigation/builder.rb', line 7 def initialize(registry, parent_item = nil) @registry = registry @parent_item = parent_item end |
Instance Attribute Details
#parent_item ⇒ Object (readonly)
Returns the value of attribute parent_item.
5 6 7 |
# File 'app/models/spree/admin/navigation/builder.rb', line 5 def parent_item @parent_item end |
#registry ⇒ Object (readonly)
Returns the value of attribute registry.
5 6 7 |
# File 'app/models/spree/admin/navigation/builder.rb', line 5 def registry @registry end |
Instance Method Details
#add(key, **options, &block) ⇒ Object
Add a navigation item If parent_item is set, the item becomes a child
14 15 16 17 18 19 20 21 22 23 24 |
# File 'app/models/spree/admin/navigation/builder.rb', line 14 def add(key, **, &block) # If we have a parent item, set it in the options if parent_item [:parent] = parent_item.key # Adjust position to be relative to parent [:position] ||= parent_item.children.size * 10 end item = registry.add(key, **, &block) item end |
#insert_after(target_key, new_key, **options) ⇒ Object
Insert after another item
61 62 63 |
# File 'app/models/spree/admin/navigation/builder.rb', line 61 def insert_after(target_key, new_key, **) registry.insert_after(target_key, new_key, **) end |
#insert_before(target_key, new_key, **options) ⇒ Object
Insert before another item
56 57 58 |
# File 'app/models/spree/admin/navigation/builder.rb', line 56 def insert_before(target_key, new_key, **) registry.insert_before(target_key, new_key, **) end |
#move(key, **position_options) ⇒ Object
Move an item
66 67 68 |
# File 'app/models/spree/admin/navigation/builder.rb', line 66 def move(key, **) registry.move(key, **) end |
#remove(key) ⇒ Object
Remove an item
46 47 48 |
# File 'app/models/spree/admin/navigation/builder.rb', line 46 def remove(key) registry.remove(key) end |
#reorder(&block) ⇒ Object
Reorder items with a block
76 77 78 |
# File 'app/models/spree/admin/navigation/builder.rb', line 76 def reorder(&block) instance_eval(&block) if block_given? end |
#replace(key, **options, &block) ⇒ Object
Replace an item
71 72 73 |
# File 'app/models/spree/admin/navigation/builder.rb', line 71 def replace(key, **, &block) registry.replace(key, **, &block) end |
#section(key, label: nil, &block) ⇒ Object
Add a section (group of items)
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'app/models/spree/admin/navigation/builder.rb', line 27 def section(key, label: nil, &block) section_item = add(key, section_label: label || key.to_s.humanize) if block_given? builder = self.class.new(registry, section_item) # Support both block styles: |nav| nav.add or just add if block.arity > 0 # Block expects parameter: do |nav| nav.add ... end block.call(builder) else # Block uses implicit self: do add ... end builder.instance_eval(&block) end end section_item end |
#update(key, **options) ⇒ Object
Update an item
51 52 53 |
# File 'app/models/spree/admin/navigation/builder.rb', line 51 def update(key, **) registry.update(key, **) end |