Class: Tabit::Item

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::TagHelper, ActionView::Helpers::UrlHelper
Defined in:
lib/tabit/item.rb

Direct Known Subclasses

Builder

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, url = nil, options = {}) ⇒ Item

Returns a new instance of Item.



15
16
17
18
19
20
21
22
23
24
# File 'lib/tabit/item.rb', line 15

def initialize(name, url = nil, options = {})
  @name, @url, @options = name, url, options

  @active = @options.delete(:active) || configuration.active_detect
  @type = @options.delete(:type) || :default
  @children = []

  @options[:inner] ||= {}
  @options[:outer] ||= {}
end

Instance Attribute Details

#activeObject

Returns the value of attribute active.



13
14
15
# File 'lib/tabit/item.rb', line 13

def active
  @active
end

#childrenObject

Returns the value of attribute children.



8
9
10
# File 'lib/tabit/item.rb', line 8

def children
  @children
end

#nameObject

Returns the value of attribute name.



10
11
12
# File 'lib/tabit/item.rb', line 10

def name
  @name
end

#optionsObject

Returns the value of attribute options.



12
13
14
# File 'lib/tabit/item.rb', line 12

def options
  @options
end

#urlObject

Returns the value of attribute url.



11
12
13
# File 'lib/tabit/item.rb', line 11

def url
  @url
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/tabit/item.rb', line 101

def active?
  if template.is_active_link? url, active
    true
  else
    children.each do |child|
      if child.active?
        return true
      end
    end

    false
  end
end

#add(name, url = nil, options = {}) ⇒ Object



26
27
28
29
30
31
# File 'lib/tabit/item.rb', line 26

def add(name, url = nil, options = {})
  Item.new(name, url, options).tap do |adding|
    @children << adding
    yield adding if block_given?
  end
end

#divider(name = nil) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/tabit/item.rb', line 45

def divider(name = nil)
  options[:class] = '' if options[:class].nil?
  options[:class] << " #{configuration.divider_class}"
  options[:class].strip!

  @children << template.(
    :li,
    name,
    options
  )
end

#heading(name = nil, options = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/tabit/item.rb', line 33

def heading(name = nil, options = {})
  options[:class] = '' if options[:class].nil?
  options[:class] << " #{configuration.heading_class}"
  options[:class].strip!

  @children << template.(
    :li,
    name,
    options
  )
end

#to_sObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/tabit/item.rb', line 57

def to_s
  clazz = if active?
    configuration.active_class
  else
    ''
  end

  options[:outer][:class] = '' if @options[:outer][:class].nil?
  options[:outer][:class] << " #{clazz}"
  options[:outer][:class].strip!

  case @type
    when :dropdown
      options[:outer][:class] = '' if @options[:outer][:class].nil?
      options[:outer][:class] << " dropdown"
      options[:outer][:class].strip!

      options[:inner][:class] = '' if @options[:inner][:class].nil?
      options[:inner][:class] << " dropdown-toggle"
      options[:inner][:class].strip!

      options[:inner][:data] ||= {}
      options[:inner][:data][:toggle] = 'dropdown'

      template.(
        :li,
        [
          template.link_to([name, (:b, '', :class => 'caret')].join.html_safe, url, options[:inner]),
          output
        ].compact.join.html_safe,
        options[:outer]
      )
    else
      template.(
        :li,
        [
          template.link_to(name, url, options[:inner]),
          output
        ].compact.join.html_safe,
        options[:outer]
      )
  end
end