Class: Padrino::Helpers::FormBuilder::AbstractFormBuilder

Inherits:
Object
  • Object
show all
Includes:
DeprecatedBuilderMethods
Defined in:
lib/padrino-helpers/form_builder/abstract_form_builder.rb

Overview

Base class for Padrino Form Builder

Direct Known Subclasses

StandardFormBuilder

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DeprecatedBuilderMethods

#field_error, #field_result, #merge_default_options!, #nested_form?, #object_class, #object_model_name, #result_options, #root_form?, #values_matches_field?

Constructor Details

#initialize(template, object, options = {}) ⇒ AbstractFormBuilder

Returns a new instance of AbstractFormBuilder.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 13

def initialize(template, object, options={})
  @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 = options
  @namespace = options[:namespace]
  @model_name = options[:as] || @object.class.to_s.underscore.tr('/', '_')
  nested = options[: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_nameObject (readonly)

Returns the value of attribute attributes_name.



9
10
11
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 9

def attributes_name
  @attributes_name
end

#is_nestedObject (readonly)

Returns the value of attribute is_nested.



9
10
11
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 9

def is_nested
  @is_nested
end

#model_nameObject (readonly)

Returns the value of attribute model_name.



9
10
11
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 9

def model_name
  @model_name
end

#multipartObject

Returns the value of attribute multipart.



8
9
10
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 8

def multipart
  @multipart
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



9
10
11
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 9

def namespace
  @namespace
end

#nested_indexObject (readonly)

Returns the value of attribute nested_index.



9
10
11
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 9

def nested_index
  @nested_index
end

#objectObject

Returns the value of attribute object.



8
9
10
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 8

def object
  @object
end

#parent_formObject (readonly)

Returns the value of attribute parent_form.



9
10
11
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 9

def parent_form
  @parent_form
end

#templateObject

Returns the value of attribute template.



8
9
10
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 8

def template
  @template
end

Class Method Details

.field_typesObject (protected)

Returns the known field types for a Formbuilder.



153
154
155
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 153

def self.field_types
  [:hidden_field, :text_field, :text_area, :password_field, :file_field, :radio_button, :check_box, :select]
end

Instance Method Details

#build_object(symbol) ⇒ Object (protected)

Returns a record from template instance or create a record of specified class.



206
207
208
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 206

def build_object(symbol)
  @template.instance_variable_get("@#{symbol}") || symbol.to_s.camelize.constantize.new
end

#check_box(field, options = {}) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 98

def check_box(field, options={})
  options = default_options(field, options, :value => '1')
  options[:checked] = true if is_checked?(field, options)
  name = field_name(field)
  html = @template.hidden_field_tag(name, :value => options.delete(:uncheck_value) || '0')
  html << @template.check_box_tag(name, options)
end

#check_box_group(field, options = {}) ⇒ Object



86
87
88
89
90
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 86

def check_box_group(field, options={})
  labeled_group(field, options) do |attributes|
    @template.check_box_tag(field_name(field)+'[]', attributes)
  end
end

#csrf_token_fieldObject



146
147
148
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 146

def csrf_token_field
  @template.csrf_token_field
end

#email_field(field, options = {}) ⇒ Object



62
63
64
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 62

def email_field(field, options={})
  @template.email_field_tag field_name(field), default_options(field, options)
end

#error_message_on(field, options = {}) ⇒ Object



33
34
35
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 33

def error_message_on(field, options={})
  @template.error_message_on object, field, options
end

#error_messages(*params) ⇒ Object



29
30
31
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 29

def error_messages(*params)
  @template.error_messages_for object, *params
end

#field_human_name(field) ⇒ Object (protected)

Returns the human name of the field. Look that use builtin I18n.



160
161
162
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 160

def field_human_name(field)
  I18n.translate("#{object_model_name}.attributes.#{field}", :count => 1, :default => field.to_s.humanize, :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”



181
182
183
184
185
186
187
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 181

def field_id(field=nil, value=nil)
  result = (namespace && !is_nested) ? "#{namespace}_" : ''
  result << field_id_fragment
  result << "_#{field}" unless field.blank?
  result << "_#{value}" unless value.blank?
  result
end

#field_name(field = nil) ⇒ Object (protected)

Returns the name for the given field. field_name(:username) => “user” field_name(:number) => “user[number]” field_name(:street) => “user[0]



169
170
171
172
173
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 169

def field_name(field=nil)
  result = field_name_fragment
  result << "[#{field}]" unless field.blank?
  result
end

#field_value(field) ⇒ Object (protected)

Returns the value for the object’s field.



199
200
201
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 199

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



134
135
136
137
138
139
140
141
142
143
144
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 134

def fields_for(child_association, collection=nil, options={}, &block)
  default_collection = self.object.send(child_association)
  collection ||= default_collection
  include_index = default_collection.respond_to?(:each)

  nested_options = { :parent => self, :association => child_association }
  Array(collection).each_with_index.inject(ActiveSupport::SafeBuffer.new) do |all,(child_instance,index)|
    nested_options[:index] = options[:index] || (include_index ? index : nil)
    all << @template.fields_for(child_instance,  { :nested => nested_options, :builder => self.class }, &block) << "\n"
  end
end

#file_field(field, options = {}) ⇒ Object



113
114
115
116
117
118
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 113

def file_field(field, options={})
  self.multipart = true
  defaults = default_options(field, options)
  defaults.delete(:value)
  @template.file_field_tag field_name(field), defaults
end

#hidden_field(field, options = {}) ⇒ Object



45
46
47
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 45

def hidden_field(field, options={})
  @template.hidden_field_tag field_name(field), default_options(field, options)
end

#image_submit(source, options = {}) ⇒ Object



124
125
126
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 124

def image_submit(source, options={})
  @template.image_submit_tag source, options
end

#label(field, options = {}, &block) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 37

def label(field, options={}, &block)
  options[:id] ||= nil
  options[:caption] ||= I18n.t("#{model_name}.attributes.#{field}", :count => 1, :default => field.to_s.humanize, :scope => :models) + ': '
  defaults = default_options(field, options)
  defaults.delete(:value)
  @template.label_tag(field_id(field), defaults, &block)
end

#labeled_group(field, options = {}) ⇒ Object (protected)

Builds a group of labels for radios or checkboxes.



213
214
215
216
217
218
219
220
221
222
223
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 213

def labeled_group(field, options={})
  options = { :id => field_id(field), :selected => field_value(field) }.update(options)
  options.update(error_class(field)){ |_,*values| values.compact.join(' ') }
  selected_values = resolve_checked_values(field, options)
  variants_for_group(options).inject(ActiveSupport::SafeBuffer.new) do |html, (caption,value)|
    variant_id = "#{options[:id]}_#{value}"
    attributes = { :value => value, :id => variant_id, :checked => selected_values.include?(value) }
    caption = yield(attributes) << ' ' << caption
    html << @template.label_tag("#{field_name(field)}[]", :for => variant_id, :caption => caption)
  end
end

#nested_object_idObject (protected)

Returns the child object if it exists.



192
193
194
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 192

def nested_object_id
  is_nested && object.respond_to?(:new_record?) && !object.new_record? && object.id
end

#number_field(field, options = {}) ⇒ Object



53
54
55
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 53

def number_field(field, options={})
  @template.number_field_tag field_name(field), default_options(field, options)
end

#password_field(field, options = {}) ⇒ Object



78
79
80
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 78

def password_field(field, options={})
  @template.password_field_tag field_name(field), default_options(field, options)
end

#radio_button(field, options = {}) ⇒ Object



106
107
108
109
110
111
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 106

def radio_button(field, options={})
  options = default_options(field, options)
  options[:checked] = true if is_checked?(field, options)
  options[:id] = field_id(field, options[:value])
  @template.radio_button_tag field_name(field), options
end

#radio_button_group(field, options = {}) ⇒ Object



92
93
94
95
96
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 92

def radio_button_group(field, options={})
  labeled_group(field, options) do |attributes|
    @template.radio_button_tag(field_name(field), attributes)
  end
end

#search_field(field, options = {}) ⇒ Object



66
67
68
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 66

def search_field(field, options={})
  @template.search_field_tag field_name(field), default_options(field, options)
end

#select(field, options = {}) ⇒ Object



82
83
84
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 82

def select(field, options={})
  @template.select_tag field_name(field), default_options(field, options)
end

#submit(*args) ⇒ Object



120
121
122
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 120

def submit(*args)
  @template.submit_tag *args
end

#telephone_field(field, options = {}) ⇒ Object Also known as: phone_field



57
58
59
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 57

def telephone_field(field, options={})
  @template.telephone_field_tag field_name(field), default_options(field, options)
end

#text_area(field, options = {}) ⇒ Object



74
75
76
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 74

def text_area(field, options={})
  @template.text_area_tag field_name(field), default_options(field, options)
end

#text_field(field, options = {}) ⇒ Object



49
50
51
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 49

def text_field(field, options={})
  @template.text_field_tag field_name(field), default_options(field, options)
end

#url_field(field, options = {}) ⇒ Object



70
71
72
# File 'lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 70

def url_field(field, options={})
  @template.url_field_tag field_name(field), default_options(field, options)
end