Class: BootstrapForms::FormBuilder

Inherits:
Padrino::Helpers::FormBuilder::AbstractFormBuilder
  • Object
show all
Includes:
Helpers::Wrappers
Defined in:
lib/bootstrap_forms/form_builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ FormBuilder

Returns a new instance of FormBuilder.



7
8
9
10
# File 'lib/bootstrap_forms/form_builder.rb', line 7

def initialize(*args)
  @field_options = {}
  super(*args)
end

Instance Method Details

#actions(&block) ⇒ Object



129
130
131
132
# File 'lib/bootstrap_forms/form_builder.rb', line 129

def actions(&block)
  content = block_given? ? capture_html(&block) : [submit, cancel].join(' ')
  (:div, content, :class => 'form-actions').html_safe
end

#button(*args) ⇒ Object



117
118
119
# File 'lib/bootstrap_forms/form_builder.rb', line 117

def button(*args)
  template.bootstrap_button_tag(*args)
end

#cancel(*args) ⇒ Object



125
126
127
# File 'lib/bootstrap_forms/form_builder.rb', line 125

def cancel(*args)
  template.bootstrap_cancel_tag(*args)
end

#check_box(name, *args) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/bootstrap_forms/form_builder.rb', line 25

def check_box(name, *args)
  @name = name
  @field_options = args.extract_options!

  control_group_div do
    input_div(false) do
      if @field_options[:label] == false || @field_options[:label] == ''
        super(name, objectify_options(@field_options)) << messages
      else
        html = super(name, objectify_options(@field_options)) << (@field_options[:label].blank? ? @name.to_s.humanize : @field_options[:label])
        html << messages
        options = { :caption => html, :class => 'checkbox' }
        options[:for] = @field_options[:id] if @field_options.include?(:id)
        label(@name, options)
      end
    end
  end.html_safe
end

#collection_check_boxes(attribute, records, record_id, record_name, *args) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/bootstrap_forms/form_builder.rb', line 64

def collection_check_boxes(attribute, records, record_id, record_name, *args)
  @name = attribute
  @field_options = args.extract_options!

  boxes = records.map do |record|
    value = record.send(record_id)
    element_id = "#{object_model_name}_#{attribute}_#{value}"

    options = objectify_options(@field_options).merge(:value => value, :id => element_id)
    options[:checked] = "checked" if [object.send(attribute)].flatten.include?(value)

    checkbox = check_box_tag("#{object_model_name}[#{attribute}][]", options)
    checkbox << record.send(record_name)
    (:label, checkbox, :class => ['checkbox', ('inline' if @field_options[:inline])].compact.join(' '))
  end.join('').html_safe

  control_group_div do
    # Prevent "for" attribute for a non existant id
    @field_options[:id] = nil
    label_field << input_div { boxes }
  end.html_safe
end

#collection_radio_buttons(attribute, records, record_id, record_name, *args) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/bootstrap_forms/form_builder.rb', line 87

def collection_radio_buttons(attribute, records, record_id, record_name, *args)
  @name = attribute
  @field_options = args.extract_options!

  buttons = records.map do |record|
    value = record.send(record_id)
    element_id = "#{object_model_name}_#{attribute}_#{value}"
    options = objectify_options(@field_options).merge(:value => value, :id => element_id)
    radio = radio_button(attribute, options)
    radio << record.send(record_name)
    (:label, radio, :class => ['radio', ('inline' if @field_options[:inline])].compact.join(' '))
  end.join('').html_safe

  control_group_div do
    # Prevent "for" attribute for a non existant id
    @field_options[:id] = nil
    label_field << input_div { buttons }
  end.html_safe
end

#radio_buttons(name, values = {}, opts = {}) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/bootstrap_forms/form_builder.rb', line 44

def radio_buttons(name, values = {}, opts = {})
  @name = name
  @field_options = opts

  buttons = values.map do |text, value|
    # Padrino does not stringify false values
    html = radio_button(name, objectify_options(@field_options).merge(:value => "#{value}"))
    html << text

    options = { :caption => html, :class => 'radio', :for => nil }
    label("#{name}_#{value}", options)
  end.join('').html_safe

  control_group_div do
    # Prevent "for" attribute for a non existant id
    @field_options[:id] = nil
    label_field << input_div { buttons }
  end.html_safe
end

#submit(*args) ⇒ Object



121
122
123
# File 'lib/bootstrap_forms/form_builder.rb', line 121

def submit(*args)
  template.bootstrap_submit_tag(*args)
end

#uneditable_input(name, *args) ⇒ Object



107
108
109
110
111
112
113
114
115
# File 'lib/bootstrap_forms/form_builder.rb', line 107

def uneditable_input(name, *args)
  @name = name
  @field_options = args.extract_options!
  @field_options[:id] ||= field_id(@name)
  @field_options[:label] ||= "#{field_human_name(@name)}: " # conform to Padrino's default
  @field_options[:value] ||= object.send(@name.to_sym)

  template.uneditable_input_tag(@name, @field_options)
end