1 2 3 4 5 6 7
def tab(name, options = {}) if name == controller.controller_name.humanize || root_of_path.humanize == name content_tag :li, link_to("<span>#{name}</span>", options), :class => "active" else content_tag :li, link_to("<span>#{name}</span>", options) end end
Refactorings
No refactoring yet !
Tien Dung
July 25, 2008, July 25, 2008 13:58, permalink
1 2 3 4
def tab(name, options = {}) match = [ controller.controller_name, root_of_path ].map(&:humanize).include?( name ) content_tag( :li, link_to("<span>#{name}</span>", options), match ? { :class => "active" } : nil ) end
PabloBM
July 25, 2008, July 25, 2008 21:00, permalink
One line longer than Tien's, but I think it's worth sacrificing it for readability
1 2 3 4 5
def tab(name, options = {}) attributes = {} attributes[:class] = 'active' if controller.controller_name.humanize || root_of_path.humanize content_tag :li, link_to("<span>#{name}</span>", options), attributes end
David
July 27, 2008, July 27, 2008 19:41, permalink
If I had to maintain this code, I'd prefer the first one (idlefingers' original), honestly.
jrun
August 7, 2008, August 07, 2008 20:46, permalink
1 2 3 4 5 6 7
def tab(name, options = {}) content_tag :li, link_to("<span>#{name}</span>", options), tab_tag_attributes end def tab_tag_attributes controller.controller_name.humanize || root_of_path.humanize ? {:class => 'active'} : {} end
Adam
August 10, 2008, August 10, 2008 22:14, permalink
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
class TabMaker def initialize(context, name, options = {}) @context, @name, @options = context, name, options end def method_missing(method, *args, &block) @context.send(method, *args, &block) end def active? [ controller.controller_name, root_of_path ].include?(@name.tableize) end def class_name active? ? "active" : nil end def to_s content_tag(:li, :class => class_name) do link_to(@options) do content_tag(:span, @name) end end end end def tab(name, options = {}) TabMaker.new(self, name, options) end
Adam
August 10, 2008, August 10, 2008 22:29, permalink
In my previous refactor attempt I wanted to illustrate using a class to simplify the code. However, for a case as simple as this it is arguably overkill. Thus, you may be more appropriate in this situation.
1 2 3 4 5 6 7 8 9 10 11
def tab(name, options = {}) content_tag(:li, :class => tab_class_name) do link_to(options) do content_tag(:span, name) end end end def tab_class_name "active" if [ controller.controller_name, root_of_path ].include?(name.tableize) end
jrun
August 10, 2008, August 10, 2008 23:56, permalink
Fixed my horribly wrong refactoring. That's what i get for doing it in a rush. In the past I've done something similar to Adam's first refactoring.
1 2 3 4 5 6 7
def tab(name, options = {}) content_tag :li, link_to("<span>#{name}</span>", options), tab_tag_attributes(name) end def tab_tag_attributes(tab_name) tab_name == controller.controller_name.humanize || tab_name == root_of_path.humanize ? {:class => 'active'} : {} end
This is for a tabbed navigation bar. If the current controller name or start of the request path matches the tab name, it adds an html class of "active" to the link.