Class: ActionNav::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/action_nav/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller) ⇒ ActionNav::Base

Initialize a new navigation

Parameters:

  • controller (ActionController::Base)


12
13
14
15
16
# File 'lib/action_nav/base.rb', line 12

def initialize(controller)
  @controller = controller
  @active_paths = []
  @context = {}
end

Instance Attribute Details

#active_pathsObject (readonly)

Returns the value of attribute active_paths.



22
23
24
# File 'lib/action_nav/base.rb', line 22

def active_paths
  @active_paths
end

#contextHash (readonly)

The context for this navigation.

Returns:

  • (Hash)


27
28
29
# File 'lib/action_nav/base.rb', line 27

def context
  @context
end

#controllerActionController::Base (readonly)

The controller that initialized this navigation.

Returns:

  • (ActionController::Base)


21
22
23
# File 'lib/action_nav/base.rb', line 21

def controller
  @controller
end

Class Method Details

.item(id, &block) ⇒ ActionNav::Item

Add a new item to this navigation

Parameters:

  • id (Symbol)

Returns:



72
73
74
75
76
# File 'lib/action_nav/base.rb', line 72

def self.item(id, &block)
  item = Item.new(nil, id)
  item.dsl(&block) if block_given?
  items[id] = item
end

.itemsHash

Return all items for this navigation

Returns:

  • (Hash)


81
82
83
# File 'lib/action_nav/base.rb', line 81

def self.items
  @items ||= {}
end

Instance Method Details

#activate(*parts) ⇒ Object Also known as: active

Add an active navigation by passing the full path to active item.



44
45
46
# File 'lib/action_nav/base.rb', line 44

def activate(*parts)
  @active_paths.push(parts)
end

#active_path?(*parts) ⇒ Boolean

Is the given active path?

Returns:

  • (Boolean)


52
53
54
55
56
57
# File 'lib/action_nav/base.rb', line 52

def active_path?(*parts)
  @active_paths.any? do |path|
    a = path.size.times.map { |i| path[0, path.size - i] }
    a.include?(parts)
  end
end

#add_context(key, value) ⇒ Hash

Add context to this navigation.

Parameters:

  • key (Symbol)
  • value (Object)

Returns:

  • (Hash)


64
65
66
# File 'lib/action_nav/base.rb', line 64

def add_context(key, value)
  @context[key] = value
end

#itemsArray<ActionNav::ItemInstance>

Return a full list of items for this instance as instances.

Returns:



33
34
35
36
37
38
39
40
# File 'lib/action_nav/base.rb', line 33

def items
  @items ||= self.class.items.each_with_object([]) do |(key, item), array|
    instance = ItemInstance.new(self, item)
    unless instance.hidden?
      array << instance
    end
  end
end