Last active
November 9, 2016 08:17
-
-
Save upinetree/ec8f88e8cf7cac8b024ff8655938b108 to your computer and use it in GitHub Desktop.
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
module ApplicationHelper | |
# activated の条件が真のとき、.active クラス付きの <a> タグを生成 | |
# | |
# 例: | |
# = activated_link_to params[:sort] == 'newly', hogehoge_path(sort: :newly) do | |
# %span 新着順 | |
# | |
def activated_link_to(activated, name = nil, options = {}, html_options = {}, &block) | |
if block_given? | |
html_options = options | |
options = name | |
end | |
options ||= {} | |
if activated | |
case html_options[:class] | |
when String | |
html_options[:class] << " active" | |
when Array | |
html_options[:class] << "active" | |
when nil | |
html_options[:class] = "active" | |
end | |
end | |
if block_given? | |
link_to(options, html_options, &block) | |
else | |
link_to(name, options, html_options) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment