Skip to content

Instantly share code, notes, and snippets.

@mikecmpbll
Created February 16, 2015 14:28
Show Gist options
  • Save mikecmpbll/d4a048d69b03069be161 to your computer and use it in GitHub Desktop.
Save mikecmpbll/d4a048d69b03069be161 to your computer and use it in GitHub Desktop.
active?
# # #
# If you pass:
# - String, then it highlight for that controller name
# - Array, then it will highlight for those actions
# - Hash, then it will highlight for only those controller and action combinations
#
def is_active?(controller_action_hash_or_array_or_string)
controller = params[:controller]
action = params[:action]
controller_action_hash =
case controller_action_hash_or_array_or_string
when Array
{ controller => controller_action_hash_or_array_or_string }
when String
{ controller_action_hash_or_array_or_string => nil }
when Hash
controller_action_hash_or_array_or_string
else
raise ArgumentError, "what are you even ..."
end
controller_action_hash = Hash[controller_action_hash.map{|k,v| [k, [v || action].flatten]}]
"active" if controller_action_hash.key?(controller) && controller_action_hash[controller].include?(action)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment