Created
May 22, 2017 23:58
-
-
Save aileron/ad096126bda87a110915eb79298421ea to your computer and use it in GitHub Desktop.
link_to_active
This file contains hidden or 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
# controller名でactiveを判断 | |
# controller_name or [controller_name] or current_page(root_path) | |
def link_to_active(href, condition, wrapper: 'li', **options, &block) | |
active = condition == true || | |
condition == controller.controller_name || | |
(condition.include?(controller.controller_name) if condition.is_a?(Array)) | |
wrapper_tag = wrapper | |
wrapper_class = '' | |
wrapper_tag, wrapper_class = wrapper.first if wrapper.is_a?(Hash) | |
options ||= {} | |
if wrapper_tag.nil? | |
options[:class] ||= '' | |
options[:class] += ' active' if active | |
link_to href, options, &block | |
else | |
content_tag(wrapper_tag, class: "#{wrapper_class} #{(' active' if active)}") do | |
link_to href, options, &block | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment