Class: Formula::FormulaFormBuilder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Defined in:
lib/formula.rb

Instance Method Summary collapse

Instance Method Details

#association(method, collection, value, text, options = {}) ⇒ Object



256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/formula.rb', line 256

def association(method, collection, value, text, options = {})
  options[:as] ||= :select
  options[:association] ||= {}
  
  self.block(method, options) do
    @template.(::Formula.association_tag, :class => [::Formula.association_class, options[:as]]) do
      case options[:as]
        when :select then collection_select :"#{method}_id", collection, value, text, 
          options[:association], options[: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>


112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/formula.rb', line 112

def block(method = nil, options = {}, &block)
  options[:error] ||= error(method) if method
        
  components = "".html_safe
  
  if method
    components << self.label(method, options[:label]) if options[:label] or options[:label].nil? and method
  end
  
  components << @template.capture(&block)
  
  options[:container] ||= {}
  options[:container][:class] = arrayorize(options[:container][:class]) << ::Formula.block_class << method
  
  components << @template.(::Formula.hint_tag , options[:hint ], :class => ::Formula.hint_class ) if options[:hint ]
  components << @template.(::Formula.error_tag, options[:error], :class => ::Formula.error_class) if options[:error]
  
  @template.(::Formula.block_tag, options[: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.button(:name)

Equivalent:

<div class="block">
  <%= f.submit("Save")
</div>


76
77
78
79
80
81
82
83
84
85
86
# File 'lib/formula.rb', line 76

def button(value = nil, options = {})
  options[:button] ||= {}
  
  options[:container] ||= {}
  options[:container][:class] = arrayorize(options[:container][:class]) << ::Formula.block_class
  
  
  @template.(::Formula.block_tag, options[:container]) do
    submit value, options[: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 %>


394
395
396
397
398
# File 'lib/formula.rb', line 394

def formula_fields_for(record_or_name_or_array, *args, &block)
  options = args.extract_options!
  options[:builder] ||= self.class
  fields_for(record_or_name_or_array, *(args << options), &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">
  <%= f.label(:name)
  <div class="input string"><%= f.text_field(:name)</div>
   <div class="error">...</div>
</div>
<div class="block">
  <%= f.label(:email)
  <div class="input string"><%= f.email_field(:email)</div>
   <div class="error">...</div>
</div>
<div class="block half">
  <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">
  <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>


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
# File 'lib/formula.rb', line 181

def input(method, options = {})
  options[:as] ||= as(method)
  options[:input] ||= {}
  
  self.block(method, options) do
    @template.(::Formula.input_tag, :class => [::Formula.input_class, options[:as]]) do
      case options[:as]
        when :text     then text_area       method, options[:input]            
        when :file     then file_field      method, options[:input]
        when :string   then text_field      method, options[:input]
        when :password then password_field  method, options[:input]
        when :hidden   then hidden_field    method, options[:input]
        when :boolean  then check_box       method, options[:input]
        when :url      then url_field       method, options[:input]
        when :email    then email_field     method, options[:input]
        when :phone    then phone_field     method, options[:input]
        when :number   then number_field    method, options[:input]
        when :country  then country_select  method, options[:input]
        when :date     then date_select     method, options[:input], options[:input].delete(:html) || {}
        when :time     then time_select     method, options[:input], options[:input].delete(:html) || {}
        when :datetime then datetime_select method, options[:input], options[:input].delete(:html) || {}
        when :select   then select          method, options[:choices], options[:input], options[:input].delete(:html) || {}
      end
    end
  end
end