Module: ActiveLinkTo

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

Constant Summary collapse

VERSION =
"1.0.1"

Instance Method Summary collapse

Instance Method Details

Wrapper around link_to. Accepts following params:

:active         => Boolean | Symbol | Regex | Controller/Action Pair
:class_active   => String
:class_inactive => String
:disable_active => Boolean
:wrap_tag       => Symbol

Example usage:

active_link_to('/users', :class_active => 'enabled')
active_link_to(users_path, :active => :exclusive, :wrap_tag => :li)


12
13
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
45
46
47
48
# File 'lib/active_link_to/active_link_to.rb', line 12

def active_link_to(*args, &block)
  if block_given?
    name          = capture(&block)
    options       = args[0] || {}
    html_options  = args[1] || {}
  else
    name          = args[0]
    options       = args[1] || {}
    html_options  = args[2] || {}
  end
  url = url_for(options)

  active_options  = { }
  link_options    = { }
  html_options.each do |k, v|
    if [:active, :class_active, :class_inactive, :active_disable, :wrap_tag].member?(k)
      active_options[k] = v
    else
      link_options[k] = v
    end
  end

  css_class = link_options.delete(:class).to_s + ' '
  css_class << active_link_to_class(url, active_options)
  css_class.strip!

  wrap_tag = active_options[:wrap_tag].present? ? active_options[:wrap_tag] : nil
  link_options[:class] = css_class if css_class.present?

  link = if active_options[:active_disable] === true && is_active_link?(url, active_options[:active])
    (:span, name, link_options)
  else
    link_to(name, url, link_options)
  end

  wrap_tag ? (wrap_tag, link, :class => css_class) : link
end

Returns css class name. Takes the link’s URL and its params Example usage:

active_link_to_class('/root', :class_active => 'on', :class_inactive => 'off')


54
55
56
57
58
59
60
# File 'lib/active_link_to/active_link_to.rb', line 54

def active_link_to_class(url, options = {})
  if is_active_link?(url, options[:active])
    options[:class_active] || 'active'
  else
    options[:class_inactive] || ''
  end
end

#is_active_link?(url, condition = nil) ⇒ Boolean

Returns true or false based on the provided path and condition Possible condition values are:

               Boolean -> true | false
                Symbol -> :exclusive | :inclusive
                 Regex -> /regex/
Controller/Action Pair -> [[:controller], [:action_a, :action_b]]

Example usage:

is_active_link?('/root', true)
is_active_link?('/root', :exclusive)
is_active_link?('/root', /^\/root/)
is_active_link?('/root', ['users', ['show', 'edit']])

Returns:

  • (Boolean)


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/active_link_to/active_link_to.rb', line 74

def is_active_link?(url, condition = nil)
  url = url_for(url).sub(/\?.*/, '') # ignore GET params
  case condition
  when :inclusive, nil
    !request.fullpath.match(/^#{Regexp.escape(url).chomp('/')}(\/.*|\?.*)?$/).blank?
  when :exclusive
    !request.fullpath.match(/^#{Regexp.escape(url)}\/?(\?.*)?$/).blank?
  when Regexp
    !request.fullpath.match(condition).blank?
  when Array
    controllers = [*condition[0]]
    actions     = [*condition[1]]
    (controllers.blank? || controllers.member?(params[:controller])) &&
    (actions.blank? || actions.member?(params[:action]))
  when TrueClass
    true
  when FalseClass
    false
  end
end