Module: TwitterBootstrapCombo::ViewHelpers

Defined in:
lib/twitter_bootstrap_combo/view_helpers.rb

Instance Method Summary collapse

Instance Method Details

#combo_box(object, method, choices, options = {}, html_options = {}) ⇒ Object

Returns a complete Twitter Bootstrap widget tailored for accessing a specified attribute (identified by method) on an object assigned to the template (identified by object) using a Twitter Bootstrap menu with choices. Additional options can be passed as a hash with options and html_options.

Supported options:

  • :include_blank - If set to true, an empty option will be created.

Supported html_options:

  • None yet

Examples:

combo_box(:event, :calendar_id, [ [ "Calendar 1", 1 ], [ "Calendar 2", 2] ])

container(:event, :calendar_id, options_from_collection_for_combo_box(Calendar.all, :id, :name, @event.calendar_id), :include_blank => true)


53
54
55
56
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
# File 'lib/twitter_bootstrap_combo/view_helpers.rb', line 53

def combo_box(object, method, choices, options = {}, html_options = {})
  current_value = self.instance_variable_get("@#{object}").instance_variable_get("@attributes")[method.to_s].value

  current_text = "(None)"
  if Array === choices then
    if ! Array === choices[0] then
      current_text = current_value
    else
      choices.each do |element|
        if current_value == element[1] then
          current_text = element[0]
        end
      end
    end
  end

  hidden_field(object, method) +
  (:div, :class => "btn-group") do
    link_to((:span, strip_tags(current_text), :class => "combo_box_text") + " " + (:i, "", :class => "fa fa-caret-down"), "#", :class => "btn btn-default dropdown-toggle", :data => { :toggle => "dropdown" }) +
    (:ul, :class => "dropdown-menu", :data => { :for => "#{object}_#{method}" }) do
      if options[:include_blank] then
        css_class = []
        if current_value == nil then
          css_class << "active"
        end
        (:li, :class => css_class.join(" ")) do
          link_to("(None)", '#', :class => "combo_box_item")
        end + (:li, "", :class => "divider")
      else
        "".html_safe
      end +
      options_for_combo_box(choices, current_value)
    end
  end
end

#options_for_combo_box(container, selected = nil) ⇒ Object

Accepts a container and return a string of combo_box items. If selected is specified, the corresponding item is marked as active.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/twitter_bootstrap_combo/view_helpers.rb', line 5

def options_for_combo_box(container, selected = nil)
  return container if String === container

  container.map do |element|
    if Array === element then
      text, value = element
    else
      text, value = element, element
    end
    css_class = []
    if selected == value then
      css_class << "active"
    end
    (:li, :class => css_class.join(" ")) do
      link_to(text, '#', :class => "combo_box_item", :data => { :value => value })
    end
  end.join().html_safe
end

#options_from_collection_for_combo_box(collection, value_method, text_method, selected = nil) ⇒ Object

Returns a string of combo_box items that have been compiled by iterating over the collection and assigning the result of the call to the value_method as the item value and the text_method as the item text. If selected is specified, the item with the corresponding value is marked as selected.



29
30
31
32
33
34
35
# File 'lib/twitter_bootstrap_combo/view_helpers.rb', line 29

def options_from_collection_for_combo_box(collection, value_method, text_method, selected = nil)
  options = collection.map do |element|
    [ element.send(text_method), element.send(value_method) ]
  end

  options_for_combo_box(options, selected)
end