Module: ActiveLinkTo
- Defined in:
- lib/active_link_to/version.rb,
lib/active_link_to/active_link_to.rb
Constant Summary collapse
- VERSION =
"1.0.5"
Instance Method Summary collapse
-
#active_link_to(*args, &block) ⇒ Object
Wrapper around link_to.
-
#active_link_to_class(url, options = {}) ⇒ Object
Returns css class name.
-
#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]].
Instance Method Details
#active_link_to(*args, &block) ⇒ Object
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 49 50 51 52 |
# File 'lib/active_link_to/active_link_to.rb', line 12 def active_link_to(*args, &block) name = block_given? ? capture(&block) : args.shift = args.shift || {} = args.shift || {} url = url_for() = { } = { } .each do |k, v| if [:active, :class_active, :class_inactive, :active_disable, :wrap_tag, :wrap_class].member?(k) [k] = v else [k] = v end end css_class = .delete(:class).to_s + ' ' wrap_tag = [:wrap_tag].present? ? [:wrap_tag] : nil wrap_class = [:wrap_class].present? ? [:wrap_class] + ' ' : '' if wrap_tag.present? wrap_class << active_link_to_class(url, ) wrap_class.strip! else css_class << active_link_to_class(url, ) css_class.strip! end [:class] = css_class if css_class.present? ['aria-current'] = 'page' if is_active_link?(url, [:active]) link = if [:active_disable] === true && is_active_link?(url, [:active]) content_tag(:span, name, ) else link_to(name, url, ) end wrap_tag ? content_tag(wrap_tag, link, class: (wrap_class if wrap_class.present?)) : link end |
#active_link_to_class(url, options = {}) ⇒ Object
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')
58 59 60 61 62 63 64 |
# File 'lib/active_link_to/active_link_to.rb', line 58 def active_link_to_class(url, = {}) if is_active_link?(url, [:active]) [:class_active] || 'active' else [: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']])
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/active_link_to/active_link_to.rb', line 80 def is_active_link?(url, condition = nil) @is_active_link ||= {} @is_active_link[[url, condition]] ||= begin original_url = url url = Addressable::URI::parse(url).path path = request.original_fullpath case condition when :inclusive, nil !path.match(/^#{Regexp.escape(url).chomp('/')}(\/.*|\?.*)?$/).blank? when :exclusive !path.match(/^#{Regexp.escape(url)}\/?(\?.*)?$/).blank? when :exact path == original_url when Regexp !path.match(condition).blank? when Array controllers = [*condition[0]] actions = [*condition[1]] (controllers.blank? || controllers.member?(params[:controller])) && (actions.blank? || actions.member?(params[:action])) || controllers.any? do |controller, action| params[:controller] == controller.to_s && params[:action] == action.to_s end when TrueClass true when FalseClass false when Hash condition.all? do |key, value| params[key].to_s == value.to_s end end end end |