Active model serializer deprication smash

This commit is contained in:
2015-03-11 07:44:28 +01:00
parent 0a1aa06f2c
commit adff870774
23 changed files with 49 additions and 27 deletions
+40
View File
@@ -174,4 +174,44 @@ module ApplicationHelper
options[:onclick] = function
link_to title, 'javascript:void(0)', options
end
# 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
def active_class(*route_specs)
options = route_specs.extract_options!
active_class = options.delete(:active_class) || 'active'
return_class = Array.wrap(options.delete(:default_class))
if route_specs.map{|rs| current_route_spec?(rs) }.any?
if params.slice(*options.keys) == options.stringify_keys
return_class << active_class
end
end
return_class
end
# 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
def current_route_spec?(route_spec)
controller, action = route_spec.split('#')
if controller and controller_path == controller
if action
if action_name == action
true
end
else
# no action means all actions of controller
true
end
else
if action_name == action
true
end
end
end
end