Module: SimpleActiveLinkTo
- Defined in:
- lib/simple_active_link_to/version.rb,
lib/simple_active_link_to/simple_active_link_to.rb
Constant Summary collapse
- VERSION =
"1.0.4"
- ACTIVE_OPTIONS =
%i[ active class_active class_inactive active_disable ].freeze
Instance Method Summary collapse
-
#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]].
-
#simple_active_link_to(name = nil, options = nil, html_options = nil, &block) ⇒ Object
Wrapper around link_to.
Instance Method Details
#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')
54 55 56 57 58 59 60 |
# File 'lib/simple_active_link_to/simple_active_link_to.rb', line 54 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']])
76 77 78 79 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/simple_active_link_to/simple_active_link_to.rb', line 76 def is_active_link?(url, condition = nil) @is_active_link ||= {} @is_active_link[[url, condition]] ||= begin case condition when :exclusive, :inclusive, nil url_path = url.split('#').first.split('?').first url_string = URI.parser.unescape(url_path).force_encoding(Encoding::BINARY) request_uri = URI.parser.unescape(request.path).force_encoding(Encoding::BINARY) if url_string == request_uri true elsif condition != :exclusive closing = url_string.end_with?('/') ? '' : '/' request_uri.start_with?(url_string + closing) else false end when :exact request.original_fullpath == url when Regexp request.original_fullpath.match?(condition) when Array controllers = Array(condition[0]) actions = Array(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, FalseClass condition when Hash condition.all? do |key, value| params[key].to_s == value.to_s end end end end |
#simple_active_link_to(name = nil, options = nil, html_options = nil, &block) ⇒ Object
Wrapper around link_to. Accepts following params:
:active => Boolean | Symbol | Regex | Controller/Action Pair
:class_active => String
:class_inactive => String
:active_disable => Boolean
Example usage:
simple_active_link_to('/users', class_active: 'enabled')
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/simple_active_link_to/simple_active_link_to.rb', line 18 def simple_active_link_to(name = nil, = nil, = nil, &block) if block_given? = = name name = capture(&block) end ||= {} = {} = {} .each do |k, v| if ACTIVE_OPTIONS.include?(k) [k] = v else [k] = v end end url = url_for() css_class = [:class] active_class = active_link_to_class(url, ) [:class] = "#{css_class} #{active_class}".strip if is_active_link?(url, [:active]) [:'aria-current'] = 'page' return content_tag(:span, name, ) if [:active_disable] == true end link_to(name, url, ) end |