2013-01-02 17:02:41 +00:00
|
|
|
require 'simple_form/i18n_cache'
|
2013-11-27 01:33:24 +00:00
|
|
|
require 'active_support/core_ext/string/output_safety'
|
2013-01-02 17:02:41 +00:00
|
|
|
|
2010-01-09 15:05:02 +00:00
|
|
|
module SimpleForm
|
|
|
|
module Inputs
|
|
|
|
class Base
|
2013-11-27 01:33:24 +00:00
|
|
|
include ERB::Util
|
|
|
|
|
2010-01-09 15:05:02 +00:00
|
|
|
extend I18nCache
|
|
|
|
|
2011-12-04 13:57:12 +00:00
|
|
|
include SimpleForm::Helpers::Autofocus
|
|
|
|
include SimpleForm::Helpers::Disabled
|
2011-11-26 09:38:47 +00:00
|
|
|
include SimpleForm::Helpers::Readonly
|
2011-12-04 12:00:39 +00:00
|
|
|
include SimpleForm::Helpers::Required
|
2011-09-03 10:00:23 +00:00
|
|
|
include SimpleForm::Helpers::Validators
|
|
|
|
|
2010-01-09 15:05:02 +00:00
|
|
|
include SimpleForm::Components::Errors
|
|
|
|
include SimpleForm::Components::Hints
|
2011-12-04 15:15:27 +00:00
|
|
|
include SimpleForm::Components::HTML5
|
2010-07-06 09:24:12 +00:00
|
|
|
include SimpleForm::Components::LabelInput
|
2011-12-04 12:14:39 +00:00
|
|
|
include SimpleForm::Components::Maxlength
|
2011-12-10 18:07:28 +00:00
|
|
|
include SimpleForm::Components::MinMax
|
2011-12-04 12:40:00 +00:00
|
|
|
include SimpleForm::Components::Pattern
|
2010-11-07 09:41:33 +00:00
|
|
|
include SimpleForm::Components::Placeholders
|
2011-12-05 01:41:54 +00:00
|
|
|
include SimpleForm::Components::Readonly
|
2010-01-09 15:05:02 +00:00
|
|
|
|
2010-12-08 02:25:14 +00:00
|
|
|
attr_reader :attribute_name, :column, :input_type, :reflection,
|
2011-12-05 18:03:41 +00:00
|
|
|
:options, :input_html_options, :input_html_classes, :html_classes
|
2010-01-09 15:05:02 +00:00
|
|
|
|
2013-01-28 21:02:59 +00:00
|
|
|
delegate :template, :object, :object_name, :lookup_model_names, :lookup_action, to: :@builder
|
2010-10-18 21:14:07 +00:00
|
|
|
|
2011-12-04 12:29:59 +00:00
|
|
|
class_attribute :default_options
|
|
|
|
self.default_options = {}
|
|
|
|
|
|
|
|
def self.enable(*keys)
|
2012-01-11 20:12:49 +00:00
|
|
|
options = self.default_options.dup
|
|
|
|
keys.each { |key| options.delete(key) }
|
|
|
|
self.default_options = options
|
2011-12-04 12:29:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.disable(*keys)
|
|
|
|
options = self.default_options.dup
|
2012-01-11 20:12:49 +00:00
|
|
|
keys.each { |key| options[key] = false }
|
2011-12-04 12:29:59 +00:00
|
|
|
self.default_options = options
|
|
|
|
end
|
|
|
|
|
2011-12-04 14:49:57 +00:00
|
|
|
# Always enabled.
|
|
|
|
enable :hint
|
|
|
|
|
2011-12-04 13:57:12 +00:00
|
|
|
# Usually disabled, needs to be enabled explicitly passing true as option.
|
2011-12-04 15:31:13 +00:00
|
|
|
disable :maxlength, :placeholder, :pattern, :min_max
|
2011-12-04 12:29:59 +00:00
|
|
|
|
2010-10-18 21:14:07 +00:00
|
|
|
def initialize(builder, attribute_name, column, input_type, options = {})
|
2011-12-04 15:15:27 +00:00
|
|
|
super
|
|
|
|
|
2013-11-06 04:52:30 +00:00
|
|
|
options = options.dup
|
|
|
|
@builder = builder
|
|
|
|
@attribute_name = attribute_name
|
|
|
|
@column = column
|
|
|
|
@input_type = input_type
|
|
|
|
@reflection = options.delete(:reflection)
|
|
|
|
@options = options.reverse_merge!(self.class.default_options)
|
|
|
|
@required = calculate_required
|
2011-12-04 11:15:22 +00:00
|
|
|
|
|
|
|
# Notice that html_options_for receives a reference to input_html_classes.
|
|
|
|
# This means that classes added dynamically to input_html_classes will
|
|
|
|
# still propagate to input_html_options.
|
2012-07-21 21:38:57 +00:00
|
|
|
@html_classes = SimpleForm.additional_classes_for(:input) { additional_classes }
|
2012-02-13 22:22:49 +00:00
|
|
|
|
2011-12-05 18:03:41 +00:00
|
|
|
@input_html_classes = @html_classes.dup
|
2013-08-16 11:11:31 +00:00
|
|
|
if SimpleForm.input_class && !input_html_classes.empty?
|
|
|
|
input_html_classes << SimpleForm.input_class
|
|
|
|
end
|
|
|
|
|
2010-11-07 09:41:33 +00:00
|
|
|
@input_html_options = html_options_for(:input, input_html_classes).tap do |o|
|
2011-11-26 09:38:47 +00:00
|
|
|
o[:readonly] = true if has_readonly?
|
2011-12-04 13:57:12 +00:00
|
|
|
o[:disabled] = true if has_disabled?
|
2011-09-03 19:48:48 +00:00
|
|
|
o[:autofocus] = true if has_autofocus?
|
2010-11-07 09:41:33 +00:00
|
|
|
end
|
2010-01-09 15:05:02 +00:00
|
|
|
end
|
|
|
|
|
2014-03-10 21:41:36 +00:00
|
|
|
def input(context)
|
2010-01-09 15:05:02 +00:00
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
def input_options
|
|
|
|
options
|
|
|
|
end
|
|
|
|
|
2012-07-21 21:38:57 +00:00
|
|
|
def additional_classes
|
2013-08-16 11:11:31 +00:00
|
|
|
@additional_classes ||= [input_type, required_class, readonly_class, disabled_class].compact
|
2012-07-21 21:38:57 +00:00
|
|
|
end
|
|
|
|
|
2012-10-07 03:31:57 +00:00
|
|
|
def input_class
|
2012-10-08 01:09:57 +00:00
|
|
|
"#{lookup_model_names.join("_")}_#{reflection_or_attribute_name}"
|
2012-10-07 03:31:57 +00:00
|
|
|
end
|
|
|
|
|
2011-09-03 10:00:23 +00:00
|
|
|
private
|
|
|
|
|
2011-09-02 16:02:39 +00:00
|
|
|
def limit
|
2012-04-21 12:10:36 +00:00
|
|
|
if column
|
|
|
|
decimal_or_float? ? decimal_limit : column_limit
|
|
|
|
end
|
2012-04-04 19:05:01 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def column_limit
|
2012-04-21 12:10:36 +00:00
|
|
|
column.limit
|
2011-09-02 16:02:39 +00:00
|
|
|
end
|
|
|
|
|
2012-04-21 12:10:36 +00:00
|
|
|
# Add one for decimal point
|
2012-04-04 19:05:01 +00:00
|
|
|
def decimal_limit
|
2012-04-21 12:10:36 +00:00
|
|
|
column_limit && (column_limit + 1)
|
2012-04-04 19:05:01 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def decimal_or_float?
|
2012-04-21 12:10:36 +00:00
|
|
|
column.number? && column.type != :integer
|
2012-04-04 19:05:01 +00:00
|
|
|
end
|
|
|
|
|
2012-01-26 18:17:12 +00:00
|
|
|
def nested_boolean_style?
|
|
|
|
options.fetch(:boolean_style, SimpleForm.boolean_style) == :nested
|
|
|
|
end
|
|
|
|
|
2010-01-09 15:05:02 +00:00
|
|
|
# Find reflection name when available, otherwise use attribute
|
|
|
|
def reflection_or_attribute_name
|
2012-10-07 03:31:57 +00:00
|
|
|
@reflection_or_attribute_name ||= reflection ? reflection.name : attribute_name
|
2010-01-09 15:05:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Retrieve options for the given namespace from the options hash
|
2012-01-26 12:51:12 +00:00
|
|
|
def html_options_for(namespace, css_classes)
|
2012-04-21 22:41:44 +00:00
|
|
|
html_options = options[:"#{namespace}_html"]
|
|
|
|
html_options = html_options ? html_options.dup : {}
|
2012-01-26 12:51:12 +00:00
|
|
|
css_classes << html_options[:class] if html_options.key?(:class)
|
2012-06-29 19:13:15 +00:00
|
|
|
html_options[:class] = css_classes unless css_classes.empty?
|
2012-02-17 00:36:34 +00:00
|
|
|
html_options
|
2010-01-09 15:05:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Lookup translations for the given namespace using I18n, based on object name,
|
|
|
|
# actual action and attribute name. Lookup priority as follows:
|
|
|
|
#
|
|
|
|
# simple_form.{namespace}.{model}.{action}.{attribute}
|
|
|
|
# simple_form.{namespace}.{model}.{attribute}
|
2011-12-10 21:50:03 +00:00
|
|
|
# simple_form.{namespace}.defaults.{attribute}
|
2010-01-09 15:05:02 +00:00
|
|
|
#
|
|
|
|
# Namespace is used for :labels and :hints.
|
|
|
|
#
|
|
|
|
# Model is the actual object name, for a @user object you'll have :user.
|
|
|
|
# Action is the action being rendered, usually :new or :edit.
|
|
|
|
# And attribute is the attribute itself, :name for example.
|
|
|
|
#
|
2011-02-12 15:42:35 +00:00
|
|
|
# The lookup for nested attributes is also done in a nested format using
|
|
|
|
# both model and nested object names, such as follow:
|
|
|
|
#
|
|
|
|
# simple_form.{namespace}.{model}.{nested}.{action}.{attribute}
|
|
|
|
# simple_form.{namespace}.{model}.{nested}.{attribute}
|
|
|
|
# simple_form.{namespace}.{nested}.{action}.{attribute}
|
|
|
|
# simple_form.{namespace}.{nested}.{attribute}
|
2011-12-08 08:44:08 +00:00
|
|
|
# simple_form.{namespace}.defaults.{attribute}
|
2011-02-12 15:42:35 +00:00
|
|
|
#
|
2010-01-09 15:05:02 +00:00
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# simple_form:
|
|
|
|
# labels:
|
|
|
|
# user:
|
|
|
|
# new:
|
|
|
|
# email: 'E-mail para efetuar o sign in.'
|
|
|
|
# edit:
|
|
|
|
# email: 'E-mail.'
|
|
|
|
#
|
|
|
|
# Take a look at our locale example file.
|
|
|
|
def translate(namespace, default='')
|
2011-11-08 11:59:34 +00:00
|
|
|
model_names = lookup_model_names.dup
|
2011-02-12 15:42:35 +00:00
|
|
|
lookups = []
|
|
|
|
|
|
|
|
while !model_names.empty?
|
|
|
|
joined_model_names = model_names.join(".")
|
|
|
|
model_names.shift
|
|
|
|
|
|
|
|
lookups << :"#{joined_model_names}.#{lookup_action}.#{reflection_or_attribute_name}"
|
|
|
|
lookups << :"#{joined_model_names}.#{reflection_or_attribute_name}"
|
|
|
|
end
|
2011-12-10 21:50:03 +00:00
|
|
|
lookups << :"defaults.#{lookup_action}.#{reflection_or_attribute_name}"
|
2012-10-24 17:51:13 +00:00
|
|
|
lookups << :"defaults.#{reflection_or_attribute_name}"
|
2010-01-09 15:05:02 +00:00
|
|
|
lookups << default
|
2011-02-12 15:42:35 +00:00
|
|
|
|
2013-01-28 21:02:59 +00:00
|
|
|
I18n.t(lookups.shift, scope: :"simple_form.#{namespace}", default: lookups).presence
|
2010-01-09 15:05:02 +00:00
|
|
|
end
|
2014-03-11 13:30:49 +00:00
|
|
|
|
|
|
|
def merged_input_options(context_options)
|
|
|
|
input_html_options.merge(context_options) do |_, oldval, newval|
|
|
|
|
if Array === oldval
|
|
|
|
oldval + Array(newval)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2010-01-09 15:05:02 +00:00
|
|
|
end
|
|
|
|
end
|
2010-09-14 03:40:03 +00:00
|
|
|
end
|