Class: Spree::Admin::Navigation::Builder

Inherits:
Object
  • Object
show all
Defined in:
app/models/spree/admin/navigation/builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_itemObject (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

#registryObject (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, **options, &block)
  # If we have a parent item, set it in the options
  if parent_item
    options[:parent] = parent_item.key
    # Adjust position to be relative to parent
    options[:position] ||= parent_item.children.size * 10
  end

  item = registry.add(key, **options, &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, **options)
  registry.insert_after(target_key, new_key, **options)
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, **options)
  registry.insert_before(target_key, new_key, **options)
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, **position_options)
  registry.move(key, **position_options)
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, **options, &block)
  registry.replace(key, **options, &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, **options)
  registry.update(key, **options)
end