Class: Padrino::Helpers::FormBuilder::AbstractFormBuilder
- Defined in:
- lib/padrino-helpers/form_builder/abstract_form_builder.rb
Overview
Base class for Padrino Form Builder
Direct Known Subclasses
Instance Attribute Summary collapse
-
#attributes_name ⇒ Object
readonly
Returns the value of attribute attributes_name.
-
#is_nested ⇒ Object
readonly
Returns the value of attribute is_nested.
-
#model_name ⇒ Object
readonly
Returns the value of attribute model_name.
-
#multipart ⇒ Object
Returns the value of attribute multipart.
-
#namespace ⇒ Object
readonly
Returns the value of attribute namespace.
-
#nested_index ⇒ Object
readonly
Returns the value of attribute nested_index.
-
#object ⇒ Object
Returns the value of attribute object.
-
#parent_form ⇒ Object
readonly
Returns the value of attribute parent_form.
-
#template ⇒ Object
Returns the value of attribute template.
Class Method Summary collapse
-
.field_types ⇒ Object
protected
Returns the known field types for a Formbuilder.
Instance Method Summary collapse
-
#build_object(symbol) ⇒ Object
protected
Returns a record from template instance or create a record of specified class.
- #check_box(field, options = {}) ⇒ Object
- #check_box_group(field, options = {}) ⇒ Object
- #color_field(field, options = {}) ⇒ Object
- #csrf_token_field ⇒ Object
- #date_field(field, options = {}) ⇒ Object
- #datetime_field(field, options = {}) ⇒ Object
- #datetime_local_field(field, options = {}) ⇒ Object
- #email_field(field, options = {}) ⇒ Object
- #error_message_on(field, options = {}) ⇒ Object
- #error_messages(*params) ⇒ Object
-
#field_human_name(field) ⇒ Object
protected
Returns the human name of the field.
-
#field_id(field = nil, value = nil) ⇒ Object
protected
Returns the id for the given field.
-
#field_name(field = nil) ⇒ Object
protected
Returns the name for the given field.
-
#field_value(field) ⇒ Object
protected
Returns the value for the object’s field.
-
#fields_for(child_association, collection = nil, options = {}, &block) ⇒ Object
Supports nested fields for a child model within a form.
- #file_field(field, options = {}) ⇒ Object
- #hidden_field(field, options = {}) ⇒ Object
- #image_submit(source, options = {}) ⇒ Object
-
#initialize(template, object, options = {}) ⇒ AbstractFormBuilder
constructor
A new instance of AbstractFormBuilder.
- #label(field, options = {}, &block) ⇒ Object
-
#labeled_group(field, options = {}) ⇒ Object
protected
Builds a group of labels for radios or checkboxes.
- #month_field(field, options = {}) ⇒ Object
-
#nested_object_id ⇒ Object
protected
Returns the child object if it exists.
- #number_field(field, options = {}) ⇒ Object
-
#object_model_name(explicit_object = object) ⇒ Object
protected
Returns the object’s models name.
- #password_field(field, options = {}) ⇒ Object
- #radio_button(field, options = {}) ⇒ Object
- #radio_button_group(field, options = {}) ⇒ Object
- #search_field(field, options = {}) ⇒ Object
- #select(field, options = {}) ⇒ Object
- #submit(*args) ⇒ Object
- #telephone_field(field, options = {}) ⇒ Object (also: #phone_field)
- #text_area(field, options = {}) ⇒ Object
- #text_field(field, options = {}) ⇒ Object
- #time_field(field, options = {}) ⇒ Object
- #url_field(field, options = {}) ⇒ Object
- #week_field(field, options = {}) ⇒ Object
Constructor Details
#initialize(template, object, options = {}) ⇒ AbstractFormBuilder
Returns a new instance of AbstractFormBuilder.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 9 def initialize(template, object, ={}) @template = template fail "FormBuilder template must be initialized" unless template @object = object.kind_of?(Symbol) ? build_object(object) : object fail "FormBuilder object must be present. If there's no object, use a symbol instead (i.e. :user)" unless object @options = @namespace = [:namespace] @model_name = [:as] || Inflections.underscore(@object.class).tr('/', '_') nested = [:nested] if @is_nested = nested && (nested_parent = nested[:parent]) && nested_parent.respond_to?(:object) @parent_form = nested_parent @nested_index = nested[:index] @attributes_name = "#{nested[:association]}_attributes" end end |
Instance Attribute Details
#attributes_name ⇒ Object (readonly)
Returns the value of attribute attributes_name.
7 8 9 |
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 7 def attributes_name @attributes_name end |
#is_nested ⇒ Object (readonly)
Returns the value of attribute is_nested.
7 8 9 |
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 7 def is_nested @is_nested end |
#model_name ⇒ Object (readonly)
Returns the value of attribute model_name.
7 8 9 |
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 7 def model_name @model_name end |
#multipart ⇒ Object
Returns the value of attribute multipart.
6 7 8 |
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 6 def multipart @multipart end |
#namespace ⇒ Object (readonly)
Returns the value of attribute namespace.
7 8 9 |
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 7 def namespace @namespace end |
#nested_index ⇒ Object (readonly)
Returns the value of attribute nested_index.
7 8 9 |
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 7 def nested_index @nested_index end |
#object ⇒ Object
Returns the value of attribute object.
6 7 8 |
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 6 def object @object end |
#parent_form ⇒ Object (readonly)
Returns the value of attribute parent_form.
7 8 9 |
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 7 def parent_form @parent_form end |
#template ⇒ Object
Returns the value of attribute template.
6 7 8 |
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 6 def template @template end |
Class Method Details
.field_types ⇒ Object (protected)
Returns the known field types for a Formbuilder.
171 172 173 174 175 176 |
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 171 def self.field_types [:hidden_field, :text_field, :text_area, :password_field, :file_field, :radio_button, :check_box, :select, :number_field, :telephone_field, :email_field, :search_field, :url_field, :datetime_field, :datetime_local_field, :date_field, :month_field, :week_field, :time_field, :color_field, ] end |
Instance Method Details
#build_object(symbol) ⇒ Object (protected)
Returns a record from template instance or create a record of specified class.
234 235 236 |
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 234 def build_object(symbol) @template.instance_variable_get("@#{symbol}") || Inflections.constantize(Inflections.camelize(symbol)).new end |
#check_box(field, options = {}) ⇒ Object
90 91 92 93 94 95 96 |
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 90 def check_box(field, ={}) = (field, , :value => '1') [:checked] = true if is_checked?(field, ) name = field_name(field) html = @template.hidden_field_tag(name, :value => .delete(:uncheck_value) || '0') html << @template.check_box_tag(name, ) end |
#check_box_group(field, options = {}) ⇒ Object
78 79 80 81 82 |
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 78 def check_box_group(field, ={}) labeled_group(field, ) do |attributes| @template.check_box_tag(field_name(field)+'[]', attributes) end end |
#color_field(field, options = {}) ⇒ Object
142 143 144 |
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 142 def color_field(field, ={}) @template.color_field_tag field_name(field), (field, ) end |
#csrf_token_field ⇒ Object
164 165 166 |
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 164 def csrf_token_field @template.csrf_token_field end |
#date_field(field, options = {}) ⇒ Object
126 127 128 |
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 126 def date_field(field, ={}) @template.date_field_tag field_name(field), (field, ) end |
#datetime_field(field, options = {}) ⇒ Object
118 119 120 |
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 118 def datetime_field(field, ={}) @template.datetime_field_tag field_name(field), (field, ) end |
#datetime_local_field(field, options = {}) ⇒ Object
122 123 124 |
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 122 def datetime_local_field(field, ={}) @template.datetime_local_field_tag field_name(field), (field, ) end |
#email_field(field, options = {}) ⇒ Object
54 55 56 |
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 54 def email_field(field, ={}) @template.email_field_tag field_name(field), (field, ) end |
#error_message_on(field, options = {}) ⇒ Object
29 30 31 |
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 29 def (field, ={}) @template. object, field, end |
#error_messages(*params) ⇒ Object
25 26 27 |
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 25 def (*params) @template. object, *params end |
#field_human_name(field) ⇒ Object (protected)
Returns the human name of the field. Look that use builtin I18n.
181 182 183 |
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 181 def field_human_name(field) I18n.translate("#{model_name}.attributes.#{field}", :count => 1, :default => Inflections.humanize(field), :scope => :models) end |
#field_id(field = nil, value = nil) ⇒ Object (protected)
Returns the id for the given field. field_id(:username) => “user_username” field_id(:gender, :male) => “user_gender_male” field_name(:number) => “user_telephone_attributes_number” field_name(:street) => “user_addresses_attributes_0_street”
209 210 211 212 213 214 215 |
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 209 def field_id(field=nil, value=nil) result = (namespace && !is_nested) ? "#{namespace}_" : '' result << field_id_fragment result << "_#{field}" if field result << "_#{value}" if value result end |
#field_name(field = nil) ⇒ Object (protected)
197 198 199 200 201 |
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 197 def field_name(field=nil) result = field_name_fragment result << "[#{field}]" if field result end |
#field_value(field) ⇒ Object (protected)
Returns the value for the object’s field.
227 228 229 |
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 227 def field_value(field) @object.respond_to?(field) ? @object.send(field) : '' end |
#fields_for(child_association, collection = nil, options = {}, &block) ⇒ Object
Supports nested fields for a child model within a form. f.fields_for :addresses f.fields_for :addresses, address f.fields_for :addresses, @addresses f.fields_for :addresses, address, index: i
152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 152 def fields_for(child_association, collection=nil, ={}, &block) default_collection = self.object.send(child_association) collection ||= default_collection include_index = default_collection.respond_to?(:each) = { :parent => self, :association => child_association } Array(collection).each_with_index.inject(SafeBuffer.new) do |all,(child_instance,index)| [:index] = [:index] || (include_index ? index : nil) all << @template.fields_for(child_instance, { :nested => , :builder => self.class }, &block) << "\n" end end |
#file_field(field, options = {}) ⇒ Object
105 106 107 108 |
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 105 def file_field(field, ={}) self.multipart = true @template.file_field_tag field_name(field), (field, ).reject{ |key, _| key == :value } end |
#hidden_field(field, options = {}) ⇒ Object
37 38 39 |
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 37 def hidden_field(field, ={}) @template.hidden_field_tag field_name(field), (field, ) end |
#image_submit(source, options = {}) ⇒ Object
114 115 116 |
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 114 def image_submit(source, ={}) @template.image_submit_tag source, end |
#label(field, options = {}, &block) ⇒ Object
33 34 35 |
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 33 def label(field, ={}, &block) @template.label_tag(field_id(field), { :caption => "#{field_human_name(field)}: " }.update(), &block) end |
#labeled_group(field, options = {}) ⇒ Object (protected)
Builds a group of labels for radios or checkboxes.
241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 241 def labeled_group(field, ={}) = { :id => field_id(field), :selected => field_value(field) }.update() .update(error_class(field)){ |_,*values| values.compact.join(' ') } selected_values = resolve_checked_values(field, ) variants_for_group().inject(SafeBuffer.new) do |html, (,value)| variant_id = "#{[:id]}_#{value}" attributes = { :value => value, :id => variant_id, :checked => selected_values.include?(value) } = yield(attributes) << ' ' << html << @template.label_tag("#{field_name(field)}[]", :for => variant_id, :caption => ) end end |
#month_field(field, options = {}) ⇒ Object
130 131 132 |
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 130 def month_field(field, ={}) @template.month_field_tag field_name(field), (field, ) end |
#nested_object_id ⇒ Object (protected)
Returns the child object if it exists.
220 221 222 |
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 220 def nested_object_id is_nested && object.respond_to?(:new_record?) && !object.new_record? && object.id end |
#number_field(field, options = {}) ⇒ Object
45 46 47 |
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 45 def number_field(field, ={}) @template.number_field_tag field_name(field), (field, ) end |
#object_model_name(explicit_object = object) ⇒ Object (protected)
Returns the object’s models name.
188 189 190 |
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 188 def object_model_name(explicit_object=object) explicit_object.is_a?(Symbol) ? explicit_object : explicit_object.class.to_s.underscore.gsub(/\//, '_') end |
#password_field(field, options = {}) ⇒ Object
70 71 72 |
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 70 def password_field(field, ={}) @template.password_field_tag field_name(field), (field, ) end |
#radio_button(field, options = {}) ⇒ Object
98 99 100 101 102 103 |
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 98 def (field, ={}) = (field, ) [:checked] = true if is_checked?(field, ) [:id] = field_id(field, [:value]) @template. field_name(field), end |
#radio_button_group(field, options = {}) ⇒ Object
84 85 86 87 88 |
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 84 def (field, ={}) labeled_group(field, ) do |attributes| @template.(field_name(field), attributes) end end |
#search_field(field, options = {}) ⇒ Object
58 59 60 |
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 58 def search_field(field, ={}) @template.search_field_tag field_name(field), (field, ) end |
#select(field, options = {}) ⇒ Object
74 75 76 |
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 74 def select(field, ={}) @template.select_tag field_name(field), (field, ) end |
#submit(*args) ⇒ Object
110 111 112 |
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 110 def submit(*args) @template.submit_tag(*args) end |
#telephone_field(field, options = {}) ⇒ Object Also known as: phone_field
49 50 51 |
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 49 def telephone_field(field, ={}) @template.telephone_field_tag field_name(field), (field, ) end |
#text_area(field, options = {}) ⇒ Object
66 67 68 |
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 66 def text_area(field, ={}) @template.text_area_tag field_name(field), (field, ) end |
#text_field(field, options = {}) ⇒ Object
41 42 43 |
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 41 def text_field(field, ={}) @template.text_field_tag field_name(field), (field, ) end |
#time_field(field, options = {}) ⇒ Object
138 139 140 |
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 138 def time_field(field, ={}) @template.time_field_tag field_name(field), (field, ) end |
#url_field(field, options = {}) ⇒ Object
62 63 64 |
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 62 def url_field(field, ={}) @template.url_field_tag field_name(field), (field, ) end |
#week_field(field, options = {}) ⇒ Object
134 135 136 |
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 134 def week_field(field, ={}) @template.week_field_tag field_name(field), (field, ) end |