heartcombo--simple_form/lib/simple_form/inputs/base.rb

155 lines
5.2 KiB
Ruby
Raw Normal View History

module SimpleForm
module Inputs
class Base
extend I18nCache
include SimpleForm::Helpers::Autofocus
include SimpleForm::Helpers::Disabled
include SimpleForm::Helpers::Readonly
2011-12-04 12:00:39 +00:00
include SimpleForm::Helpers::Required
include SimpleForm::Helpers::Validators
include SimpleForm::Components::Errors
include SimpleForm::Components::Hints
2011-12-04 15:15:27 +00:00
include SimpleForm::Components::HTML5
include SimpleForm::Components::LabelInput
2011-12-04 15:31:13 +00:00
include SimpleForm::Components::MinMax
2011-12-04 12:14:39 +00:00
include SimpleForm::Components::Maxlength
2011-12-04 12:40:00 +00:00
include SimpleForm::Components::Pattern
include SimpleForm::Components::Placeholders
2011-12-05 01:41:54 +00:00
include SimpleForm::Components::Readonly
attr_reader :attribute_name, :column, :input_type, :reflection,
2011-12-04 11:15:22 +00:00
:options, :input_html_options, :input_html_classes
delegate :template, :object, :object_name, :lookup_model_names, :lookup_action, :to => :@builder
class_attribute :default_options
self.default_options = {}
def self.enable(*keys)
self.ability(keys, true)
end
def self.disable(*keys)
self.ability(keys, false)
end
def self.ability(keys, value)
options = self.default_options.dup
keys.each { |key| options[key] = value }
self.default_options = options
end
# Always enabled.
enable :hint
# 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
def initialize(builder, attribute_name, column, input_type, options = {})
2011-12-04 15:15:27 +00:00
super
@builder = builder
@attribute_name = attribute_name
@column = column
@input_type = input_type
@reflection = options.delete(:reflection)
2011-12-04 15:31:13 +00:00
@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.
@input_html_classes = [input_type, required_class, readonly_class, disabled_class].compact
@input_html_options = html_options_for(:input, input_html_classes).tap do |o|
o[:readonly] = true if has_readonly?
o[:disabled] = true if has_disabled?
o[:autofocus] = true if has_autofocus?
end
end
def input
raise NotImplementedError
end
def input_options
options
end
private
def add_size!
input_html_options[:size] ||= [limit, SimpleForm.default_input_size].compact.min
end
def limit
column && column.limit
end
# Find reflection name when available, otherwise use attribute
def reflection_or_attribute_name
reflection ? reflection.name : attribute_name
end
# Retrieve options for the given namespace from the options hash
def html_options_for(namespace, extra)
html_options = options[:"#{namespace}_html"] || {}
2011-12-04 11:15:22 +00:00
html_options[:class] = (extra << html_options[:class]) if extra.present?
html_options
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}
# simple_form.{namespace}.{attribute}
#
# 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.
#
# 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}
# simple_form.{namespace}.{attribute}
#
# 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='')
model_names = lookup_model_names.dup
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
lookups << :"#{attribute_name}" unless reflection
lookups << default
I18n.t(lookups.shift, :scope => :"simple_form.#{namespace}", :default => lookups).presence
end
end
end
end