Module: Bootstrap::ActiveLinkTo

Included in:
Docs::ApplicationHelper
Defined in:
lib/bootstrap/active_link_to.rb

Defined Under Namespace

Classes: ActiveLink

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)


89
90
91
# File 'lib/bootstrap/active_link_to.rb', line 89

def active_link_to(*args, &block)
  ActiveLink.new(self).render(*args, &block)
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')


97
98
99
100
# File 'lib/bootstrap/active_link_to.rb', line 97

def active_link_to_class(url, options={}, is_active=nil)
  is_active = is_active_link?(url, options[:active]) if is_active.nil?
  is_active ? (options[:class_active] ||= 'active') : options[:class_inactive]
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)


114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/bootstrap/active_link_to.rb', line 114

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)}(\/.*|\?.*)?$/).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