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)


56
57
58
# File 'lib/tabnav/tab.rb', line 56

def active?
  @active
end

#has_link?Boolean

Returns true if this tab has had a link set on it.

Returns:

  • (Boolean)


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

def has_link?
  !! @link_url
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.



47
48
49
50
51
52
53
# File 'lib/tabnav/tab.rb', line 47

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.



35
36
37
38
# File 'lib/tabnav/tab.rb', line 35

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



27
28
29
# File 'lib/tabnav/tab.rb', line 27

def named(text)
  @name = text
end

#renderObject

:nodoc:



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/tabnav/tab.rb', line 60

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 has_link?
      @template.link_to @name, @link_url, @link_options
    else
      @template. :span, @name
    end
  end
end