Class: Facades::Helpers::Navigation::NavigationLink

Inherits:
Object
  • Object
show all
Defined in:
lib/facades/helpers/navigation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text, href, options) ⇒ NavigationLink

Returns a new instance of NavigationLink.



169
170
171
172
173
174
# File 'lib/facades/helpers/navigation.rb', line 169

def initialize(text, href, options)
  @href, @text, @options = href, text, options
  @matcher  = (options.delete(:proc) || options.delete(:matcher))
  curr_path = options.delete(:path)
  @matcher ||= curr_path
end

Instance Attribute Details

#hrefObject

Returns the value of attribute href.



166
167
168
# File 'lib/facades/helpers/navigation.rb', line 166

def href
  @href
end

#matcherObject (readonly)

Returns the value of attribute matcher.



167
168
169
# File 'lib/facades/helpers/navigation.rb', line 167

def matcher
  @matcher
end

#optionsObject

Returns the value of attribute options.



166
167
168
# File 'lib/facades/helpers/navigation.rb', line 166

def options
  @options
end

#textObject

Returns the value of attribute text.



166
167
168
# File 'lib/facades/helpers/navigation.rb', line 166

def text
  @text
end

Instance Method Details

#active?Boolean

Checks the link’s href against either a proc, regexp, or current request.path depending on the options provided. Returns true if there is a match, false if not.

Returns:

  • (Boolean)


182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/facades/helpers/navigation.rb', line 182

def active?
  return matcher.call(href) if matcher.is_a?(Proc)
  return ( (href =~ matcher).to_i >= 1 ) if matcher.is_a?(Regexp)
  
  match_path = normalize_path(matcher)
  href_path  = normalize_path(href)
  
  return false if href_path.blank? && !match_path.blank?
  
  if key = options.delete(:match)
    path_matcher = case key.to_s
    when 'exact'  then match_path.match(%r{^/?#{href_path}/?$}i)
    when 'after'  then match_path.match(%r{/?#{href_path}/?(.*)$}i)
    when 'before' then match_path.match(%r{#{href_path}/?$}i)
    end
  else
    path_matcher = match_path.match(/#{href_path}\/.*/i)
  end
  !!(match_path == href_path || path_matcher)
end