Module: Dunlop::ActiveClassHelper
- Included in:
- ApplicationHelper
- Defined in:
- app/helpers/dunlop/active_class_helper.rb
Instance Method Summary collapse
-
#active_class(*route_specs) ⇒ Object
Return active or nil based on the given route spec %li{ class: active_class(‘users’) # true if controller is users, false otherwise %li{ class: active_class(‘pages#about’) # true if controller is pages and action is about.
-
#current_route_spec?(route_spec, options = {}) ⇒ Boolean
Check if the current route matches the route given as argument.
Instance Method Details
#active_class(*route_specs) ⇒ Object
Return active or nil based on the given route spec
%li{ class: active_class('users') # true if controller is users, false otherwise
%li{ class: active_class('pages#about') # true if controller is pages and action is about
5 6 7 8 9 10 |
# File 'app/helpers/dunlop/active_class_helper.rb', line 5 def active_class(*route_specs) = route_specs. return nil if Array.wrap([:except]).any?{|exception| current_route_spec?(exception) } return 'active' if route_specs.any?{|rs| current_route_spec?(rs, ) } nil end |
#current_route_spec?(route_spec, options = {}) ⇒ Boolean
Check if the current route matches the route given as argument. The syntax is meant to be a bit similar to specifying routes in ‘config/routes.rb`.
current_route_spec?('products') #=> true if controller name is products, false otherwise
current_route_spec?('products#show') #=> true if controller_name is products AND action_name is show
current_route_spec?('#show') #=> true if action_name is show, false otherwise
NOTE: this helper is tested through the active_class helper
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'app/helpers/dunlop/active_class_helper.rb', line 19 def current_route_spec?(route_spec, = {}) return route_spec.match([controller_path, action_name].join('#')) if route_spec.is_a?(Regexp) controller, action = route_spec.split('#') return action == params[:id] if controller_path == 'high_voltage/pages' actual_controller_parts = controller_path.split('/') if controller #and controller_path == controller tested_controller_parts = controller.split('/') return if tested_controller_parts.size > actual_controller_parts.size if actual_controller_parts[0...tested_controller_parts.size] == tested_controller_parts # controller spec matches return true unless action action_name == action end else action_name == action end end |