Class: MenuBlock::MenuItem

Inherits:
Object
  • Object
show all
Defined in:
lib/components/menu_block.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, name, default: false, match_params: {}, &route_block) ⇒ MenuItem

Returns a new instance of MenuItem.



67
68
69
70
71
72
73
# File 'lib/components/menu_block.rb', line 67

def initialize(context, name, default: false, match_params: {}, &route_block)
  @context = context
  @name = name
  @route_block = route_block
  @default = default
  @match_params = match_params
end

Instance Attribute Details

#defaultObject (readonly) Also known as: default?

Returns the value of attribute default.



62
63
64
# File 'lib/components/menu_block.rb', line 62

def default
  @default
end

#match_paramsObject (readonly)

Returns the value of attribute match_params.



63
64
65
# File 'lib/components/menu_block.rb', line 63

def match_params
  @match_params
end

#nameObject (readonly)

Returns the value of attribute name.



60
61
62
# File 'lib/components/menu_block.rb', line 60

def name
  @name
end

#route_blockObject (readonly)

Returns the value of attribute route_block.



61
62
63
# File 'lib/components/menu_block.rb', line 61

def route_block
  @route_block
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/components/menu_block.rb', line 79

def active?
  matches = []

  # Set default match_params. For example: This is the default matcher for :controller if :controller isn't specified
  match_params[:controller] ||= recognized_route[:controller]

  # Make sure everything in match_params is contained in params.
  match_params.each do |key,val|
    # This will handle arrays, strings, numbers, etc. params should always return a string or nil.
    matches << Array(val).compact.map(&:to_s).include?(params[key])
  end

  # Loop through query parameters to make sure they match
  ::Rack::Utils.parse_query(URI.parse(route).query).each do |param, val|
    matches << (params[param] == val)
  end

  matches.all?
end

#routeObject



75
76
77
# File 'lib/components/menu_block.rb', line 75

def route
  @route ||= route_block.call
end