Class: DynamicMenu::DynamicMenu

Inherits:
MenuItem
  • Object
show all
Defined in:
lib/dynamic_menu.rb

Instance Attribute Summary

Attributes inherited from MenuItem

#items, #name, #target

Instance Method Summary (collapse)

Methods inherited from MenuItem

#[], #[]=, #active?, #active_item, #add, #next, #position, #previous, #self_or_inherited_attribute

Constructor Details

- (DynamicMenu) initialize(*args) {|_self| ... }

A new instance of DynamicMenu

Yields:

  • (_self)

Yield Parameters:



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/dynamic_menu.rb', line 55

def initialize(*args, &block)

  _parent     = args[0].is_a?(DynamicMenu) ? args.delete_at(0) : nil
  if kontroller = (args[0].is_a?(ActionController::Base) ? args.delete_at(0) : nil)
    request = kontroller.request
    @url = "#{request.protocol}#{request.host_with_port}#{request.request_uri}"
    @method = request.request_method.to_s
  end

  options = args.last.is_a?(Hash) ? args.pop : {}

  @parent             = _parent
  @items              = []
  @name               = options.delete(:name)         || args[0]
  @target             = options.delete(:target)       || args[1]
  @targets            = options.delete(:targets)      || [args[2]].flatten
  @active             = options.delete(:active)
  
  @html_options       = {}

  options.each { |key, value| self[key.to_sym] = value }

  yield(self) if block_given?

  @active = @active || [[@target] + @targets].flatten.compact.map { |target|
                if target.is_a?(String) or target.is_a?(Regexp)
                  url     = target
                  method  = :get
                elsif target.is_a?(Hash)
                  url     = target[:url]
                  method  = target[:method] || :get
                end
                current_page?(url, method)
              }.include?(true)

  if @active
    self[:html_options][:class].blank? ? (self[:html_options][:class] = (@active_class || 'active')) : (self[:html_options][:class] += ' ' + (@active_class || 'active'))
  end

end