Class: Tabnav::Tab

Inherits:
Object
  • Object
show all
Defined in:
lib/tabnav/tab.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template, params, html_options = {}) ⇒ Tab

:nodoc:



4
5
6
7
8
9
10
# File 'lib/tabnav/tab.rb', line 4

def initialize(template, params, html_options = {}) # :nodoc:
  @html_options = html_options
  @params = params
  @template = template
  @name = ''
  @active = false
end

Instance Attribute Details

The link options (if any)



19
20
21
# File 'lib/tabnav/tab.rb', line 19

def link_options
  @link_options
end

The link destination



16
17
18
# File 'lib/tabnav/tab.rb', line 16

def link_url
  @link_url
end

#nameObject

The name of this tab



13
14
15
# File 'lib/tabnav/tab.rb', line 13

def name
  @name
end

Instance Method Details

#active?Boolean

Returns true of this tab is highlighted.

Returns:

  • (Boolean)


51
52
53
# File 'lib/tabnav/tab.rb', line 51

def active?
  @active
end

#highlights_on(rule) ⇒ Object

Adds a highlight condition to this tab. rule can be one of the following:

  • A Hash: The tab will be highlighted if all the values in the given hash match the params hash (strings and symbols are treated as equivelent).

  • A Proc: The proc will be called, and the tab will be highlighted if it returns true.

If multiple highlight conditions are given, the tab will be highlighted if any of them match.



42
43
44
45
46
47
48
# File 'lib/tabnav/tab.rb', line 42

def highlights_on(rule)
  if rule.is_a?(Hash)
    @active |= rule.with_indifferent_access.all? {|k, v| @params[k].to_s == v.to_s}
  elsif rule.is_a?(Proc)
    @active |= rule.call
  end
end

Sets the link destination.

link_options is an option hash of options that will be passed through to the link_to call.



30
31
32
33
# File 'lib/tabnav/tab.rb', line 30

def links_to(url, link_options = {})
  @link_url = url
  @link_options = link_options
end

#named(text) ⇒ Object

Sets the name of this tab. This will be used as the contents of the link or span



22
23
24
# File 'lib/tabnav/tab.rb', line 22

def named(text)
  @name = text
end

#renderObject

:nodoc:



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/tabnav/tab.rb', line 55

def render # :nodoc:
  @html_options[:class] = "#{@html_options[:class]} active".strip if self.active?
  partial = @html_options.delete(:tab_content_partial)
  @template.(:li, @html_options) do
    if partial
      @template.render :partial => partial, :locals => {:tab => self}
    elsif @link_url
      @template.link_to @name, @link_url, @link_options
    else
      @template. :span, @name
    end
  end
end