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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 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]

          column = find_attribute_column(attribute_name)
          input_type = default_input_type(attribute_name, column, options)

          localized_label = SimpleForm::Inputs::Base.new(
            self,
            attribute_name,
            column,
            input_type,
            options
          ).send(:raw_label_text)

          required = object.class.validators_on(attribute_name).any? do |v|
            v.kind == :presence &&
              valid_validator?(v) &&
              valid_localized_presence_validator?(v, loc)
          end

          fields.input(
            loc.to_sym,
            options.merge(collection: collection, label: "#{localized_label} (#{loc})".html_safe, required: required)
          )
        end.join.html_safe
      end + error(attribute_name)
    end

    private

    def valid_localized_presence_validator?(validator, locale)
      in_option = validator.options.fetch(:in, nil)
      with_option = validator.options.fetch(:with, nil)
      return true unless in_option.present? || with_option.present?

      with_option = ::I18n.default_locale if with_option == :default_locale
      Array(in_option || with_option).map(&:to_s).include?(locale.to_s)
    end
  end
end