Class: BootstrapForms::FormBuilder

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

Constant Summary

Constants included from Helpers::Wrappers

Helpers::Wrappers::BOOTSTRAP_OPTIONS

Instance Method Summary collapse

Instance Method Details

#actions(&block) ⇒ Object



221
222
223
224
225
226
227
228
229
# File 'lib/bootstrap_forms/form_builder.rb', line 221

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

#button(name = nil, args = {}) ⇒ Object



192
193
194
195
196
197
198
199
200
# File 'lib/bootstrap_forms/form_builder.rb', line 192

def button(name = nil, args = {})
  name, args = nil, name if name.is_a?(Hash)
  @name = name
  @field_options = field_options(args)
  @args = args

  @field_options[:class] ||= 'btn'
  super(name, args.merge(@field_options))
end

#cancel(name = nil, args = {}) ⇒ Object



212
213
214
215
216
217
218
219
# File 'lib/bootstrap_forms/form_builder.rb', line 212

def cancel(name = nil, args = {})
  name, args = nil, name if name.is_a?(Hash)
  name ||= I18n.t('bootstrap_forms.buttons.cancel')
  @field_options = field_options(args)
  @field_options[:class] ||= 'btn cancel'
  @field_options[:back] ||= :back
  link_to(name, @field_options[:back], :class => @field_options[:class])
end

#check_box(name, args = {}) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/bootstrap_forms/form_builder.rb', line 82

def check_box(name, args = {})
  @name = name
  @field_options = field_options(args)
  @args = args

  control_group_div do
    input_div do
      @field_options.merge!(required_attribute)
      if @field_options[:label] == false || @field_options[:label] == ''
        extras { super(name, @args.merge(@field_options)) }
      else
        klasses = 'checkbox'
        klasses << ' inline' if @field_options.delete(:inline) == true
        @args.delete :inline
        label(@name, :class => klasses) do
          extras { super(name, @args.merge(@field_options)) + (@field_options[:label].blank? ? human_attribute_name : @field_options[:label])}
        end
      end
    end
  end
end

#collection_check_boxes(attribute, records, record_id, record_name, args = {}) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/bootstrap_forms/form_builder.rb', line 129

def collection_check_boxes(attribute, records, record_id, record_name, args = {})
  @name = attribute
  @field_options = field_options(args)
  @args = args

  control_group_div do
    label_field + input_div do
      options = @field_options.except(*BOOTSTRAP_OPTIONS).merge(required_attribute)
      # Since we're using check_box_tag() we may have to lookup the instance ourselves
      instance = object || @template.instance_variable_get("@#{object_name}")
      boxes = records.collect do |record|
        options[:id] = "#{object_name}_#{attribute}_#{record.send(record_id)}"
        checkbox = check_box_tag("#{object_name}[#{attribute}][]", record.send(record_id), [instance.send(attribute)].flatten.include?(record.send(record_id)), options)

        (:label, :class => ['checkbox', ('inline' if @field_options[:inline])].compact) do
          checkbox + record.send(record_name)
        end
      end.join('')
      boxes << extras
      boxes.html_safe
    end
  end
end

#collection_radio_buttons(attribute, records, record_id, record_name, args = {}) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/bootstrap_forms/form_builder.rb', line 153

def collection_radio_buttons(attribute, records, record_id, record_name, args = {})
  @name = attribute
  @field_options = field_options(args)
  @args = args

  control_group_div do
    label_field + input_div do
      options = @field_options.merge(required_attribute)
      buttons = records.collect do |record|
        radiobutton = radio_button(attribute, record.send(record_id), options)
        (:label, :class => ['radio', ('inline' if @field_options[:inline])].compact) do
          radiobutton + record.send(record_name)
        end
      end.join('')
      buttons << extras
      buttons.html_safe
    end
  end
end

#error_messagesObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/bootstrap_forms/form_builder.rb', line 8

def error_messages
  if object.try(:errors) and object.errors.full_messages.any?
    (:div, :class => 'alert alert-block alert-error validation-errors') do
      (:h4, I18n.t('bootstrap_forms.errors.header', :model => object.class.model_name.human), :class => 'alert-heading') +
      (:ul) do
        object.errors.full_messages.map do |message|
          (:li, message)
        end.join('').html_safe
      end
    end
  else
    '' # return empty string
  end
end

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



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/bootstrap_forms/form_builder.rb', line 104

def radio_buttons(name, values = {}, opts = {})
  @name = name
  @field_options = @options.slice(:namespace, :index).merge(opts.merge(required_attribute))
  control_group_div do
    label_field + input_div do
      klasses = 'radio'
      klasses << ' inline' if @field_options.delete(:inline) == true

      buttons = values.map do |text, value|
        radio_options = @field_options
        if value.is_a? Hash
          radio_options = radio_options.merge(value)
          value = radio_options.delete(:value)
        end

        label("#{@name}_#{value}", :class => klasses) do
          radio_button(name, value, radio_options) + text
        end
      end.join('')
      buttons << extras
      buttons.html_safe
    end
  end
end

#submit(name = nil, args = {}) ⇒ Object



202
203
204
205
206
207
208
209
210
# File 'lib/bootstrap_forms/form_builder.rb', line 202

def submit(name = nil, args = {})
  name, args = nil, name if name.is_a?(Hash)
  @name = name
  @field_options = field_options(args)
  @args = args

  @field_options[:class] ||= 'btn btn-primary'
  super(name, args.merge(@field_options))
end

#uneditable_input(name, args = {}) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/bootstrap_forms/form_builder.rb', line 173

def uneditable_input(name, args = {})
  @name = name
  @field_options = field_options(args)
  @args = args

  control_group_div do
    label_field + input_div do
      extras do
        value = @field_options.delete(:value)
        @field_options[:class] = [@field_options[:class], 'uneditable-input'].compact

        (:span, @field_options) do
          value || object.send(@name.to_sym) rescue nil
        end
      end
    end
  end
end