Class: SimpleForm::FormBuilder
- Inherits:
-
ActionView::Helpers::FormBuilder
- Object
- ActionView::Helpers::FormBuilder
- SimpleForm::FormBuilder
- Extended by:
- MapType
- Includes:
- Inputs
- Defined in:
- lib/simple_form/form_builder.rb
Constant Summary
- ACTIONS =
When action is create or update, we still should use new and edit
{ create: :new, update: :edit }
Instance Attribute Summary (collapse)
-
- (Object) object
readonly
Returns the value of attribute object.
-
- (Object) object_name
readonly
Returns the value of attribute object_name.
-
- (Object) template
readonly
Returns the value of attribute template.
-
- (Object) wrapper
readonly
Returns the value of attribute wrapper.
Class Method Summary (collapse)
Instance Method Summary (collapse)
-
- (Object) association(association, options = {}, &block)
Helper for dealing with association selects/radios, generating the collection automatically.
- - (Object) button(type, *args, &block)
-
- (Object) button_button
Creates a button:.
-
- (Object) collection_check_boxes(method, collection, value_method, text_method, options = {}, html_options = {}, &block)
Creates a collection of check boxes for each item in the collection, associated with a clickable label.
-
- (Object) collection_radio_buttons(method, collection, value_method, text_method, options = {}, html_options = {}, &block)
Create a collection of radio inputs for the attribute.
-
- (Object) error(attribute_name, options = {})
Creates an error tag based on the given attribute, only when the attribute contains errors.
-
- (Object) error_notification(options = {})
Creates an error notification message that only appears when the form object has some error.
-
- (Object) full_error(attribute_name, options = {})
Return the error but also considering its name.
-
- (Object) hint(attribute_name, options = {})
Creates a hint tag for the given attribute.
-
- (FormBuilder) initialize
constructor
:nodoc:.
-
- (Object) input(attribute_name, options = {}, &block)
(also: #attribute)
Basic input helper, combines all components in the stack to generate input html based on options the user define and some guesses through database column information.
-
- (Object) input_field(attribute_name, options = {})
Creates a input tag for the given attribute.
-
- (Object) label(attribute_name, *args)
Creates a default label tag for the given attribute.
-
- (Object) lookup_action
The action to be used in lookup.
-
- (Object) lookup_model_names
Extract the model names from the object_name mess, ignoring numeric and explicit child indexes.
Methods included from MapType
Constructor Details
- (FormBuilder) initialize
:nodoc:
36 37 38 39 40 |
# File 'lib/simple_form/form_builder.rb', line 36 def initialize(*) #:nodoc: super @defaults = [:defaults] @wrapper = SimpleForm.wrapper([:wrapper] || SimpleForm.default_wrapper) end |
Instance Attribute Details
- (Object) object (readonly)
Returns the value of attribute object
7 8 9 |
# File 'lib/simple_form/form_builder.rb', line 7 def object @object end |
- (Object) object_name (readonly)
Returns the value of attribute object_name
7 8 9 |
# File 'lib/simple_form/form_builder.rb', line 7 def object_name @object_name end |
- (Object) template (readonly)
Returns the value of attribute template
7 8 9 |
# File 'lib/simple_form/form_builder.rb', line 7 def template @template end |
- (Object) wrapper (readonly)
Returns the value of attribute wrapper
7 8 9 |
# File 'lib/simple_form/form_builder.rb', line 7 def wrapper @wrapper end |
Class Method Details
+ (Object) discovery_cache
32 33 34 |
# File 'lib/simple_form/form_builder.rb', line 32 def self.discovery_cache @discovery_cache ||= {} end |
Instance Method Details
- (Object) association(association, options = {}, &block)
Helper for dealing with association selects/radios, generating the collection automatically. It's just a wrapper to input, so all options supported in input are also supported by association. Some extra options can also be given:
Examples
simple_form_for @user do |f|
f.association :company # Company.all
end
f.association :company, collection: Company.all(order: 'name')
# Same as using :order option, but overriding collection
Block
When a block is given, association simple behaves as a proxy to simple_fields_for:
f.association :company do |c|
c.input :name
c.input :type
end
From the options above, only :collection can also be supplied.
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/simple_form/form_builder.rb', line 170 def association(association, ={}, &block) = .dup return simple_fields_for(*[association, .delete(:collection), ].compact, &block) if block_given? raise ArgumentError, "Association cannot be used in forms not associated with an object" unless @object reflection = find_association_reflection(association) raise "Association #{association.inspect} not found" unless reflection [:as] ||= :select [:collection] ||= .fetch(:collection) { reflection.klass.all(reflection..slice(:conditions, :order)) } attribute = case reflection.macro when :belongs_to (reflection.respond_to?(:options) && reflection.[:foreign_key]) || :#{reflection.name}_id" when :has_one raise ArgumentError, ":has_one associations are not supported by f.association" else if [:as] == :select = [:input_html] ||= {} [:multiple] = true unless .key?(:multiple) end # Force the association to be preloaded for performance. if [:preload] != false && object.respond_to?(association) target = object.send(association) target.to_a if target.respond_to?(:to_a) end :#{reflection.name.to_s.singularize}_ids" end input(attribute, .merge(reflection: reflection)) end |
- (Object) button(type, *args, &block)
220 221 222 223 224 225 226 227 228 229 |
# File 'lib/simple_form/form_builder.rb', line 220 def (type, *args, &block) = args..dup [:class] = [SimpleForm., [:class]].compact args << if respond_to?("#{type}_button") send("#{type}_button", *args, &block) else send(type, *args, &block) end end |
- (Object) button_button
Creates a button:
form_for @user do |f|
f. :submit
end
It just acts as a proxy to method name given. We also alias original Rails button implementation (3.2 forward (to delegate to the original when calling `f.button :button`.
219 |
# File 'lib/simple_form/form_builder.rb', line 219 alias_method :button_button, :button |
- (Object) collection_check_boxes(method, collection, value_method, text_method, options = {}, html_options = {}, &block)
Creates a collection of check boxes for each item in the collection, associated with a clickable label. Use value_method and text_method to convert items in the collection for use as text/value in check boxes. You can give a symbol or a proc to both value_method and text_method, that will be evaluated for each item in the collection.
Examples
form_for @user do |f|
f.collection_check_boxes :options, [[true, 'Yes'] ,[false, 'No']], :first, :last
end
<input name="user[options][]" type="hidden" value="" />
<input id="user_options_true" name="user[options][]" type="checkbox" value="true" />
<label class="collection_check_boxes" for="user_options_true">Yes</label>
<input name="user[options][]" type="hidden" value="" />
<input id="user_options_false" name="user[options][]" type="checkbox" value="false" />
<label class="collection_check_boxes" for="user_options_false">No</label>
It is also possible to give a block that should generate the check box + label. To wrap the check box with the label, for instance:
form_for @user do |f|
f.collection_check_boxes(
:options, [[true, 'Yes'] ,[false, 'No']], :first, :last
) do |b|
b.label { b.check_box + b.text }
end
end
Options
Collection check box accepts some extra options:
* checked => the value or values that should be checked initially. Accepts
a single item or an array of items. It overrides existing associations.
* disabled => the value or values that should be disabled. Accepts a single
item or an array of items.
* collection_wrapper_tag => the tag to wrap the entire collection.
* collection_wrapper_class => the CSS class to use for collection_wrapper_tag. This option
is ignored if the :collection_wrapper_tag option is blank.
* item_wrapper_tag => the tag to wrap each item in the collection.
* item_wrapper_class => the CSS class to use for item_wrapper_tag
* a block => to generate the label + check box or any other component.
434 435 436 |
# File 'lib/simple_form/form_builder.rb', line 434 def collection_check_boxes(method, collection, value_method, text_method, = {}, = {}, &block) SimpleForm::Tags::CollectionCheckBoxes.new(@object_name, method, @template, collection, value_method, text_method, (), @default_options.merge()).render(&block) end |
- (Object) collection_radio_buttons(method, collection, value_method, text_method, options = {}, html_options = {}, &block)
Create a collection of radio inputs for the attribute. Basically this helper will create a radio input associated with a label for each text/value option in the collection, using value_method and text_method to convert these text/value. You can give a symbol or a proc to both value_method and text_method, that will be evaluated for each item in the collection.
Examples
form_for @user do |f|
f.collection_radio_buttons :options, [[true, 'Yes'] ,[false, 'No']], :first, :last
end
<input id="user_options_true" name="user[options]" type="radio" value="true" />
<label class="collection_radio_buttons" for="user_options_true">Yes</label>
<input id="user_options_false" name="user[options]" type="radio" value="false" />
<label class="collection_radio_buttons" for="user_options_false">No</label>
It is also possible to give a block that should generate the radio + label. To wrap the radio with the label, for instance:
form_for @user do |f|
f.(
:options, [[true, 'Yes'] ,[false, 'No']], :first, :last
) do |b|
b.label { b. + b.text }
end
end
Options
Collection radio accepts some extra options:
* checked => the value that should be checked initially.
* disabled => the value or values that should be disabled. Accepts a single
item or an array of items.
* collection_wrapper_tag => the tag to wrap the entire collection.
* collection_wrapper_class => the CSS class to use for collection_wrapper_tag
* item_wrapper_tag => the tag to wrap each item in the collection.
* item_wrapper_class => the CSS class to use for item_wrapper_tag
* a block => to generate the label + radio or any other component.
380 381 382 |
# File 'lib/simple_form/form_builder.rb', line 380 def (method, collection, value_method, text_method, = {}, = {}, &block) SimpleForm::Tags::CollectionRadioButtons.new(@object_name, method, @template, collection, value_method, text_method, (), @default_options.merge()).render(&block) end |
- (Object) error(attribute_name, options = {})
Creates an error tag based on the given attribute, only when the attribute contains errors. All the given options are sent as :error_html.
Examples
f.error :name
f.error :name, id: "cool_error"
239 240 241 242 243 244 245 246 247 |
# File 'lib/simple_form/form_builder.rb', line 239 def error(attribute_name, ={}) = .dup [:error_html] = .except(:error_tag, :error_prefix, :error_method) column = find_attribute_column(attribute_name) input_type = default_input_type(attribute_name, column, ) wrapper.find(:error). render(SimpleForm::Inputs::Base.new(self, attribute_name, column, input_type, )) end |
- (Object) error_notification(options = {})
Creates an error notification message that only appears when the form object has some error. You can give a specific message with the :message option, otherwise it will look for a message using I18n. All other options given are passed straight as html options to the html tag.
Examples
f.error_notification
f.error_notification message: 'Something went wrong'
f.error_notification id: 'user_error_message', class: 'form_error'
329 330 331 |
# File 'lib/simple_form/form_builder.rb', line 329 def error_notification(={}) SimpleForm::ErrorNotification.new(self, ).render end |
- (Object) full_error(attribute_name, options = {})
Return the error but also considering its name. This is used when errors for a hidden field need to be shown.
Examples
f.full_error :token #=> <span class="error">Token is invalid</span>
256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/simple_form/form_builder.rb', line 256 def full_error(attribute_name, ={}) = .dup [:error_prefix] ||= if object.class.respond_to?(:human_attribute_name) object.class.human_attribute_name(attribute_name.to_s) else attribute_name.to_s.humanize end error(attribute_name, ) end |
- (Object) hint(attribute_name, options = {})
Creates a hint tag for the given attribute. Accepts a symbol indicating an attribute for I18n lookup or a string. All the given options are sent as :hint_html.
Examples
f.hint :name # Do I18n lookup
f.hint :name, id: "cool_hint"
f.hint "Don't forget to accept this"
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/simple_form/form_builder.rb', line 278 def hint(attribute_name, ={}) = .dup [:hint_html] = .except(:hint_tag, :hint) if attribute_name.is_a?(String) [:hint] = attribute_name attribute_name, column, input_type = nil, nil, nil else column = find_attribute_column(attribute_name) input_type = default_input_type(attribute_name, column, ) end wrapper.find(:hint). render(SimpleForm::Inputs::Base.new(self, attribute_name, column, input_type, )) end |
- (Object) input(attribute_name, options = {}, &block) Also known as: attribute
Basic input helper, combines all components in the stack to generate input html based on options the user define and some guesses through database column information. By default a call to input will generate label + input + hint (when defined) + errors (when exists), and all can be configured inside a wrapper html.
Examples
# Imagine @user has error "can't be blank" on name
simple_form_for @user do |f|
f.input :name, hint: 'My hint'
end
This is the output html (only the input portion, not the form):
<label class="string required" for="user_name">
<abbr title="required">*</abbr> Super User Name!
</label>
<input class="string required" id="user_name" maxlength="100"
name="user[name]" type="text" value="Carlos" />
<span class="hint">My hint</span>
<span class="error">can't be blank</span>
Each database type will render a default input, based on some mappings and heuristic to determine which is the best option.
You have some options for the input to enable/disable some functions:
as: allows you to define the input type you want, for instance you
can use it to generate a text field for a date column.
required: defines whether this attribute is required or not. True
by default.
The fact SimpleForm is built in components allow the interface to be unified. So, for instance, if you need to disable :hint for a given input, you can pass hint: false. The same works for :error, :label and :wrapper.
Besides the html for any component can be changed. So, if you want to change the label html you just need to give a hash to :label_html. To configure the input html, supply :input_html instead and so on.
Options
Some inputs, as datetime, time and select allow you to give extra options, like prompt and/or include blank. Such options are given in plainly:
f.input :created_at, include_blank: true
Collection
When playing with collections (:radio_buttons, :check_boxes and :select inputs), you have three extra options:
collection: use to determine the collection to generate the radio or select
label_method: the method to apply on the array collection to get the label
value_method: the method to apply on the array collection to get the value
Priority
Some inputs, as :time_zone and :country accepts a :priority option. If none is given SimpleForm.time_zone_priority and SimpleForm.country_priority are used respectively.
107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/simple_form/form_builder.rb', line 107 def input(attribute_name, ={}, &block) = @defaults.deep_dup.deep_merge() if @defaults input = find_input(attribute_name, , &block) chosen = if name = [:wrapper] || find_wrapper_mapping(input.input_type) name.respond_to?(:render) ? name : SimpleForm.wrapper(name) else wrapper end chosen.render input end |
- (Object) input_field(attribute_name, options = {})
Creates a input tag for the given attribute. All the given options are sent as :input_html.
Examples
simple_form_for @user do |f|
f.input_field :name
end
This is the output html (only the input portion, not the form):
<input class="string required" id="user_name" maxlength="100"
name="user[name]" type="text" value="Carlos" />
136 137 138 139 140 141 142 |
# File 'lib/simple_form/form_builder.rb', line 136 def input_field(attribute_name, ={}) = .dup [:input_html] = .except(:as, :collection, :label_method, :value_method) = @defaults.deep_dup.deep_merge() if @defaults SimpleForm::Wrappers::Root.new([:min_max, :maxlength, :placeholder, :pattern, :readonly, :input], wrapper: false).render find_input(attribute_name, ) end |
- (Object) label(attribute_name, *args)
Creates a default label tag for the given attribute. You can give a label through the :label option or using i18n. All the given options are sent as :label_html.
Examples
f.label :name # Do I18n lookup
f.label :name, "Name" # Same behavior as Rails, do not add required tag
f.label :name, label: "Name" # Same as above, but adds required tag
f.label :name, required: false
f.label :name, id: "cool_label"
307 308 309 310 311 312 313 314 315 316 |
# File 'lib/simple_form/form_builder.rb', line 307 def label(attribute_name, *args) return super if args.first.is_a?(String) || block_given? = args..dup [:label_html] = .except(:label, :required, :as) column = find_attribute_column(attribute_name) input_type = default_input_type(attribute_name, column, ) SimpleForm::Inputs::Base.new(self, attribute_name, column, input_type, ).label end |
- (Object) lookup_action
The action to be used in lookup.
457 458 459 460 461 462 463 464 |
# File 'lib/simple_form/form_builder.rb', line 457 def lookup_action @lookup_action ||= begin action = template.controller.action_name return unless action action = action.to_sym ACTIONS[action] || action end end |
- (Object) lookup_model_names
446 447 448 449 450 451 452 453 454 |
# File 'lib/simple_form/form_builder.rb', line 446 def lookup_model_names @lookup_model_names ||= begin child_index = [:child_index] names = object_name.to_s.scan(/([a-zA-Z_]+)/).flatten names.delete(child_index) if child_index names.each { |name| name.gsub!('_attributes', '') } names.freeze end end |