Class: Tabnav::Tab

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

Direct Known Subclasses

Navbar

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

:nodoc:



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

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

Instance Attribute Details

The link options (if any)



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

def link_options
  @link_options
end

The link destination



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

def link_url
  @link_url
end

#nameObject

The name of this tab



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

def name
  @name
end

Instance Method Details

#active?Boolean

Returns true if this tab is highlighted.

Returns:

  • (Boolean)


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

def active?
  @active
end

#has_link?Boolean

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

Returns:

  • (Boolean)


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

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.



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

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.



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

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



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

def named(text)
  @name = text
end

#renderObject

:nodoc:



61
62
63
64
65
66
67
# File 'lib/tabnav/tab.rb', line 61

def render # :nodoc:
  options = @html_options.dup
  options[:class] = "#{options[:class]} active".strip if self.active?
  @template.(:li, options) do
    render_tab
  end
end