class SimpleForm::Inputs::Base

Attributes

attribute_name[R]
column[R]
html_classes[R]
input_html_classes[R]
input_html_options[R]
input_type[R]
options[R]
reflection[R]

Public Class Methods

disable(*keys) click to toggle source
# File lib/simple_form/inputs/base.rb, line 43
def self.disable(*keys)
  options = self.default_options.dup
  keys.each { |key| options[key] = false }
  self.default_options = options
end
enable(*keys) click to toggle source
# File lib/simple_form/inputs/base.rb, line 37
def self.enable(*keys)
  options = self.default_options.dup
  keys.each { |key| options.delete(key) }
  self.default_options = options
end
new(builder, attribute_name, column, input_type, options = {}) click to toggle source
Calls superclass method SimpleForm::Components::HTML5.new
# File lib/simple_form/inputs/base.rb, line 55
def initialize(builder, attribute_name, column, input_type, options = {})
  super

  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

  # 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.
  @html_classes = SimpleForm.additional_classes_for(:input) { additional_classes }

  @input_html_classes = @html_classes.dup
  if SimpleForm.input_class && !input_html_classes.empty?
    input_html_classes << SimpleForm.input_class
  end

  @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

Public Instance Methods

additional_classes() click to toggle source
# File lib/simple_form/inputs/base.rb, line 92
def additional_classes
  @additional_classes ||= [input_type, required_class, readonly_class, disabled_class].compact
end
input(wrapper_options = nil) click to toggle source
# File lib/simple_form/inputs/base.rb, line 84
def input(wrapper_options = nil)
  raise NotImplementedError
end
input_class() click to toggle source
# File lib/simple_form/inputs/base.rb, line 96
def input_class
  "#{lookup_model_names.join("_")}_#{reflection_or_attribute_name}"
end
input_options() click to toggle source
# File lib/simple_form/inputs/base.rb, line 88
def input_options
  options
end

Private Instance Methods

column_limit() click to toggle source
# File lib/simple_form/inputs/base.rb, line 108
def column_limit
  column.limit
end
decimal_limit() click to toggle source

Add one for decimal point

# File lib/simple_form/inputs/base.rb, line 113
def decimal_limit
  column_limit && (column_limit + 1)
end
decimal_or_float?() click to toggle source
# File lib/simple_form/inputs/base.rb, line 117
def decimal_or_float?
  column.number? && column.type != :integer
end
html_options_for(namespace, css_classes) click to toggle source

Retrieve options for the given namespace from the options hash

# File lib/simple_form/inputs/base.rb, line 131
def html_options_for(namespace, css_classes)
  html_options = options[:"#{namespace}_html"]
  html_options = html_options ? html_options.dup : {}
  css_classes << html_options[:class] if html_options.key?(:class)
  html_options[:class] = css_classes unless css_classes.empty?
  html_options
end
i18n_scope() click to toggle source
# File lib/simple_form/inputs/base.rb, line 202
def i18n_scope
  SimpleForm.i18n_scope
end
limit() click to toggle source
# File lib/simple_form/inputs/base.rb, line 102
def limit
  if column
    decimal_or_float? ? decimal_limit : column_limit
  end
end
merge_wrapper_options(options, wrapper_options) click to toggle source
# File lib/simple_form/inputs/base.rb, line 190
def merge_wrapper_options(options, wrapper_options)
  if wrapper_options
    options.merge(wrapper_options) do |_, oldval, newval|
      if Array === oldval
        oldval + Array(newval)
      end
    end
  else
    options
  end
end
nested_boolean_style?() click to toggle source
# File lib/simple_form/inputs/base.rb, line 121
def nested_boolean_style?
  options.fetch(:boolean_style, SimpleForm.boolean_style) == :nested
end
reflection_or_attribute_name() click to toggle source

Find reflection name when available, otherwise use attribute

# File lib/simple_form/inputs/base.rb, line 126
def reflection_or_attribute_name
  @reflection_or_attribute_name ||= reflection ? reflection.name : attribute_name
end
translate_from_namespace(namespace, default = '') click to toggle source

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}.defaults.{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}.defaults.{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.
# File lib/simple_form/inputs/base.rb, line 172
def translate_from_namespace(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 << :"defaults.#{lookup_action}.#{reflection_or_attribute_name}"
  lookups << :"defaults.#{reflection_or_attribute_name}"
  lookups << default

  I18n.t(lookups.shift, scope: :"#{i18n_scope}.#{namespace}", default: lookups).presence
end