heartcombo--simple_form/lib/simple_form/form_builder.rb

390 lines
14 KiB
Ruby
Raw Normal View History

2009-11-18 23:50:43 +00:00
module SimpleForm
class FormBuilder < ActionView::Helpers::FormBuilder
attr_reader :template, :object_name, :object
2009-12-09 18:55:39 +00:00
extend MapType
include SimpleForm::Inputs
map_type :text, :file, :to => SimpleForm::Inputs::MappingInput
map_type :string, :password, :email, :search, :tel, :url, :to => SimpleForm::Inputs::StringInput
map_type :integer, :decimal, :float, :to => SimpleForm::Inputs::NumericInput
map_type :select, :radio, :check_boxes, :to => SimpleForm::Inputs::CollectionInput
map_type :date, :time, :datetime, :to => SimpleForm::Inputs::DateTimeInput
map_type :country, :time_zone, :to => SimpleForm::Inputs::PriorityInput
map_type :boolean, :to => SimpleForm::Inputs::BooleanInput
2011-04-27 18:55:03 +00:00
def self.discovery_cache
@discovery_cache ||= {}
end
2009-12-11 03:18:56 +00:00
# Basic input helper, combines all components in the stack to generate
# input html based on options the user define and some guesses through
2009-12-10 17:57:24 +00:00
# database column information. By default a call to input will generate
2009-12-11 03:18:56 +00:00
# label + input + hint (when defined) + errors (when exists), and all can
# be configured inside a wrapper html.
2009-12-10 21:52:23 +00:00
#
2009-12-11 11:19:46 +00:00
# == Examples
2009-12-10 21:52:23 +00:00
#
2009-12-10 17:57:24 +00:00
# # Imagine @user has error "can't be blank" on name
# simple_form_for @user do |f|
# f.input :name, :hint => 'My hint'
# end
#
2009-12-11 02:34:20 +00:00
# This is the output html (only the input portion, not the form):
#
# <label class="string required" for="user_name">
# <abbr title="required">*</abbr> Super User Name!
# </label>
# <input class="string required" id="user_name" maxlength="100"
# name="user[name]" size="100" type="text" value="Carlos" />
2009-12-10 17:57:24 +00:00
# <span class="hint">My hint</span>
# <span class="error">can't be blank</span>
#
# Each database type will render a default input, based on some mappings and
# heuristic to determine which is the best option.
2009-12-10 21:52:23 +00:00
#
2009-12-10 17:57:24 +00:00
# You have some options for the input to enable/disable some functions:
#
2009-12-10 21:52:23 +00:00
# :as => allows you to define the input type you want, for instance you
# can use it to generate a text field for a date column.
#
# :required => defines whether this attribute is required or not. True
# by default.
#
# The fact SimpleForm is built in components allow the interface to be unified.
# So, for instance, if you need to disable :hint for a given input, you can pass
# :hint => false. The same works for :error, :label and :wrapper.
#
# Besides the html for any component can be changed. So, if you want to change
# the label html you just need to give a hash to :label_html. To configure the
# input html, supply :input_html instead and so on.
#
# == Options
#
# Some inputs, as datetime, time and select allow you to give extra options, like
2009-12-11 02:34:20 +00:00
# prompt and/or include blank. Such options are given in plainly:
2009-12-10 21:52:23 +00:00
#
2009-12-11 02:34:20 +00:00
# f.input :created_at, :include_blank => true
2009-12-10 21:52:23 +00:00
#
# == Collection
#
# When playing with collections (:radio and :select inputs), you have three extra
# options:
#
# :collection => use to determine the collection to generate the radio or select
#
# :label_method => the method to apply on the array collection to get the label
#
# :value_method => the method to apply on the array collection to get the value
#
# == Priority
#
# Some inputs, as :time_zone and :country accepts a :priority option. If none is
# given SimpleForm.time_zone_priority and SimpleForm.country_priority are used respectivelly.
#
2010-01-10 04:12:57 +00:00
def input(attribute_name, options={}, &block)
column = find_attribute_column(attribute_name)
input_type = default_input_type(attribute_name, column, options)
2010-11-07 09:18:37 +00:00
2010-01-10 04:12:57 +00:00
if block_given?
2010-11-07 09:18:37 +00:00
SimpleForm::Inputs::BlockInput.new(self, attribute_name, column, input_type, options, &block).render
2010-01-09 14:39:14 +00:00
else
2011-04-27 18:55:03 +00:00
find_mapping(input_type).new(self, attribute_name, column, input_type, options).render
2010-01-09 14:39:14 +00:00
end
2009-11-19 21:26:16 +00:00
end
alias :attribute :input
# Creates a input tag for the given attribute. All the given options
# are sent as :input_html.
#
# == Examples
#
# simple_form_for @user do |f|
2011-04-19 18:16:10 +00:00
# f.input_field :name
# end
#
# This is the output html (only the input portion, not the form):
#
# <input class="string required" id="user_name" maxlength="100"
# name="user[name]" size="100" type="text" value="Carlos" />
#
2011-04-19 18:16:10 +00:00
def input_field(attribute_name, options={})
2011-04-19 21:20:58 +00:00
options[:input_html] = options.except(:as)
options.merge!(:components => [:input], :wrapper => false)
input(attribute_name, options)
end
2009-11-19 21:26:16 +00:00
2009-12-11 02:18:14 +00:00
# Helper for dealing with association selects/radios, generating the
2009-12-11 11:19:46 +00:00
# collection automatically. It's just a wrapper to input, so all options
# supported in input are also supported by association. Some extra options
# can also be given:
#
# == Examples
2009-12-11 02:18:14 +00:00
#
# simple_form_for @user do |f|
# f.association :company # Company.all
# end
#
# f.association :company, :collection => Company.all(:order => 'name')
# # Same as using :order option, but overriding collection
#
2010-01-10 04:12:57 +00:00
# == Block
#
# When a block is given, association simple behaves as a proxy to
# simple_fields_for:
#
# f.association :company do |c|
# c.input :name
# c.input :type
# end
#
# From the options above, only :collection can also be supplied.
#
def association(association, options={}, &block)
return simple_fields_for(*[association,
options.delete(:collection), options].compact, &block) if block_given?
2009-12-11 02:18:14 +00:00
raise ArgumentError, "Association cannot be used in forms not associated with an object" unless @object
reflection = find_association_reflection(association)
raise "Association #{association.inspect} not found" unless reflection
options[:as] ||= :select
options[:collection] ||= reflection.klass.all(reflection.options.slice(:conditions, :order))
attribute = case reflection.macro
when :belongs_to
reflection.options[:foreign_key] || :"#{reflection.name}_id"
when :has_one
2011-04-29 19:23:42 +00:00
raise ":has_one associations are not supported by f.association"
else
if options[:as] == :select
html_options = options[:input_html] ||= {}
html_options[:size] ||= 5
html_options[:multiple] = true unless html_options.key?(:multiple)
end
2009-12-11 02:18:14 +00:00
:"#{reflection.name.to_s.singularize}_ids"
end
2009-12-11 02:18:14 +00:00
2011-04-29 19:23:42 +00:00
# Force the association to be preloaded for performance.
if options[:preload] != false && object.respond_to?(association)
target = object.send(association)
target.to_a if target.respond_to?(:to_a)
end
input(attribute, options.merge(:reflection => reflection))
2009-12-11 02:18:14 +00:00
end
2009-12-10 20:03:27 +00:00
# Creates a button:
#
# form_for @user do |f|
# f.button :submit
# end
#
2010-02-06 20:44:07 +00:00
# It just acts as a proxy to method name given.
2009-12-10 20:03:27 +00:00
#
2010-02-06 20:44:07 +00:00
def button(type, *args, &block)
options = args.extract_options!
options[:class] = "button #{options[:class]}".strip
args << options
2010-11-07 09:18:37 +00:00
if respond_to?("#{type}_button")
send("#{type}_button", *args, &block)
2010-02-06 20:44:07 +00:00
else
send(type, *args, &block)
2009-12-10 20:03:27 +00:00
end
end
2009-12-10 17:57:24 +00:00
# Creates an error tag based on the given attribute, only when the attribute
2009-12-10 21:52:23 +00:00
# contains errors. All the given options are sent as :error_html.
#
# == Examples
#
# f.error :name
# f.error :name, :id => "cool_error"
2009-12-10 22:11:15 +00:00
#
def error(attribute_name, options={})
options[:error_html] = options.dup
column = find_attribute_column(attribute_name)
input_type = default_input_type(attribute_name, column, options)
SimpleForm::Inputs::Base.new(self, attribute_name, column, input_type, options).error
end
2011-04-27 19:58:59 +00:00
# Return the error but also considering its name. This is used
# when errors for a hidden field need to be shown.
#
# == Examples
#
# f.full_error :token #=> <span class="error">Token is invalid</span>
#
def full_error(attribute_name, options={})
2011-04-27 20:07:47 +00:00
options[:error_prefix] ||= if object.class.respond_to?(:human_attribute_name)
2011-04-27 19:58:59 +00:00
object.class.human_attribute_name(attribute_name.to_s)
else
attribute_name.to_s.humanize
end
error(attribute_name, options)
end
2009-12-10 21:52:23 +00:00
# Creates a hint tag for the given attribute. Accepts a symbol indicating
# an attribute for I18n lookup or a string. All the given options are sent
# as :hint_html.
#
# == Examples
#
# f.hint :name # Do I18n lookup
# f.hint :name, :id => "cool_hint"
# f.hint "Don't forget to accept this"
#
def hint(attribute_name, options={})
options[:hint_html] = options.dup
if attribute_name.is_a?(String)
options[:hint] = attribute_name
attribute_name, column, input_type = nil, nil, nil
else
column = find_attribute_column(attribute_name)
input_type = default_input_type(attribute_name, column, options)
end
SimpleForm::Inputs::Base.new(self, attribute_name, column, input_type, options).hint
end
2009-12-10 17:57:24 +00:00
# Creates a default label tag for the given attribute. You can give a label
2009-12-10 21:52:23 +00:00
# through the :label option or using i18n. All the given options are sent
# as :label_html.
#
# == Examples
#
# f.label :name # Do I18n lookup
# f.label :name, "Name" # Same behavior as Rails, do not add required tag
# f.label :name, :label => "Name" # Same as above, but adds required tag
#
# f.label :name, :required => false
# f.label :name, :id => "cool_label"
#
def label(attribute_name, *args)
2009-12-10 21:52:23 +00:00
return super if args.first.is_a?(String)
options = args.extract_options!
options[:label_html] = options.dup
2010-11-07 09:18:37 +00:00
options[:label] = options.delete(:label)
options[:required] = options.delete(:required)
column = find_attribute_column(attribute_name)
input_type = default_input_type(attribute_name, column, options)
SimpleForm::Inputs::Base.new(self, attribute_name, column, input_type, options).label
end
# Creates an error notification message that only appears when the form object
# has some error. You can give a specific message with the :message option,
# otherwise it will look for a message using I18n. All other options given are
# passed straight as html options to the html tag.
#
# == Examples
#
# f.error_notification
# f.error_notification :message => 'Something went wrong'
# f.error_notification :id => 'user_error_message', :class => 'form_error'
#
def error_notification(options={})
SimpleForm::ErrorNotification.new(self, options).render
end
private
2009-12-10 17:57:24 +00:00
# Attempt to guess the better input type given the defined options. By
# default alwayls fallback to the user :as option, or to a :select when a
# collection is given.
def default_input_type(attribute_name, column, options) #:nodoc:
return options[:as].to_sym if options[:as]
2010-11-26 00:35:38 +00:00
return :select if options[:collection]
custom_type = find_custom_type(attribute_name.to_s) and return custom_type
input_type = column.try(:type)
case input_type
2011-02-08 01:25:09 +00:00
when :timestamp
:datetime
when :string, nil
case attribute_name.to_s
when /password/ then :password
when /time_zone/ then :time_zone
when /country/ then :country
when /email/ then :email
when /phone/ then :tel
when /url/ then :url
else
2011-02-08 01:25:09 +00:00
file_method?(attribute_name) ? :file : (input_type || :string)
end
else
input_type
end
end
2009-12-08 19:08:36 +00:00
def find_custom_type(attribute_name) #:nodoc:
SimpleForm.input_mappings.find { |match, type|
attribute_name =~ match
}.try(:last) if SimpleForm.input_mappings
end
def file_method?(attribute_name) #:nodoc:
file = @object.send(attribute_name) if @object.respond_to?(attribute_name)
file && SimpleForm.file_methods.any? { |m| file.respond_to?(m) }
2009-12-11 13:12:57 +00:00
end
def find_attribute_column(attribute_name) #:nodoc:
if @object.respond_to?(:column_for_attribute)
@object.column_for_attribute(attribute_name)
end
end
def find_association_reflection(association) #:nodoc:
if @object.class.respond_to?(:reflect_on_association)
@object.class.reflect_on_association(association)
end
2009-12-11 02:18:14 +00:00
end
2011-04-27 18:55:03 +00:00
# Attempts to find a mapping. It follows the following rules:
#
# 1) It tries to find a registered mapping, if succeeds:
2011-04-27 20:00:00 +00:00
# a) Try to find an alternative with the same name in the Object scope
2011-04-27 18:55:03 +00:00
# b) Or use the found mapping
# 2) If not, fallbacks to #{input_type}Input
# 3) If not, fallbacks to SimpleForm::Inputs::#{input_type}
def find_mapping(input_type) #:nodoc:
discovery_cache[input_type] ||=
if mapping = self.class.mappings[input_type]
mapping_override(mapping) || mapping
else
camelized = "#{input_type.to_s.camelize}Input"
attempt_mapping(camelized, Object) || attempt_mapping(camelized, self.class) ||
raise("No input found for #{input_type}")
end
end
# If cache_discovery is enabled, use the class level cache that persists
# between requests, otherwise use the instance one.
def discovery_cache #:nodoc:
if SimpleForm.cache_discovery
self.class.discovery_cache
else
@discovery_cache ||= {}
end
end
def mapping_override(klass) #:nodoc:
name = klass.name
if name =~ /^SimpleForm::Inputs/
2011-04-27 19:26:02 +00:00
attempt_mapping name.split("::").last, Object
2011-04-27 18:55:03 +00:00
end
end
def attempt_mapping(mapping, at) #:nodoc:
return if SimpleForm.inputs_discovery == false && at == Object
begin
at.const_get(mapping)
rescue NameError => e
e.message =~ /#{mapping}$/ ? nil : raise
end
end
2009-11-18 23:50:43 +00:00
end
end