Skip to content

Instantly share code, notes, and snippets.

@upinetree
Last active November 9, 2016 08:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save upinetree/ec8f88e8cf7cac8b024ff8655938b108 to your computer and use it in GitHub Desktop.
Save upinetree/ec8f88e8cf7cac8b024ff8655938b108 to your computer and use it in GitHub Desktop.
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