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'
2011-12-05 15:26:49 +00: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 18:06:59 +00:00
2009-12-09 20:25:12 +00:00
module SimpleForm
2013-01-02 16:11:46 +00:00
extend ActiveSupport :: Autoload
autoload :Helpers
autoload :Wrappers
eager_autoload do
autoload :Components
autoload :ErrorNotification
autoload :FormBuilder
autoload :Inputs
end
def self . eager_load!
super
SimpleForm :: Inputs . eager_load!
SimpleForm :: Components . eager_load!
end
2009-12-10 01:57:05 +00:00
2014-03-11 19:06:45 +00:00
CUSTOM_INPUT_DEPRECATION_WARN = <<-WARN
2014-03-11 22:41:25 +00:00
%{ name } method now accepts a ` wrapper_options ` argument . The method definition without the argument is deprecated and will be removed in the next Simple Form version . Change your code from :
2014-03-11 19:06:45 +00:00
def % { name }
to
2014-03-11 22:41:25 +00:00
def % { name } ( wrapper_options )
2014-03-11 22:04:41 +00:00
See https : / / github . com / plataformatec / simple_form / pull / 997 for more information .
2014-03-11 19:06:45 +00:00
WARN
2014-04-02 21:22:02 +00:00
@@configured = false
def self . configured? #:nodoc:
@@configured
end
2011-09-04 09:31:24 +00:00
## CONFIGURATION OPTIONS
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
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
2013-01-30 13:36:44 +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
2013-01-30 13:36:44 +00:00
@@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
2012-01-27 18:09:45 +00:00
# You can define the class to use on all collection wrappers, defaulting to none.
2011-10-10 17:06:53 +00:00
mattr_accessor :collection_wrapper_class
@@collection_wrapper_class = nil
2012-01-30 13:27:17 +00:00
# You can wrap each item in a collection of radio/check boxes with a tag,
2013-01-30 13:36:44 +00:00
# defaulting to span. Please note that when using :boolean_style = :nested,
2012-01-30 13:27:17 +00:00
# SimpleForm will force this option to be a :label.
2010-11-21 16:51:49 +00:00
mattr_accessor :item_wrapper_tag
2012-01-27 18:49:06 +00:00
@@item_wrapper_tag = :span
2012-01-27 18:09:45 +00:00
# You can define the class to use on all item wrappers, defaulting to none.
mattr_accessor :item_wrapper_class
@@item_wrapper_class = nil
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
2014-04-02 23:09:22 +00:00
@@label_text = lambda { | label , required , explicit_label | " #{ required } #{ label } " }
2009-12-11 13:12:57 +00:00
2013-01-30 13:36:44 +00:00
# You can define the class to be used on all labels. Defaults to none.
2011-02-07 23:42:18 +00:00
mattr_accessor :label_class
@@label_class = nil
2012-01-26 20:20:41 +00:00
# Define the way to render check boxes / radio buttons with labels.
2013-01-28 21:02:59 +00:00
# inline: input + label (default)
# nested: label > input
2012-01-26 17:06:33 +00:00
mattr_accessor :boolean_style
@@boolean_style = :inline
2012-01-26 13:30:08 +00:00
2014-07-23 19:03:13 +00:00
# DEPRECATED: You can define the class to be used on all forms. Default is
# simple_form.
2014-11-19 16:30:55 +00:00
mattr_reader :form_class
2011-03-07 22:13:28 +00:00
@@form_class = :simple_form
2014-07-23 19:03:13 +00:00
# You can define the default class to be used on all forms. Can be overriden
# with `html: { :class }`. Defaults to none.
mattr_accessor :default_form_class
@@default_form_class = nil
2013-01-30 13:36:44 +00:00
# You can define which elements should obtain additional classes.
2012-02-16 17:42:47 +00:00
mattr_accessor :generate_additional_classes_for
@@generate_additional_classes_for = [ :wrapper , :label , :input ]
2012-02-13 22:22:49 +00:00
2013-01-30 13:36:44 +00:00
# Whether attributes are required by default or not.
2010-07-06 09:28:23 +00:00
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
2009-12-11 13:12:57 +00:00
# Collection of methods to detect if a file type was given.
mattr_accessor :file_methods
2013-01-30 13:36:44 +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
2012-08-11 12:52:10 +00:00
# Custom wrappers for input types. This should be a hash containing an input
2012-09-07 19:14:40 +00:00
# type as key and the wrapper that will be used for all inputs with specified type.
2013-01-28 21:02:59 +00:00
# e.g { string: :string_wrapper, boolean: :boolean_wrapper }
2013-11-12 22:59:30 +00:00
# You can also set a wrapper mapping per form basis.
2014-03-21 17:19:31 +00:00
# e.g simple_form_for(@foo, wrapper_mappings: { check_boxes: :bootstrap_checkbox })
2012-08-11 12:52:10 +00:00
mattr_accessor :wrapper_mappings
@@wrapper_mappings = nil
2014-06-05 10:46:12 +00:00
# Namespaces where SimpleForm should look for custom input classes that override
2014-06-26 20:54:06 +00:00
# default inputs. Namespaces are given as string to allow lazy loading inputs.
2014-06-05 10:46:12 +00:00
# e.g. config.custom_inputs_namespaces << "CustomInputs"
# will try to find CustomInputs::NumericInput when an :integer
2014-06-26 20:54:06 +00:00
# field is called.
2014-06-05 10:46:12 +00:00
mattr_accessor :custom_inputs_namespaces
@@custom_inputs_namespaces = [ ]
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
2012-12-27 21:39:32 +00:00
# DEPRECATED: Maximum size allowed for inputs.
2010-02-06 21:06:25 +00:00
mattr_accessor :default_input_size
2012-12-27 21:39:32 +00:00
@@default_input_size = nil
2010-02-06 21:06:25 +00:00
2011-12-04 14:49:57 +00: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 06:12:45 +00:00
2011-04-27 18:55:03 +00:00
# Automatically discover new inputs in Rails' autoload path.
mattr_accessor :inputs_discovery
@@inputs_discovery = true
2013-01-30 13:36:44 +00:00
# Cache SimpleForm inputs discovery.
2011-04-27 18:55:03 +00:00
mattr_accessor :cache_discovery
2012-01-11 02:56:42 +00:00
@@cache_discovery = defined? ( Rails ) && ! Rails . env . development?
2011-04-27 18:55:03 +00:00
2013-01-30 13:36:44 +00:00
# Adds a class to each generated button, mostly for compatiblity.
2011-09-12 02:14:39 +00:00
mattr_accessor :button_class
@@button_class = 'button'
2013-04-29 15:13:33 +00:00
# Override the default ActiveModelHelper behaviour of wrapping the input.
2013-03-19 14:27:11 +00:00
# This gets taken care of semantically by adding an error class to the wrapper tag
# containing the input.
mattr_accessor :field_error_proc
@@field_error_proc = proc do | html_tag , instance_tag |
html_tag
end
2011-09-27 02:13:06 +00:00
# Adds a class to each generated inputs
mattr_accessor :input_class
@@input_class = nil
2013-11-12 22:18:52 +00:00
# Defines if an input wrapper class should be included or not
mattr_accessor :include_default_input_wrapper_class
@@include_default_input_wrapper_class = true
2014-03-12 20:09:02 +00:00
# Define the default class of the input wrapper of the boolean input.
mattr_accessor :boolean_label_class
@@boolean_label_class = 'checkbox'
2011-09-04 09:31:24 +00:00
## WRAPPER CONFIGURATION
2012-01-24 18:54:28 +00:00
# The default wrapper to be used by the FormBuilder.
mattr_accessor :default_wrapper
@@default_wrapper = :default
2014-04-02 21:31:16 +00:00
@@wrappers = { } #:nodoc:
2011-09-04 09:12:16 +00:00
2014-04-04 14:00:47 +00:00
mattr_accessor :i18n_scope
@@i18n_scope = 'simple_form'
2011-09-04 09:31:24 +00:00
# Retrieves a given wrapper
def self . wrapper ( name )
2014-03-14 14:51:59 +00:00
@@wrappers [ name . to_s ] or raise WrapperNotFound , " Couldn't find wrapper with name #{ name } "
2011-09-27 01:47:06 +00:00
end
# Raised when fails to find a given wrapper name
class WrapperNotFound < StandardError
2011-09-04 09:12:16 +00:00
end
2011-09-04 09:31:24 +00: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-27 02:41:53 +00:00
options = args . extract_options!
name = args . first || :default
2014-03-14 14:51:59 +00:00
@@wrappers [ name . to_s ] = build ( options , & block )
2011-09-04 09:31:24 +00:00
else
@@wrappers
2011-09-04 09:12:16 +00:00
end
2009-12-11 20:23:00 +00:00
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.
2014-03-11 22:12:08 +00:00
def self . build ( options = { } )
2011-11-09 22:02:27 +00:00
options [ :tag ] = :div if options [ :tag ] . nil?
2012-01-11 20:12:49 +00:00
builder = SimpleForm :: Wrappers :: Builder . new ( options )
2011-09-03 08:33:57 +00:00
yield builder
2011-09-03 08:45:10 +00:00
SimpleForm :: Wrappers :: Root . new ( builder . to_a , options )
end
2013-01-28 21:02:59 +00:00
wrappers class : :input , hint_class : :field_with_hint , error_class : :field_with_errors do | b |
2011-12-04 15:15:27 +00:00
b . use :html5
2011-12-04 15:31:13 +00:00
b . use :min_max
2011-12-04 12:14:39 +00:00
b . use :maxlength
2011-09-03 08:33:57 +00:00
b . use :placeholder
2012-01-11 20:12:49 +00:00
b . optional :pattern
b . optional :readonly
2011-12-04 11:15:22 +00:00
2011-09-03 08:33:57 +00:00
b . use :label_input
2013-01-28 21:02:59 +00:00
b . use :hint , wrap_with : { tag : :span , class : :hint }
b . use :error , wrap_with : { tag : :span , class : :error }
2011-09-03 08:33:57 +00:00
end
2011-09-04 09:31:24 +00:00
2012-02-17 00:36:34 +00:00
def self . additional_classes_for ( component )
generate_additional_classes_for . include? ( component ) ? yield : [ ]
end
2012-12-27 03:40:58 +00:00
## SETUP
2012-12-27 21:39:32 +00:00
def self . default_input_size = ( * )
ActiveSupport :: Deprecation . warn " [SIMPLE_FORM] SimpleForm.default_input_size= is deprecated and has no effect " , caller
end
2014-07-23 19:03:13 +00:00
def self . form_class = ( value )
ActiveSupport :: Deprecation . warn " [SIMPLE_FORM] SimpleForm.form_class= is deprecated and will be removed in 4.x. Use SimpleForm.default_form_class= instead " , caller
@@form_class = value
end
2014-04-02 21:31:35 +00:00
# Default way to setup Simple Form. Run rails generate simple_form:install
2011-09-04 09:31:24 +00:00
# to create a fresh initializer with all configuration values.
def self . setup
2014-04-02 21:22:02 +00:00
@@configured = true
2011-09-04 09:31:24 +00:00
yield self
end
2011-09-27 01:47:06 +00:00
end
2013-01-02 16:11:46 +00:00
require 'simple_form/railtie' if defined? ( Rails )