Parent

Files

Padrino::Helpers::FormBuilder::AbstractFormBuilder

Attributes

object[RW]
template[RW]

Public Class Methods

new(template, object, options={}) click to toggle source
# File lib/padrino-helpers/form_builder/abstract_form_builder.rb, line 7
def initialize(template, object, options={})
  @template = template
  @object   = build_object(object)
  @options  = 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

Protected Class Methods

field_types() click to toggle source

Returns the known field types for a formbuilder

# File lib/padrino-helpers/form_builder/abstract_form_builder.rb, line 117
def self.field_types
  [:hidden_field, :text_field, :text_area, :password_field, :file_field, :radio_button, :check_box, :select]
end

Public Instance Methods

check_box(field, options={}) click to toggle source

f.check_box :remember_me, :value => ‘true’, :uncheck_value => ‘0’

# File lib/padrino-helpers/form_builder/abstract_form_builder.rb, line 68
def check_box(field, options={})
  unchecked_value = options.delete(:uncheck_value) || '0'
  options.reverse_merge!(:id => field_id(field), :value => '1')
  options.reverse_merge!(:checked => true) if values_matches_field?(field, options[:value])
  html  = @template.hidden_field_tag(options[:name] || field_name(field), :value => unchecked_value, :id => nil)
  html << @template.check_box_tag(field_name(field), options)
end
error_message_on(field, options={}) click to toggle source

f.error_message_on(field)

# File lib/padrino-helpers/form_builder/abstract_form_builder.rb, line 22
def error_message_on(field, options={})
  @template.error_message_on(object, field, options)
end
error_messages(*params) click to toggle source

f.error_messages

# File lib/padrino-helpers/form_builder/abstract_form_builder.rb, line 16
def error_messages(*params)
  params.unshift object
  @template.error_messages_for(*params)
end
fields_for(child_association, instance_or_collection=nil, &block) click to toggle source

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 lib/padrino-helpers/form_builder/abstract_form_builder.rb, line 104
def fields_for(child_association, instance_or_collection=nil, &block)
  default_collection = self.object.send(child_association)
  include_index = default_collection.respond_to?(:each)
  nested_options = { :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|
    nested_options[:index] = include_index ? index : nil
    @template.fields_for(child_instance,  { :nested => nested_options }, &block)
  end.join("\n")
end
file_field(field, options={}) click to toggle source

f.file_field :photo, :class => ‘avatar’

# File lib/padrino-helpers/form_builder/abstract_form_builder.rb, line 84
def file_field(field, options={})
  options.reverse_merge!(:id => field_id(field))
  options.merge!(:class => field_error(field, options))
  @template.file_field_tag field_name(field), options
end
hidden_field(field, options={}) click to toggle source

f.hidden_field :session_id, :value => “45”

# File lib/padrino-helpers/form_builder/abstract_form_builder.rb, line 33
def hidden_field(field, options={})
  options.reverse_merge!(:value => field_value(field), :id => field_id(field))
  @template.hidden_field_tag field_name(field), options
end
image_submit(source, options={}) click to toggle source

f.image_submit “buttons/submit.png”, :class => ‘large’

# File lib/padrino-helpers/form_builder/abstract_form_builder.rb, line 96
def image_submit(source, options={})
  @template.image_submit_tag source, options
end
label(field, options={}) click to toggle source

f.label :username, :caption => “Nickname”

# File lib/padrino-helpers/form_builder/abstract_form_builder.rb, line 27
def label(field, options={})
  options.reverse_merge!(:caption => "#{field_human_name(field)}: ")
  @template.label_tag(field_id(field), options)
end
password_field(field, options={}) click to toggle source

f.password_field :password, :id => ‘password’

# File lib/padrino-helpers/form_builder/abstract_form_builder.rb, line 53
def password_field(field, options={})
  options.reverse_merge!(:value => field_value(field), :id => field_id(field))
  options.merge!(:class => field_error(field, options))
  @template.password_field_tag field_name(field), options
end
radio_button(field, options={}) click to toggle source

f.radio_button :gender, :value => ‘male’

# File lib/padrino-helpers/form_builder/abstract_form_builder.rb, line 77
def radio_button(field, options={})
  options.reverse_merge!(:id => field_id(field, options[:value]))
  options.reverse_merge!(:checked => true) if values_matches_field?(field, options[:value])
  @template.radio_button_tag field_name(field), options
end
select(field, options={}) click to toggle source

f.select :color, :options => [‘red’, ‘green’], :include_blank => true f.select :color, :collection => @colors, :fields => [:name, :id]

# File lib/padrino-helpers/form_builder/abstract_form_builder.rb, line 61
def select(field, options={})
  options.reverse_merge!(:id => field_id(field), :selected => field_value(field))
  options.merge!(:class => field_error(field, options))
  @template.select_tag field_name(field), options
end
submit(caption="Submit", options={}) click to toggle source

f.submit “Update”, :class => ‘large’

# File lib/padrino-helpers/form_builder/abstract_form_builder.rb, line 91
def submit(caption="Submit", options={})
  @template.submit_tag caption, options
end
text_area(field, options={}) click to toggle source

f.text_area :summary, :value => “(enter summary)”, :id => ‘summary’

# File lib/padrino-helpers/form_builder/abstract_form_builder.rb, line 46
def text_area(field, options={})
  options.reverse_merge!(:value => field_value(field), :id => field_id(field))
  options.merge!(:class => field_error(field, options))
  @template.text_area_tag field_name(field), options
end
text_field(field, options={}) click to toggle source

f.text_field :username, :value => “(blank)”, :id => ‘username’

# File lib/padrino-helpers/form_builder/abstract_form_builder.rb, line 39
def text_field(field, options={})
  options.reverse_merge!(:value => field_value(field), :id => field_id(field))
  options.merge!(:class => field_error(field, options))
  @template.text_field_tag field_name(field), options
end

Protected Instance Methods

build_object(object_or_symbol) click to toggle source

explicit_object is either a symbol or a record Returns a new record of the type specified in the object

# File lib/padrino-helpers/form_builder/abstract_form_builder.rb, line 198
def build_object(object_or_symbol)
  object_or_symbol.is_a?(Symbol) ? @template.instance_variable_get("@#{object_or_symbol}") || object_class(object_or_symbol).new : object_or_symbol
end
field_error(field, options) click to toggle source

Add a :invalid css class to the field if it contain an error

# File lib/padrino-helpers/form_builder/abstract_form_builder.rb, line 128
def field_error(field, options)
  error = @object.errors[field] rescue nil
  error.blank? ? options[:class] : [options[:class], :invalid].flatten.compact.join(" ")
end
field_human_name(field) click to toggle source

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

# File lib/padrino-helpers/form_builder/abstract_form_builder.rb, line 134
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) click to toggle source

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”

# File lib/padrino-helpers/form_builder/abstract_form_builder.rb, line 163
def field_id(field=nil, value=nil)
  result = []
  if root_form?
    result << object_model_name
  elsif nested_form?
    parent_form = @options[:nested][:parent]
    attributes_name = "#{@options[:nested][:association]}_attributes"
    nested_index = @options[:nested][:index]
    fragment = [parent_form.field_id, "_#{attributes_name}"]
    fragment.push("_#{nested_index}") if nested_index
    result << fragment
  end
  result << "_#{field}" unless field.blank?
  result << "_#{value}" unless value.blank?
  result.flatten.join
end
field_name(field=nil) click to toggle source

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

# File lib/padrino-helpers/form_builder/abstract_form_builder.rb, line 142
def field_name(field=nil)
  result = []
  if root_form?
    result << object_model_name
  elsif nested_form?
    parent_form = @options[:nested][:parent]
    attributes_name = "#{@options[:nested][:association]}_attributes"
    nested_index = @options[:nested][:index]
    fragment = [parent_form.field_name, "[#{attributes_name}", "]"]
    fragment.insert(2, "][#{nested_index}") if nested_index
    result << fragment
  end
  result << "[#{field}]" unless field.blank?
  result.flatten.join
end
field_value(field) click to toggle source

Returns the value for the object’s field field_value(:username) => “Joey”

# File lib/padrino-helpers/form_builder/abstract_form_builder.rb, line 192
def field_value(field)
  @object && @object.respond_to?(field) ? @object.send(field) : ""
end
nested_form?() click to toggle source

Returns true if this form object is nested in a parent form

# File lib/padrino-helpers/form_builder/abstract_form_builder.rb, line 186
def nested_form?
  @options[:nested] && @options[:nested][:parent] && @options[:nested][:parent].respond_to?(:object)
end
nested_object_id() click to toggle source

Returns the child object if it exists

# File lib/padrino-helpers/form_builder/abstract_form_builder.rb, line 181
def nested_object_id
  nested_form? && object.respond_to?(:new_record?) && !object.new_record? && object.id
end
object_class(explicit_object) click to toggle source

Returns the class type for the given object

# File lib/padrino-helpers/form_builder/abstract_form_builder.rb, line 209
def object_class(explicit_object)
  explicit_object.is_a?(Symbol) ? explicit_object.to_s.camelize.constantize : explicit_object.class
end
object_model_name(explicit_object=object) click to toggle source

Returns the object’s models name

=> user_assignment
# File lib/padrino-helpers/form_builder/abstract_form_builder.rb, line 204
def object_model_name(explicit_object=object)
  explicit_object.is_a?(Symbol) ? explicit_object : explicit_object.class.to_s.underscore.gsub(/\//, '_')
end
root_form?() click to toggle source

Returns true if this form is the top-level (not nested)

# File lib/padrino-helpers/form_builder/abstract_form_builder.rb, line 214
def root_form?
  !nested_form?
end
values_matches_field?(field, value) click to toggle source

Returns true if the value matches the value in the field field_has_value?(:gender, ‘male’)

# File lib/padrino-helpers/form_builder/abstract_form_builder.rb, line 123
def values_matches_field?(field, value)
  value.present? && (field_value(field).to_s == value.to_s || field_value(field).to_s == 'true')
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.