Created
December 26, 2020 18:31
-
-
Save gudata/c32e1f3fb63a03fd372be7127a12e21c to your computer and use it in GitHub Desktop.
current_item
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Usage | |
# current_item('public') | |
# current_item(:public) | |
# current_item('public#dashboard') | |
# current_item(['settings#private', 'settings#mail']) | |
# current_item([['settings, 'private']', ['settings', ['mail']]) | |
# current_item([[:settings, :private]', [:settings, [:mail]]) | |
NavActiveClasses = current_hash = { | |
'aria-current': 'page', | |
'class': 'active' | |
} | |
def current_item(something) | |
if something.is_a?(String) || something.is_a?(Symbol) | |
something = "#{something}" | |
if something.include?('#') | |
item = something.split('#') | |
return NavActiveClasses if "#{item.first}" == controller_name && "#{item.last}" == action_name | |
else | |
return NavActiveClasses if "#{something}" == controller_name | |
end | |
return {} | |
end | |
something.each do |item| | |
case item | |
when String | |
if item.include?('#') | |
item = item.split('#') | |
return NavActiveClasses if "#{item.first}" == controller_name && "#{item.last}" == action_name | |
else | |
return NavActiveClasses if item.include?(controller_name) | |
end | |
when Array | |
case item | |
when Array | |
puts("#{something} -> controller_name == #{item.first} && action_name == #{item.last})") | |
return NavActiveClasses if controller_name == "#{item.first}" && action_name == "#{item.last}" | |
when String | |
if item.include('#') | |
item = item.split('#') | |
end | |
puts("#{something} -> controller_name == #{item.first} && action_name == #{item.last})") | |
return NavActiveClasses if controller_name == item.first && action_name == item.last | |
end | |
end | |
end | |
{} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment