Parent

Files

SimpleForm::Inputs::Base

Constants

ACTIONS

When action is create or update, we still should use new and edit

Public Class Methods

new(builder) click to toggle source
# File lib/simple_form/inputs/base.rb, line 20
def initialize(builder)
  @builder = builder
end

Public Instance Methods

input() click to toggle source
# File lib/simple_form/inputs/base.rb, line 24
def input
  raise NotImplementedError
end
input_html_classes() click to toggle source
# File lib/simple_form/inputs/base.rb, line 36
def input_html_classes
  [input_type, required_class]
end
input_html_options() click to toggle source
# File lib/simple_form/inputs/base.rb, line 32
def input_html_options
  html_options_for(:input, input_html_classes)
end
input_options() click to toggle source
# File lib/simple_form/inputs/base.rb, line 28
def input_options
  options
end
render() click to toggle source
# File lib/simple_form/inputs/base.rb, line 40
def render
  content = "".html_safe
  components_list.each do |component|
    next if options[component] == false
    content.safe_concat send(component).to_s
  end
  wrap(content)
end

Protected Instance Methods

attribute_required?() click to toggle source
# File lib/simple_form/inputs/base.rb, line 55
def attribute_required?
  if options.key?(:required)
    options[:required]
  elsif object.class.respond_to?(:validators_on)
    object.class.validators_on(attribute_name).any? { |v| v.kind == :presence }
  else
    attribute_required_by_default?
  end
end
attribute_required_by_default?() click to toggle source
# File lib/simple_form/inputs/base.rb, line 65
def attribute_required_by_default?
  SimpleForm.required_by_default
end
components_list() click to toggle source
# File lib/simple_form/inputs/base.rb, line 51
def components_list
  SimpleForm.components
end
html_options_for(namespace, extra) click to toggle source

Retrieve options for the given namespace from the options hash

# File lib/simple_form/inputs/base.rb, line 79
def html_options_for(namespace, extra)
  html_options = options[:"#{namespace}_html"] || {}
  html_options[:class] = (extra << html_options[:class]).join(' ').strip if extra.present?
  html_options
end
lookup_action() click to toggle source

The action to be used in lookup.

# File lib/simple_form/inputs/base.rb, line 119
def lookup_action
  action = template.controller.action_name.to_sym
  ACTIONS[action] || action
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 74
def reflection_or_attribute_name
  reflection ? reflection.name : attribute_name
end
required_class() click to toggle source
# File lib/simple_form/inputs/base.rb, line 69
def required_class
  attribute_required? ? :required : :optional
end
translate(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}.{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.

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 109
def translate(namespace, default='')
  lookups = []
  lookups << :"#{object_name}.#{lookup_action}.#{reflection_or_attribute_name}"
  lookups << :"#{object_name}.#{reflection_or_attribute_name}"
  lookups << :"#{reflection_or_attribute_name}"
  lookups << default
  I18n.t(lookups.shift, :scope => :"simple_form.#{namespace}", :default => lookups)
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.