Class: Padrino::Helpers::FormBuilder::AbstractFormBuilder
- Inherits:
-
Object
- Object
- Padrino::Helpers::FormBuilder::AbstractFormBuilder
- Defined in:
- lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_builder/abstract_form_builder.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#object ⇒ Object
Returns the value of attribute object.
-
#template ⇒ Object
Returns the value of attribute template.
Instance Method Summary collapse
-
#check_box(field, options = {}) ⇒ Object
f.check_box :remember_me, :value => ‘true’, :uncheck_value => ‘0’.
-
#check_box_group(field, options = {}) ⇒ Object
f.check_box_group :color, :options => [‘red’, ‘green’, ‘blue’], :selected => [‘red’, ‘blue’] f.check_box_group :color, :collection => @colors, :fields => [:name, :id].
- #csrf_token_field ⇒ Object
- #email_field(field, options = {}) ⇒ Object
-
#error_message_on(field, options = {}) ⇒ Object
f.error_message_on(field).
-
#error_messages(*params) ⇒ Object
f.error_messages.
-
#fields_for(child_association, instance_or_collection = nil, &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.
-
#file_field(field, options = {}) ⇒ Object
f.file_field :photo, :class => ‘avatar’.
-
#hidden_field(field, options = {}) ⇒ Object
f.hidden_field :session_id, :value => “45”.
-
#image_submit(source, options = {}) ⇒ Object
f.image_submit “buttons/submit.png”, :class => ‘large’.
-
#initialize(template, object, options = {}) ⇒ AbstractFormBuilder
constructor
A new instance of AbstractFormBuilder.
-
#label(field, options = {}, &block) ⇒ Object
f.label :username, :caption => “Nickname”.
- #number_field(field, options = {}) ⇒ Object
-
#password_field(field, options = {}) ⇒ Object
f.password_field :password, :id => ‘password’.
-
#radio_button(field, options = {}) ⇒ Object
f.radio_button :gender, :value => ‘male’.
-
#radio_button_group(field, options = {}) ⇒ Object
f.radio_button_group :color, :options => [‘red’, ‘green’] f.radio_button_group :color, :collection => @colors, :fields => [:name, :id], :selected => @colors.first.
- #search_field(field, options = {}) ⇒ Object
-
#select(field, options = {}) ⇒ Object
f.select :color, :options => [‘red’, ‘green’], :include_blank => true f.select :color, :collection => @colors, :fields => [:name, :id].
-
#submit(caption = "Submit", options = {}) ⇒ Object
f.submit “Update”, :class => ‘large’.
- #telephone_field(field, options = {}) ⇒ Object (also: #phone_field)
-
#text_area(field, options = {}) ⇒ Object
f.text_area :summary, :value => “(enter summary)”, :id => ‘summary’.
-
#text_field(field, options = {}) ⇒ Object
f.text_field :username, :value => “(blank)”, :id => ‘username’.
- #url_field(field, options = {}) ⇒ Object
Constructor Details
#initialize(template, object, options = {}) ⇒ AbstractFormBuilder
Returns a new instance of AbstractFormBuilder.
7 8 9 10 11 12 13 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 7 def initialize(template, object, ={}) @template = template @object = build_object(object) @options = raise "FormBuilder template must be initialized!" unless template raise "FormBuilder object must not be a nil value. If there's no object, use a symbol instead! (i.e :user)" unless object end |
Instance Attribute Details
#object ⇒ Object
Returns the value of attribute object.
5 6 7 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 5 def object @object end |
#template ⇒ Object
Returns the value of attribute template.
5 6 7 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 5 def template @template end |
Instance Method Details
#check_box(field, options = {}) ⇒ Object
f.check_box :remember_me, :value => ‘true’, :uncheck_value => ‘0’
124 125 126 127 128 129 130 131 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 124 def check_box(field, ={}) html = ActiveSupport::SafeBuffer.new unchecked_value = .delete(:uncheck_value) || '0' .reverse_merge!(:id => field_id(field), :value => '1') .reverse_merge!(:checked => true) if values_matches_field?(field, [:value]) html << @template.hidden_field_tag([:name] || field_name(field), :value => unchecked_value, :id => nil) html << @template.check_box_tag(field_name(field), ) end |
#check_box_group(field, options = {}) ⇒ Object
f.check_box_group :color, :options => [‘red’, ‘green’, ‘blue’], :selected => [‘red’, ‘blue’] f.check_box_group :color, :collection => @colors, :fields => [:name, :id]
100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 100 def check_box_group(field, ={}) selected_values = Array([:selected] || field_value(field)) if [:collection] fields = [:fields] || [:name, :id] # don't use map!, it will break some orms selected_values = selected_values.map{ |v| (v.respond_to?(fields[0]) ? v.send(fields[1]) : v).to_s } end labeled_group( field, ) do |variant| @template.check_box_tag( field_name(field)+'[]', :value => variant[1], :id => variant[2], :checked => selected_values.include?(variant[1]) ) end end |
#csrf_token_field ⇒ Object
172 173 174 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 172 def csrf_token_field @template.csrf_token_field end |
#email_field(field, options = {}) ⇒ Object
58 59 60 61 62 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 58 def email_field(field, ={}) .reverse_merge!(:value => field_value(field), :id => field_id(field)) .merge!(:class => field_error(field, )) @template.email_field_tag field_name(field), end |
#error_message_on(field, options = {}) ⇒ Object
f.error_message_on(field)
22 23 24 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 22 def (field, ={}) @template.(object, field, ) end |
#error_messages(*params) ⇒ Object
f.error_messages
16 17 18 19 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 16 def (*params) params.unshift object @template.(*params) end |
#fields_for(child_association, instance_or_collection = nil, &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
161 162 163 164 165 166 167 168 169 170 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 161 def fields_for(child_association, instance_or_collection=nil, &block) default_collection = self.object.send(child_association) include_index = default_collection.respond_to?(:each) = { :parent => self, :association => child_association } nested_objects = instance_or_collection ? Array(instance_or_collection) : Array(default_collection) result = nested_objects.each_with_index.map do |child_instance, index| [:index] = include_index ? index : nil @template.fields_for(child_instance, { :nested => }, &block) end.join("\n").html_safe end |
#file_field(field, options = {}) ⇒ Object
f.file_field :photo, :class => ‘avatar’
141 142 143 144 145 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 141 def file_field(field, ={}) .reverse_merge!(:id => field_id(field)) .merge!(:class => field_error(field, )) @template.file_field_tag field_name(field), end |
#hidden_field(field, options = {}) ⇒ Object
f.hidden_field :session_id, :value => “45”
33 34 35 36 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 33 def hidden_field(field, ={}) .reverse_merge!(:value => field_value(field), :id => field_id(field)) @template.hidden_field_tag field_name(field), end |
#image_submit(source, options = {}) ⇒ Object
f.image_submit “buttons/submit.png”, :class => ‘large’
153 154 155 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 153 def image_submit(source, ={}) @template.image_submit_tag source, end |
#label(field, options = {}, &block) ⇒ Object
f.label :username, :caption => “Nickname”
27 28 29 30 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 27 def label(field, ={}, &block) .reverse_merge!(:caption => "#{field_human_name(field)}: ") @template.label_tag(field_id(field), , &block) end |
#number_field(field, options = {}) ⇒ Object
45 46 47 48 49 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 45 def number_field(field, ={}) .reverse_merge!(:value => field_value(field), :id => field_id(field)) .merge!(:class => field_error(field, )) @template.number_field_tag field_name(field), end |
#password_field(field, options = {}) ⇒ Object
f.password_field :password, :id => ‘password’
84 85 86 87 88 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 84 def password_field(field, ={}) .reverse_merge!(:value => field_value(field), :id => field_id(field)) .merge!(:class => field_error(field, )) @template.password_field_tag field_name(field), end |
#radio_button(field, options = {}) ⇒ Object
f.radio_button :gender, :value => ‘male’
134 135 136 137 138 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 134 def (field, ={}) .reverse_merge!(:id => field_id(field, [:value])) .reverse_merge!(:checked => true) if values_matches_field?(field, [:value]) @template. field_name(field), end |
#radio_button_group(field, options = {}) ⇒ Object
f.radio_button_group :color, :options => [‘red’, ‘green’] f.radio_button_group :color, :collection => @colors, :fields => [:name, :id], :selected => @colors.first
114 115 116 117 118 119 120 121 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 114 def (field, ={}) fields = [:fields] || [:name, :id] selected_value = [:selected] || field_value(field) selected_value = selected_value.send(fields[1]) if selected_value.respond_to?(fields[0]) labeled_group( field, ) do |variant| @template.( field_name(field), :value => variant[1], :id => variant[2], :checked => variant[1] == selected_value.to_s ) end end |
#search_field(field, options = {}) ⇒ Object
64 65 66 67 68 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 64 def search_field(field, ={}) .reverse_merge!(:value => field_value(field), :id => field_id(field)) .merge!(:class => field_error(field, )) @template.search_field_tag field_name(field), end |
#select(field, options = {}) ⇒ Object
f.select :color, :options => [‘red’, ‘green’], :include_blank => true f.select :color, :collection => @colors, :fields => [:name, :id]
92 93 94 95 96 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 92 def select(field, ={}) .reverse_merge!(:id => field_id(field), :selected => field_value(field)) .merge!(:class => field_error(field, )) @template.select_tag field_name(field), end |
#submit(caption = "Submit", options = {}) ⇒ Object
f.submit “Update”, :class => ‘large’
148 149 150 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 148 def submit(="Submit", ={}) @template.submit_tag , end |
#telephone_field(field, options = {}) ⇒ Object Also known as: phone_field
51 52 53 54 55 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 51 def telephone_field(field, ={}) .reverse_merge!(:value => field_value(field), :id => field_id(field)) .merge!(:class => field_error(field, )) @template.telephone_field_tag field_name(field), end |
#text_area(field, options = {}) ⇒ Object
f.text_area :summary, :value => “(enter summary)”, :id => ‘summary’
77 78 79 80 81 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 77 def text_area(field, ={}) .reverse_merge!(:value => field_value(field), :id => field_id(field)) .merge!(:class => field_error(field, )) @template.text_area_tag field_name(field), end |
#text_field(field, options = {}) ⇒ Object
f.text_field :username, :value => “(blank)”, :id => ‘username’
39 40 41 42 43 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 39 def text_field(field, ={}) .reverse_merge!(:value => field_value(field), :id => field_id(field)) .merge!(:class => field_error(field, )) @template.text_field_tag field_name(field), end |
#url_field(field, options = {}) ⇒ Object
70 71 72 73 74 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_builder/abstract_form_builder.rb', line 70 def url_field(field, ={}) .reverse_merge!(:value => field_value(field), :id => field_id(field)) .merge!(:class => field_error(field, )) @template.url_field_tag field_name(field), end |