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



156
157
158
159
160
161
162
163
164
# File 'lib/bootstrap_forms/form_builder.rb', line 156

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



132
133
134
135
136
137
138
139
# File 'lib/bootstrap_forms/form_builder.rb', line 132

def button(name = nil, *args)
  @name = name
  @field_options = field_options(args)
  @args = args

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

#cancel(*args) ⇒ Object



150
151
152
153
154
# File 'lib/bootstrap_forms/form_builder.rb', line 150

def cancel(*args)
  @field_options = field_options(args)
  @field_options[:class] ||= 'btn cancel'
  link_to(I18n.t('bootstrap_forms.buttons.cancel'), (@field_options[:back] || :back), :class => @field_options[:class])
end

#check_box(name, *args) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/bootstrap_forms/form_builder.rb', line 36

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 << @field_options)) }
      else
        label(@name, :class => 'checkbox') do
          extras { super(name, *(@args << @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



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/bootstrap_forms/form_builder.rb', line 69

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



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/bootstrap_forms/form_builder.rb', line 91

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



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

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



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/bootstrap_forms/form_builder.rb', line 55

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
      values.map do |text, value|
        label("#{@name}_#{value}", :class => 'radio') do
          extras { radio_button(name, value, @field_options) + text }
        end
      end.join.html_safe
    end
  end
end

#submit(name = nil, *args) ⇒ Object



141
142
143
144
145
146
147
148
# File 'lib/bootstrap_forms/form_builder.rb', line 141

def submit(name = nil, *args)
  @name = name
  @field_options = field_options(args)
  @args = args

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

#uneditable_input(name, *args) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/bootstrap_forms/form_builder.rb', line 113

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