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

def translated_inputs(name = "Translations", options = {}, &block)
  options.symbolize_keys!
  switch_locale = options.fetch(:switch_locale, false)
  auto_sort = options.fetch(:auto_sort, true)
  form_buffers.last << template.(:div, class: "activeadmin-translations") do
    template.(:ul, class: "available-locales") do
      (auto_sort ? I18n.available_locales.sort : I18n.available_locales).map do |locale|
        default = 'default' if locale == I18n.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 <<
    (auto_sort ? I18n.available_locales.sort : I18n.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