Class: BootstrapForms::FormBuilder

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

Instance Method Summary collapse

Instance Method Details

#actions(&block) ⇒ Object



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

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



183
184
185
186
187
188
189
190
191
# File 'lib/bootstrap_forms/form_builder.rb', line 183

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



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

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



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/bootstrap_forms/form_builder.rb', line 120

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 + extras do
      (:div, :class => 'controls') do
        options = @field_options.merge(required_attribute)
        records.collect do |record|
          options[:id] = "#{object_name}_#{attribute}_#{record.send(record_id)}"
          checkbox = check_box_tag("#{object_name}[#{attribute}][]", record.send(record_id), [object.send(attribute)].flatten.include?(record.send(record_id)), options)

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

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



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/bootstrap_forms/form_builder.rb', line 142

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 + extras do
      (:div, :class => 'controls') do
        options = @field_options.merge(required_attribute)
        records.collect do |record|
          options[:id] = "#{object_name}_#{attribute}_#{record.send(record_id)}"
          radiobutton = radio_button_tag("#{object_name}[#{attribute}]", record.send(record_id), object.send(attribute) == record.send(record_id), options)

          (:label, :class => ['radio', ('inline' if @field_options[:inline])].compact) do
            radiobutton + (:span, record.send(record_name))
          end
        end.join('').html_safe
      end
    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
# 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
      values.map do |text, value|
        label("#{@name}_#{value}", :class => klasses) do
          extras { radio_button(name, value, @field_options) + text }
        end
      end.join.html_safe
    end
  end
end

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



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

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



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/bootstrap_forms/form_builder.rb', line 164

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