Class: BootstrapBaseHelper::List

Inherits:
Object
  • Object
show all
Defined in:
app/helpers/bootstrap_base_helper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(templte, *args) ⇒ List

Returns a new instance of List.

[View source]

10
11
12
13
14
15
# File 'app/helpers/bootstrap_base_helper.rb', line 10

def initialize(templte, *args)
  self.template = templte
  self.options = args.extract_options!.dup
  self.li_options = options.delete(:li_options) || {}
  self.collection = args
end

Instance Attribute Details

#blockObject

Returns the value of attribute block.


8
9
10
# File 'app/helpers/bootstrap_base_helper.rb', line 8

def block
  @block
end

#collectionObject

Returns the value of attribute collection.


8
9
10
# File 'app/helpers/bootstrap_base_helper.rb', line 8

def collection
  @collection
end

#li_optionsObject

Returns the value of attribute li_options.


8
9
10
# File 'app/helpers/bootstrap_base_helper.rb', line 8

def li_options
  @li_options
end

#optionsObject

Returns the value of attribute options.


8
9
10
# File 'app/helpers/bootstrap_base_helper.rb', line 8

def options
  @options
end

#templateObject

Returns the value of attribute template.


8
9
10
# File 'app/helpers/bootstrap_base_helper.rb', line 8

def template
  @template
end

Instance Method Details

#add(*args) ⇒ Object Also known as: <<

[View source]

17
18
19
20
# File 'app/helpers/bootstrap_base_helper.rb', line 17

def add(*args)
  li_opts = args.extract_options!
  collection << {options: li_opts, content: args.join}
end

#to_sObject

[View source]

24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/helpers/bootstrap_base_helper.rb', line 24

def to_s
  type = options.delete(:type) || 'unordered'
  tag = (type == 'ordered') ? 'ol' : 'ul'
  unstyled_class = (type == 'unstyled') ? 'unstyled ' : ''

  template.(tag, nil, template.merge_predef_class(unstyled_class, options)) do
    ul_content = ''

    collection.each do |obj|
      if obj.is_a?(Hash)
        if obj.has_key?(:options) && obj.has_key?(:content)
          ul_content << template.('li', nil, obj[:options].reverse_merge!(li_options)) do
            obj[:content].html_safe
          end
        else
          nested_collection = obj.to_a.first
          ul_content << template.('li', nil, li_options) do
            (nested_collection.first + template.list(*nested_collection.last, options)).html_safe
          end
        end
      elsif obj.is_a?(Array)
        ul_content << template.list(*obj, options)
      else
        ul_content << template.('li', nil, li_options) do
          obj.html_safe
        end
      end
    end

    ul_content.html_safe
  end
end