Module: ActiveAdmin::Globalize::FormBuilderExtension

Extended by:
ActiveSupport::Concern
Defined in:
lib/active_admin/globalize/form_builder_extension.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#translated_inputs(name = "Translations", options = {}, &block) ⇒ Object



6
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
# File 'lib/active_admin/globalize/form_builder_extension.rb', line 6

def translated_inputs(name = "Translations", options = {}, &block)
  options.symbolize_keys!
  available_locales = options.fetch(:available_locales, I18n.available_locales)
  switch_locale = options.fetch(:switch_locale, false)
  default_locale = options.fetch(:default_locale, I18n.default_locale)
  if block_given?
    # If this translated_inputs is nested inside an inputs, AA has already set this
    template.assigns[:has_many_block] = true
  end
  template.(:div, class: "activeadmin-translations") do
    template.(:ul, class: "available-locales") do
      available_locales.map do |locale|
        default = 'default' if locale == default_locale
        template.(:li) do
          I18n.with_locale(switch_locale ? locale : I18n.locale) do
            template.(:a, I18n.t(:"active_admin.globalize.language.#{locale}"), href:".locale-#{locale}", :class => default)
          end
        end
      end.join.html_safe
    end <<
    available_locales.map do |locale|
      translation = object.translations.find { |t| t.locale.to_s == locale.to_s }
      translation ||= object.translations.build(locale: locale)
      fields = proc do |form|
        form.input(:locale, as: :hidden)
        form.input(:id, as: :hidden)
        I18n.with_locale(switch_locale ? locale : I18n.locale) do
          block.call(form)
        end
      end
      inputs_for_nested_attributes(
        for: [:translations, translation ],
        class: "inputs locale locale-#{translation.locale}",
        &fields
      )
    end.join.html_safe
  end
end