Skip to content

Instantly share code, notes, and snippets.

@gudata
Created December 26, 2020 18:31
Show Gist options
  • Save gudata/c32e1f3fb63a03fd372be7127a12e21c to your computer and use it in GitHub Desktop.
Save gudata/c32e1f3fb63a03fd372be7127a12e21c to your computer and use it in GitHub Desktop.
current_item
# 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