2b404bb55d9b5dc78841795c4b731771

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.

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 !

5071c5b861341c0dcfcf6ac86327701f

Tien Dung

July 25, 2008, July 25, 2008 13:58, permalink

2 ratings. Login to rate!
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
2b404bb55d9b5dc78841795c4b731771

idlefingers

July 25, 2008, July 25, 2008 14:17, permalink

No rating. Login to rate!

Thanks! That's much nicer.

4ebfc8d183472ac3cffff96be3e0ec68

PabloBM

July 25, 2008, July 25, 2008 21:00, permalink

1 rating. Login to rate!

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
C086c85007bbf3fe5fbd11a53aaaf330

David

July 27, 2008, July 27, 2008 19:41, permalink

No rating. Login to rate!

If I had to maintain this code, I'd prefer the first one (idlefingers' original), honestly.

Ba0b6f35c33d3275de38f31bdd848396

jrun

August 7, 2008, August 07, 2008 20:46, permalink

No rating. Login to rate!
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
8484df61a1434e91cb088f53cc089f18

Adam

August 10, 2008, August 10, 2008 22:14, permalink

No rating. Login to rate!
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
8484df61a1434e91cb088f53cc089f18

Adam

August 10, 2008, August 10, 2008 22:29, permalink

No rating. Login to rate!

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
Ba0b6f35c33d3275de38f31bdd848396

jrun

August 10, 2008, August 10, 2008 23:56, permalink

No rating. Login to rate!

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

Your refactoring





Format Copy from initial code

or Cancel