Module: SimpleFormLocalizedInput::FormBuilder

Includes:
SimpleForm::Helpers::Validators
Defined in:
lib/simple_form_localized_input/form_builder.rb

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/simple_form_localized_input/form_builder.rb', line 7

def self.included(base)
  base.class_eval do
    def localized_input(attribute_name, options = {})
      field_name = [attribute_name, 'translations'].join('_')
      field_value = OpenStruct.new(object.send(field_name))

      simple_fields_for(field_name, field_value) do |fields|
        ::I18n.available_locales.collect do |loc|
          collection = options[:collection_translations] ? options[:collection_translations][loc.to_s] : options[:collection]
          label = [object.class.human_attribute_name(attribute_name), "(#{loc})"].reject(&:blank?).join(' ')
          required = object.class.validators_on(attribute_name).any? { |v| v.kind == :presence && valid_validator?(v) }

          fields.input(loc.to_sym, options.merge(collection: collection, label: label, required: required))
        end.join.html_safe
      end
    end
  end
end