2010-02-06 19:41:35 +00:00
|
|
|
require 'action_view'
|
2009-12-09 20:25:12 +00:00
|
|
|
require 'simple_form/action_view_extensions/form_helper'
|
|
|
|
require 'simple_form/action_view_extensions/builder'
|
2009-12-09 18:06:59 +00:00
|
|
|
|
2009-12-09 20:25:12 +00:00
|
|
|
module SimpleForm
|
2010-07-06 20:29:26 +00:00
|
|
|
autoload :Components, 'simple_form/components'
|
|
|
|
autoload :ErrorNotification, 'simple_form/error_notification'
|
|
|
|
autoload :FormBuilder, 'simple_form/form_builder'
|
2011-09-03 10:00:23 +00:00
|
|
|
autoload :Helpers, 'simple_form/helpers'
|
2010-07-06 20:29:26 +00:00
|
|
|
autoload :I18nCache, 'simple_form/i18n_cache'
|
|
|
|
autoload :Inputs, 'simple_form/inputs'
|
|
|
|
autoload :MapType, 'simple_form/map_type'
|
2011-09-02 18:33:03 +00:00
|
|
|
autoload :Wrappers, 'simple_form/wrappers'
|
2009-12-10 01:57:05 +00:00
|
|
|
|
2011-09-03 07:38:19 +00:00
|
|
|
# The wrapper object.
|
|
|
|
mattr_accessor :wrapper
|
|
|
|
@@wrapper = nil
|
2010-10-15 15:06:32 +00:00
|
|
|
|
2010-07-06 09:38:35 +00:00
|
|
|
# Method used to tidy up errors.
|
|
|
|
mattr_accessor :error_method
|
|
|
|
@@error_method = :first
|
|
|
|
|
2010-07-06 20:29:26 +00:00
|
|
|
# Default tag used for error notification helper.
|
|
|
|
mattr_accessor :error_notification_tag
|
|
|
|
@@error_notification_tag = :p
|
|
|
|
|
2010-10-18 13:21:19 +00:00
|
|
|
# CSS class to add for error notification helper.
|
|
|
|
mattr_accessor :error_notification_class
|
2010-11-07 09:18:37 +00:00
|
|
|
@@error_notification_class = :error_notification
|
2010-10-18 13:21:19 +00:00
|
|
|
|
|
|
|
# ID to add for error notification helper.
|
|
|
|
mattr_accessor :error_notification_id
|
|
|
|
@@error_notification_id = nil
|
|
|
|
|
2010-01-09 15:05:02 +00:00
|
|
|
# Series of attemps to detect a default label method for collection.
|
2009-12-10 02:22:53 +00:00
|
|
|
mattr_accessor :collection_label_methods
|
2009-12-11 01:48:29 +00:00
|
|
|
@@collection_label_methods = [ :to_label, :name, :title, :to_s ]
|
2009-12-10 02:22:53 +00:00
|
|
|
|
2010-01-09 15:05:02 +00:00
|
|
|
# Series of attemps to detect a default value method for collection.
|
2009-12-10 02:22:53 +00:00
|
|
|
mattr_accessor :collection_value_methods
|
|
|
|
@@collection_value_methods = [ :id, :to_s ]
|
2009-12-10 02:34:45 +00:00
|
|
|
|
2010-11-21 16:51:49 +00:00
|
|
|
# You can wrap a collection of radio/check boxes in a pre-defined tag, defaulting to none.
|
|
|
|
mattr_accessor :collection_wrapper_tag
|
|
|
|
@@collection_wrapper_tag = nil
|
|
|
|
|
|
|
|
# You can wrap each item in a collection of radio/check boxes with a tag, defaulting to none.
|
|
|
|
mattr_accessor :item_wrapper_tag
|
2011-04-06 03:33:41 +00:00
|
|
|
@@item_wrapper_tag = :span
|
2010-11-21 16:51:49 +00:00
|
|
|
|
2009-12-10 22:11:15 +00:00
|
|
|
# How the label text should be generated altogether with the required text.
|
|
|
|
mattr_accessor :label_text
|
|
|
|
@@label_text = lambda { |label, required| "#{required} #{label}" }
|
2009-12-11 13:12:57 +00:00
|
|
|
|
2011-02-07 23:42:18 +00:00
|
|
|
# You can define the class to use on all labels. Default is nil.
|
|
|
|
mattr_accessor :label_class
|
|
|
|
@@label_class = nil
|
|
|
|
|
2011-03-07 22:13:28 +00:00
|
|
|
# You can define the class to use on all forms. Default is simple_form.
|
|
|
|
mattr_accessor :form_class
|
|
|
|
@@form_class = :simple_form
|
|
|
|
|
2010-07-06 09:28:23 +00:00
|
|
|
# Whether attributes are required by default (or not).
|
|
|
|
mattr_accessor :required_by_default
|
|
|
|
@@required_by_default = true
|
|
|
|
|
2011-05-17 02:17:09 +00:00
|
|
|
# Tell browsers whether to use default HTML5 validations (novalidate option).
|
2011-05-16 09:50:31 +00:00
|
|
|
mattr_accessor :browser_validations
|
|
|
|
@@browser_validations = true
|
2011-04-06 00:23:40 +00:00
|
|
|
|
2011-05-17 02:17:09 +00:00
|
|
|
# Determines whether HTML5 types (:email, :url, :search, :tel) and attributes
|
|
|
|
# (e.g. required) are used or not. True by default.
|
|
|
|
# Having this on in non-HTML5 compliant sites can cause odd behavior in
|
|
|
|
# HTML5-aware browsers such as Chrome.
|
2011-05-16 09:42:58 +00:00
|
|
|
mattr_accessor :html5
|
|
|
|
@@html5 = true
|
2011-04-08 00:53:27 +00:00
|
|
|
|
2009-12-11 13:12:57 +00:00
|
|
|
# Collection of methods to detect if a file type was given.
|
|
|
|
mattr_accessor :file_methods
|
2009-12-13 01:05:51 +00:00
|
|
|
@@file_methods = [ :mounted_as, :file?, :public_filename ]
|
2009-12-11 13:53:18 +00:00
|
|
|
|
2010-11-26 00:35:38 +00:00
|
|
|
# Custom mappings for input types. This should be a hash containing a regexp
|
|
|
|
# to match as key, and the input type that will be used when the field name
|
|
|
|
# matches the regexp as value, such as { /count/ => :integer }.
|
|
|
|
mattr_accessor :input_mappings
|
|
|
|
@@input_mappings = nil
|
|
|
|
|
2009-12-11 13:53:18 +00:00
|
|
|
# Default priority for time_zone inputs.
|
|
|
|
mattr_accessor :time_zone_priority
|
|
|
|
@@time_zone_priority = nil
|
|
|
|
|
|
|
|
# Default priority for country inputs.
|
|
|
|
mattr_accessor :country_priority
|
|
|
|
@@country_priority = nil
|
2009-12-11 20:23:00 +00:00
|
|
|
|
2010-02-06 21:06:25 +00:00
|
|
|
# Maximum size allowed for inputs.
|
|
|
|
mattr_accessor :default_input_size
|
|
|
|
@@default_input_size = 50
|
|
|
|
|
2010-10-04 09:59:25 +00:00
|
|
|
# When off, do not use translations in hint, labels and placeholders.
|
|
|
|
# It is a small performance improvement if you are not using such features.
|
2010-09-30 06:12:45 +00:00
|
|
|
mattr_accessor :translate
|
|
|
|
@@translate = true
|
|
|
|
|
2011-04-27 18:55:03 +00:00
|
|
|
# Automatically discover new inputs in Rails' autoload path.
|
|
|
|
mattr_accessor :inputs_discovery
|
|
|
|
@@inputs_discovery = true
|
|
|
|
|
|
|
|
# Cache simple form inputs discovery
|
|
|
|
mattr_accessor :cache_discovery
|
|
|
|
@@cache_discovery = !Rails.env.development?
|
|
|
|
|
2010-06-04 05:41:04 +00:00
|
|
|
# Default way to setup SimpleForm. Run rails generate simple_form:install
|
2009-12-11 20:23:00 +00:00
|
|
|
# to create a fresh initializer with all configuration values.
|
|
|
|
def self.setup
|
|
|
|
yield self
|
|
|
|
end
|
2011-09-02 18:33:03 +00:00
|
|
|
|
2011-09-03 08:45:10 +00:00
|
|
|
# Builds a new wrapper using SimpleForm::Wrappers::Builder.
|
|
|
|
def self.build(options={})
|
2011-09-03 08:33:57 +00:00
|
|
|
builder = SimpleForm::Wrappers::Builder.new
|
|
|
|
yield builder
|
2011-09-03 08:45:10 +00:00
|
|
|
SimpleForm::Wrappers::Root.new(builder.to_a, options)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Build a new wrapper using SimpleForm::Wrappers::Builder and
|
|
|
|
# sets it as the default wrapper.
|
|
|
|
def self.components(*args, &block)
|
|
|
|
self.wrapper = build(*args, &block)
|
2011-09-03 08:33:57 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
components :tag => :div, :class => :input, :error_class => :field_with_errors do |b|
|
|
|
|
b.use :placeholder
|
|
|
|
b.use :label_input
|
|
|
|
b.use :hint, :tag => :span, :class => :hint
|
|
|
|
b.use :error, :tag => :span, :class => :error
|
|
|
|
end
|
|
|
|
|
2011-09-03 07:38:19 +00:00
|
|
|
## DEPRECATED METHODS SINCE 2.0
|
|
|
|
|
2011-09-03 08:45:10 +00:00
|
|
|
# DEPRECATED. Default tag used on hints.
|
2011-09-03 07:38:19 +00:00
|
|
|
mattr_accessor :hint_tag
|
|
|
|
@@hint_tag = :span
|
|
|
|
|
2011-09-03 08:45:10 +00:00
|
|
|
# DEPRECATED. CSS class to add to all hint tags.
|
2011-09-03 07:38:19 +00:00
|
|
|
mattr_accessor :hint_class
|
|
|
|
@@hint_class = :hint
|
|
|
|
|
2011-09-03 08:45:10 +00:00
|
|
|
# DEPRECATED. Default tag used on errors.
|
2011-09-03 07:38:19 +00:00
|
|
|
mattr_accessor :error_tag
|
|
|
|
@@error_tag = :span
|
|
|
|
|
2011-09-03 08:45:10 +00:00
|
|
|
# DEPRECATED. CSS class to add to all error tags.
|
2011-09-03 07:38:19 +00:00
|
|
|
mattr_accessor :error_class
|
|
|
|
@@error_class = :error
|
|
|
|
|
2011-09-03 08:45:10 +00:00
|
|
|
# DEPRECATED. You can wrap all inputs in a pre-defined tag. Default is a div.
|
2011-09-03 07:38:19 +00:00
|
|
|
mattr_accessor :wrapper_tag
|
|
|
|
@@wrapper_tag = :div
|
|
|
|
|
2011-09-03 08:45:10 +00:00
|
|
|
# DEPRECATED. You can define the class to use on all wrappers. Default is input.
|
2011-09-03 07:38:19 +00:00
|
|
|
mattr_accessor :wrapper_class
|
|
|
|
@@wrapper_class = :input
|
|
|
|
|
2011-09-03 08:45:10 +00:00
|
|
|
# DEPRECATED. You can define the class to add to the wrapper when the field has errors. Default is field_with_errors.
|
2011-09-03 07:38:19 +00:00
|
|
|
mattr_accessor :wrapper_error_class
|
|
|
|
@@wrapper_error_class = :field_with_errors
|
2011-09-02 18:33:03 +00:00
|
|
|
|
2011-09-03 08:45:10 +00:00
|
|
|
# DEPRECATED. Define components using an array.
|
2011-09-02 18:33:03 +00:00
|
|
|
def self.components=(array)
|
2011-09-03 08:45:10 +00:00
|
|
|
ActiveSupport::Deprecation.warn <<-TEXT
|
|
|
|
Setting config.components= in SimpleForm is deprecated. SimpleForm 2.0 ships with a new components syntax which is more flexible and powerful. If your components were defined as:
|
|
|
|
|
|
|
|
config.components = [ :placeholder, :maxlength, :label_input, :hint, :error ]
|
|
|
|
|
|
|
|
They can now be defined as:
|
|
|
|
|
|
|
|
config.components :tag => :div, :class => :input,
|
|
|
|
:error_class => :field_with_errors do |b|
|
|
|
|
b.use :placeholder
|
|
|
|
b.use :maxlength
|
|
|
|
b.use :label_input
|
|
|
|
b.use :hint, :tag => :span, :class => :hint
|
|
|
|
b.use :error, :tag => :span, :class => :error
|
|
|
|
end
|
|
|
|
|
|
|
|
The new components syntax also allows custom wrappers:
|
|
|
|
|
|
|
|
config.components do |b|
|
|
|
|
b.use :placeholder
|
|
|
|
b.use :maxlength
|
|
|
|
b.use :label_input
|
|
|
|
b.use :tag => :div, :class => "separator" do |ba|
|
|
|
|
ba.use :hint, :tag => :span, :class => :hint
|
|
|
|
ba.use :error, :tag => :span, :class => :error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
The following methods are also deprecated: wrapper_tag, wrapper_class, wrapper_error_class, error_tag, error_class, hint_tag and hint_class.
|
|
|
|
TEXT
|
|
|
|
|
|
|
|
self.deprecated_components = array
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.deprecated_components=(array) #:nodoc:
|
2011-09-03 07:38:19 +00:00
|
|
|
self.wrapper = Wrappers::Root.new(
|
|
|
|
array.map do |item|
|
|
|
|
case item
|
|
|
|
when :error
|
|
|
|
Wrappers::Single.new(:error, :tag => SimpleForm.error_tag, :class => SimpleForm.error_class)
|
|
|
|
when :hint
|
|
|
|
Wrappers::Single.new(:hint, :tag => SimpleForm.hint_tag, :class => SimpleForm.hint_class)
|
|
|
|
else
|
|
|
|
item
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
:tag => SimpleForm.wrapper_tag,
|
|
|
|
:class => SimpleForm.wrapper_class,
|
|
|
|
:error_class => SimpleForm.wrapper_error_class
|
|
|
|
)
|
2011-09-02 18:33:03 +00:00
|
|
|
end
|
2011-09-03 07:38:19 +00:00
|
|
|
end
|