Module: NavigationLinkTo

Defined in:
lib/navigation_link_to.rb,
lib/navigation_link_to/version.rb

Constant Summary collapse

VERSION =
"0.0.2"

Instance Method Summary collapse

Instance Method Details

#action?(*actions) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
12
# File 'lib/navigation_link_to.rb', line 9

def action?(*actions)
  actions = actions.map(&:to_sym)
  actions.include?(controller.action_name.to_sym)
end

#controller?(*controllers) ⇒ Boolean

Returns:

  • (Boolean)


4
5
6
7
# File 'lib/navigation_link_to.rb', line 4

def controller?(*controllers)
  controllers = controllers.map(&:to_sym)
  controllers.include?(controller.controller_name.to_sym)
end


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/navigation_link_to.rb', line 14

def navigation_link_to(*args, &block)
  if block_given?
    options      = args.first || {}
    html_options = args.second
    navigation_link_to(capture(&block), options, html_options)
  else
    title        = args[0]
    options      = args[1] || {}
    html_options = args[2] || {}
    url = url_for(options)
    controller = html_options[:controller]
    wrapper = html_options[:wrapper].nil? ? 'li' : html_options[:wrapper]
    is_current_controller = controller && controller?(controller)
    is_current_page = current_page?(url) && !html_options.has_key?(:active)
    is_force_active = html_options[:active]
    if is_current_controller || is_current_page || is_force_active
      html_options[:class] = [html_options[:class], 'active'].compact.join(' ')
      html_options[:wrapper_class] = [html_options[:wrapper_class], 'active'].compact.join(' ')
    end
    if wrapper
       wrapper, class: html_options[:wrapper_class] do
        badge, badge_class = html_options.delete(:badge), html_options.delete(:badge_class)
        wrapper_content = link_to(title, url, html_options, &block)
        wrapper_content << (:div, badge, class: badge_class) if badge
        wrapper_content
      end
    else
      link_to(title, options, html_options)
    end
  end
end