Class: Formula::FormulaFormBuilder
- Inherits:
-
ActionView::Helpers::FormBuilder
- Object
- ActionView::Helpers::FormBuilder
- Formula::FormulaFormBuilder
- Defined in:
- lib/formula.rb
Instance Method Summary collapse
- #association(method, collection, value, text, options = {}) ⇒ Object
-
#block(method = nil, options = {}, &block) ⇒ Object
Basic container generator for use with blocks.
-
#button(value = nil, options = {}) ⇒ Object
Generate a form button.
-
#formula_fields_for(record_or_name_or_array, *args, &block) ⇒ Object
(also: #fieldsula_for)
Generates a wrapper around fields_form with :builder set to FormulaFormBuilder.
-
#input(method, options = {}) ⇒ Object
Generate a suitable form input for a given method by performing introspection on the type.
Instance Method Details
#association(method, collection, value, text, options = {}) ⇒ Object
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
# File 'lib/formula.rb', line 283 def association(method, collection, value, text, = {}) [:as] ||= :select [:association] ||= {} klass = [::Formula.association_class, [:as]] klass << ::Formula.association_error_class if ::Formula.association_error_class.present? and error?(method) self.block(method, ) do @template.content_tag(::Formula.association_tag, :class => klass) do case [:as] when :select then collection_select :"#{method}_id", collection, value, text, [:association], [:association].delete(:html) || {} end end end end |
#block(method = nil, options = {}, &block) ⇒ Object
Basic container generator for use with blocks.
Options:
-
:hint - specify a hint to be displayed (‘We promise not to spam you.’, etc.)
-
:label - override the default label used (‘Name:’, ‘URL:’, etc.)
-
:error - override the default error used (‘invalid’, ‘incorrect’, etc.)
Usage:
f.block(:name, :label => "Name:", :hint => "Please use your full name.", :container => { :class => 'fill' }) do
...
end
Equivalent:
<div class='block fill'>
<%= f.label(:name, "Name:") %>
...
<div class="hint">Please use your full name.</div>
<div class="error">...</div>
</div>
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/formula.rb', line 134 def block(method = nil, = {}, &block) [:error] ||= error(method) if method components = "".html_safe if method components << self.label(method, [:label]) if [:label] or [:label].nil? and method end components << @template.capture(&block) [:container] ||= {} [:container][:class] = arrayorize([:container][:class]) << ::Formula.block_class << method [:container][:class] << ::Formula.block_error_class if ::Formula.block_error_class.present? and error?(method) components << @template.content_tag(::Formula.hint_tag , [:hint ], :class => ::Formula.hint_class ) if [:hint ] components << @template.content_tag(::Formula.error_tag, [:error], :class => ::Formula.error_class) if [:error] @template.content_tag(::Formula.block_tag, [:container]) do components end end |
#button(value = nil, options = {}) ⇒ Object
Generate a form button.
Options:
-
:container - add custom options to the container
-
:button - add custom options to the button
Usage:
f.(:name)
Equivalent:
<div class="block">
<%= f.submit("Save")
</div>
98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/formula.rb', line 98 def (value = nil, = {}) [:button] ||= {} [:container] ||= {} [:container][:class] = arrayorize([:container][:class]) << ::Formula.block_class @template.content_tag(::Formula.block_tag, [:container]) do submit value, [:button] end end |
#formula_fields_for(record_or_name_or_array, *args, &block) ⇒ Object Also known as: fieldsula_for
Generates a wrapper around fields_form with :builder set to FormulaFormBuilder.
Supports:
-
f.formula_fields_for(@user.company)
-
f.fieldsula_for(@user.company)
Equivalent:
-
f.fields_for(@user.company, :builder => Formula::FormulaFormBuilder))
Usage:
<% f.formula_fields_for(@user.company) do |company_f| %>
<%= company_f.input :url %>
<%= company_f.input :phone %>
<% end %>
434 435 436 437 438 |
# File 'lib/formula.rb', line 434 def formula_fields_for(record_or_name_or_array, *args, &block) = args. [:builder] ||= self.class fields_for(record_or_name_or_array, *(args << ), &block) end |
#input(method, options = {}) ⇒ Object
Generate a suitable form input for a given method by performing introspection on the type.
Options:
-
:as - override the default type used (:url, :email, :phone, :password, :number, :text)
-
:label - override the default label used (‘Name:’, ‘URL:’, etc.)
-
:error - override the default error used (‘invalid’, ‘incorrect’, etc.)
-
:input - add custom options to the input ({ :class => ‘goregous’ }, etc.)
-
:container - add custom options to the container ({ :class => ‘gorgeous’ }, etc.)
Usage:
f.input(:name)
f.input(:email)
f.input(:password_a, :label => "Password", :hint => "It's a secret!", :container => { :class => "half" })
f.input(:password_b, :label => "Password", :hint => "It's a secret!", :container => { :class => "half" })
Equivalent:
<div class="block name">
<%= f.label(:name)
<div class="input string"><%= f.text_field(:name)</div>
<div class="error">...</div>
</div>
<div class="block email">
<%= f.label(:email)
<div class="input string"><%= f.email_field(:email)</div>
<div class="error">...</div>
</div>
<div class="block half password_a">
<div class="input">
<%= f.label(:password_a, "Password")
<%= f.password_field(:password_a)
<div class="hint">It's a secret!</div>
<div class="error">...</div>
</div>
</div>
<div class="block half password_b">
<div class="input">
<%= f.label(:password_b, "Password")
<%= f.password_field(:password_b)
<div class="hint">It's a secret!</div>
<div class="error">...</div>
</div>
</div>
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/formula.rb', line 204 def input(method, = {}) [:as] ||= as(method) [:input] ||= {} return hidden_field method, [:input] if [:as] == :hidden klass = [::Formula.input_class, [:as]] klass << ::Formula.input_error_class if ::Formula.input_error_class.present? and error?(method) self.block(method, ) do @template.content_tag(::Formula.input_tag, :class => klass) do case [:as] when :text then text_area method, ::Formula..merge([:input] || {}) when :file then file_field method, ::Formula..merge([:input] || {}) when :string then text_field method, ::Formula..merge([:input] || {}) when :password then password_field method, ::Formula..merge([:input] || {}) when :url then url_field method, ::Formula..merge([:input] || {}) when :email then email_field method, ::Formula..merge([:input] || {}) when :phone then phone_field method, ::Formula..merge([:input] || {}) when :number then number_field method, ::Formula..merge([:input] || {}) when :boolean then check_box method, ::Formula..merge([:input] || {}) when :country then country_select method, ::Formula..merge([:input] || {}) when :date then date_select method, ::Formula..merge([:input] || {}), [:input].delete(:html) || {} when :time then time_select method, ::Formula..merge([:input] || {}), [:input].delete(:html) || {} when :datetime then datetime_select method, ::Formula..merge([:input] || {}), [:input].delete(:html) || {} when :select then select method, [:choices], ::Formula..merge([:input] || {}), [:input].delete(:html) || {} end end end end |