2010-02-06 14:41:35 -05:00
|
|
|
require 'action_view'
|
2009-12-09 15:25:12 -05:00
|
|
|
require 'simple_form/action_view_extensions/form_helper'
|
|
|
|
require 'simple_form/action_view_extensions/builder'
|
2011-12-05 10:26:49 -05:00
|
|
|
require 'active_support/core_ext/hash/slice'
|
|
|
|
require 'active_support/core_ext/hash/except'
|
|
|
|
require 'active_support/core_ext/hash/reverse_merge'
|
2009-12-09 13:06:59 -05:00
|
|
|
|
2009-12-09 15:25:12 -05:00
|
|
|
module SimpleForm
|
2010-07-06 16:29:26 -04:00
|
|
|
autoload :Components, 'simple_form/components'
|
|
|
|
autoload :ErrorNotification, 'simple_form/error_notification'
|
|
|
|
autoload :FormBuilder, 'simple_form/form_builder'
|
2011-09-03 06:00:23 -04:00
|
|
|
autoload :Helpers, 'simple_form/helpers'
|
2010-07-06 16:29:26 -04:00
|
|
|
autoload :I18nCache, 'simple_form/i18n_cache'
|
|
|
|
autoload :Inputs, 'simple_form/inputs'
|
|
|
|
autoload :MapType, 'simple_form/map_type'
|
2011-09-02 14:33:03 -04:00
|
|
|
autoload :Wrappers, 'simple_form/wrappers'
|
2009-12-09 20:57:05 -05:00
|
|
|
|
2011-09-04 05:31:24 -04:00
|
|
|
## CONFIGURATION OPTIONS
|
2010-10-15 11:06:32 -04:00
|
|
|
|
2010-07-06 05:38:35 -04:00
|
|
|
# Method used to tidy up errors.
|
|
|
|
mattr_accessor :error_method
|
|
|
|
@@error_method = :first
|
|
|
|
|
2010-07-06 16:29:26 -04:00
|
|
|
# Default tag used for error notification helper.
|
|
|
|
mattr_accessor :error_notification_tag
|
|
|
|
@@error_notification_tag = :p
|
|
|
|
|
2010-10-18 09:21:19 -04:00
|
|
|
# CSS class to add for error notification helper.
|
|
|
|
mattr_accessor :error_notification_class
|
2010-11-07 04:18:37 -05:00
|
|
|
@@error_notification_class = :error_notification
|
2010-10-18 09:21:19 -04:00
|
|
|
|
|
|
|
# ID to add for error notification helper.
|
|
|
|
mattr_accessor :error_notification_id
|
|
|
|
@@error_notification_id = nil
|
|
|
|
|
2010-01-09 10:05:02 -05:00
|
|
|
# Series of attemps to detect a default label method for collection.
|
2009-12-09 21:22:53 -05:00
|
|
|
mattr_accessor :collection_label_methods
|
2009-12-10 20:48:29 -05:00
|
|
|
@@collection_label_methods = [ :to_label, :name, :title, :to_s ]
|
2009-12-09 21:22:53 -05:00
|
|
|
|
2010-01-09 10:05:02 -05:00
|
|
|
# Series of attemps to detect a default value method for collection.
|
2009-12-09 21:22:53 -05:00
|
|
|
mattr_accessor :collection_value_methods
|
|
|
|
@@collection_value_methods = [ :id, :to_s ]
|
2009-12-09 21:34:45 -05:00
|
|
|
|
2010-11-21 11:51:49 -05: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
|
|
|
|
|
2011-10-10 13:06:53 -04:00
|
|
|
# You can define the class to use on all collection wrappers. Defaulting to none.
|
|
|
|
mattr_accessor :collection_wrapper_class
|
|
|
|
@@collection_wrapper_class = nil
|
|
|
|
|
2010-11-21 11:51:49 -05:00
|
|
|
# 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-05 23:33:41 -04:00
|
|
|
@@item_wrapper_tag = :span
|
2010-11-21 11:51:49 -05:00
|
|
|
|
2009-12-10 17:11:15 -05: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 08:12:57 -05:00
|
|
|
|
2011-02-07 18:42:18 -05:00
|
|
|
# You can define the class to use on all labels. Default is nil.
|
|
|
|
mattr_accessor :label_class
|
|
|
|
@@label_class = nil
|
|
|
|
|
2012-01-26 15:20:41 -05:00
|
|
|
# Define the way to render check boxes / radio buttons with labels.
|
|
|
|
# :inline => input + label (default)
|
2012-01-26 08:30:08 -05:00
|
|
|
# :nested => label > input
|
2012-01-26 12:06:33 -05:00
|
|
|
mattr_accessor :boolean_style
|
|
|
|
@@boolean_style = :inline
|
2012-01-26 08:30:08 -05:00
|
|
|
|
2011-03-07 17:13:28 -05: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 05:28:23 -04:00
|
|
|
# Whether attributes are required by default (or not).
|
|
|
|
mattr_accessor :required_by_default
|
|
|
|
@@required_by_default = true
|
|
|
|
|
2011-05-16 22:17:09 -04:00
|
|
|
# Tell browsers whether to use default HTML5 validations (novalidate option).
|
2011-05-16 05:50:31 -04:00
|
|
|
mattr_accessor :browser_validations
|
|
|
|
@@browser_validations = true
|
2011-04-05 20:23:40 -04:00
|
|
|
|
2009-12-11 08:12:57 -05:00
|
|
|
# Collection of methods to detect if a file type was given.
|
|
|
|
mattr_accessor :file_methods
|
2009-12-12 20:05:51 -05:00
|
|
|
@@file_methods = [ :mounted_as, :file?, :public_filename ]
|
2009-12-11 08:53:18 -05:00
|
|
|
|
2010-11-25 19:35:38 -05: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 08:53:18 -05: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 15:23:00 -05:00
|
|
|
|
2010-02-06 16:06:25 -05:00
|
|
|
# Maximum size allowed for inputs.
|
|
|
|
mattr_accessor :default_input_size
|
|
|
|
@@default_input_size = 50
|
|
|
|
|
2011-12-04 09:49:57 -05:00
|
|
|
# When off, do not use translations in labels. Disabling translation in
|
|
|
|
# hints and placeholders can be done manually in the wrapper API.
|
|
|
|
mattr_accessor :translate_labels
|
|
|
|
@@translate_labels = true
|
2010-09-30 02:12:45 -04:00
|
|
|
|
2011-04-27 14:55:03 -04: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
|
2012-01-10 21:56:42 -05:00
|
|
|
@@cache_discovery = defined?(Rails) && !Rails.env.development?
|
2011-04-27 14:55:03 -04:00
|
|
|
|
2011-09-11 22:14:39 -04:00
|
|
|
# Adds a class to each generated button, mostly for compatiblity
|
|
|
|
mattr_accessor :button_class
|
|
|
|
@@button_class = 'button'
|
|
|
|
|
2011-09-04 05:31:24 -04:00
|
|
|
## WRAPPER CONFIGURATION
|
2012-01-24 13:54:28 -05:00
|
|
|
# The default wrapper to be used by the FormBuilder.
|
|
|
|
mattr_accessor :default_wrapper
|
|
|
|
@@default_wrapper = :default
|
2011-09-04 05:31:24 -04:00
|
|
|
@@wrappers = {}
|
2011-09-04 05:12:16 -04:00
|
|
|
|
2011-09-04 05:31:24 -04:00
|
|
|
# Retrieves a given wrapper
|
|
|
|
def self.wrapper(name)
|
2011-09-26 23:59:06 -04:00
|
|
|
@@wrappers[name.to_sym] or raise WrapperNotFound, "Couldn't find wrapper with name #{name}"
|
2011-09-26 21:47:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Raised when fails to find a given wrapper name
|
|
|
|
class WrapperNotFound < StandardError
|
2011-09-04 05:12:16 -04:00
|
|
|
end
|
|
|
|
|
2011-09-04 05:31:24 -04:00
|
|
|
# Define a new wrapper using SimpleForm::Wrappers::Builder
|
|
|
|
# and store it in the given name.
|
|
|
|
def self.wrappers(*args, &block)
|
|
|
|
if block_given?
|
2011-09-26 22:41:53 -04:00
|
|
|
options = args.extract_options!
|
|
|
|
name = args.first || :default
|
|
|
|
@@wrappers[name.to_sym] = build(options, &block)
|
2011-09-04 05:31:24 -04:00
|
|
|
else
|
|
|
|
@@wrappers
|
2011-09-04 05:12:16 -04:00
|
|
|
end
|
2009-12-11 15:23:00 -05:00
|
|
|
end
|
2011-09-02 14:33:03 -04:00
|
|
|
|
2011-09-03 04:45:10 -04:00
|
|
|
# Builds a new wrapper using SimpleForm::Wrappers::Builder.
|
|
|
|
def self.build(options={})
|
2011-11-09 17:02:27 -05:00
|
|
|
options[:tag] = :div if options[:tag].nil?
|
2012-01-11 15:12:49 -05:00
|
|
|
builder = SimpleForm::Wrappers::Builder.new(options)
|
2011-09-03 04:33:57 -04:00
|
|
|
yield builder
|
2011-09-03 04:45:10 -04:00
|
|
|
SimpleForm::Wrappers::Root.new(builder.to_a, options)
|
|
|
|
end
|
|
|
|
|
2012-01-09 14:34:37 -05:00
|
|
|
wrappers :class => :input, :hint_class => :field_with_hint, :error_class => :field_with_errors do |b|
|
2011-12-04 10:15:27 -05:00
|
|
|
b.use :html5
|
|
|
|
|
2011-12-04 10:31:13 -05:00
|
|
|
b.use :min_max
|
2011-12-04 07:14:39 -05:00
|
|
|
b.use :maxlength
|
2011-09-03 04:33:57 -04:00
|
|
|
b.use :placeholder
|
2012-01-11 15:12:49 -05:00
|
|
|
b.optional :pattern
|
|
|
|
b.optional :readonly
|
2011-12-04 06:15:22 -05:00
|
|
|
|
2011-09-03 04:33:57 -04:00
|
|
|
b.use :label_input
|
|
|
|
b.use :hint, :tag => :span, :class => :hint
|
|
|
|
b.use :error, :tag => :span, :class => :error
|
|
|
|
end
|
2011-09-04 05:31:24 -04:00
|
|
|
|
|
|
|
## SETUP
|
|
|
|
|
2011-12-04 10:15:27 -05:00
|
|
|
DEPRECATED = %w(hint_tag hint_class error_tag error_class wrapper_tag wrapper_class wrapper_error_class components html5)
|
2011-12-08 03:47:18 -05:00
|
|
|
@@deprecated = []
|
2011-09-04 05:31:24 -04:00
|
|
|
|
|
|
|
DEPRECATED.each do |method|
|
2011-12-08 03:47:18 -05:00
|
|
|
class_eval "def self.#{method}=(*); @@deprecated << :#{method}=; end"
|
2011-12-04 10:15:27 -05:00
|
|
|
class_eval "def self.#{method}; ActiveSupport::Deprecation.warn 'SimpleForm.#{method} is deprecated and has no effect'; end"
|
2011-12-04 09:49:57 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.translate=(value)
|
|
|
|
ActiveSupport::Deprecation.warn "SimpleForm.translate= is disabled in favor of translate_labels="
|
|
|
|
self.translate_labels = value
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.translate
|
|
|
|
ActiveSupport::Deprecation.warn "SimpleForm.translate is disabled in favor of translate_labels"
|
|
|
|
self.translate_labels
|
2011-09-04 05:31:24 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Default way to setup SimpleForm. Run rails generate simple_form:install
|
|
|
|
# to create a fresh initializer with all configuration values.
|
|
|
|
def self.setup
|
|
|
|
yield self
|
|
|
|
|
2011-12-08 03:47:18 -05:00
|
|
|
unless @@deprecated.empty?
|
|
|
|
raise "[SIMPLE FORM] Your simple form initializer file is using the following methods: #{@@deprecated.to_sentence}. " <<
|
|
|
|
"Those methods are part of the outdated configuration API. Updating to the new API is easy and fast. " <<
|
|
|
|
"Check for more info here: https://github.com/plataformatec/simple_form/wiki/Upgrading-to-Simple-Form-2.0"
|
2011-09-04 05:31:24 -04:00
|
|
|
end
|
|
|
|
end
|
2011-09-26 21:47:06 -04:00
|
|
|
end
|