Class: ActiveAdmin::Views::AttributesTable

Inherits:
Component
  • Object
show all
Defined in:
lib/active_admin/globalize/attributes_table_extension.rb

Overview

Provide additional method #translated_row to attribute table

Instance Method Summary collapse

Instance Method Details

#translated_row(field, opts) ⇒ Object

Show a row with their translations and selectors for choosing locale to show.

If a block is given it will be used to format the translation, otherwise field taken from translation object is used as translation.

Additional options are forwarded to the original row call.

end

end

Examples:

Show inlined translation values for title field

show do |p|
  attributes_table do
    translated_row(:title)
  end

Show block translation for body field with selection of initial locale

show do |p|
  attributes_table do
    translated_row(:body, inline: false, locale: :es)
  end

Parameters:

  • field (String)

    row label, also used as field name if none given in options

  • opts (Hash)

    the options to create a message with. @option opts [String] :field field to retrieve from model if different from first argument of args @option opts [String] :locale (I18n.locale) initial locale to show in the ui @option opts [Boolean] :inline (true) if true locale selectors (and values) are shown inline,

    otherwise as block content and tabs
    

Yields:

  • if given will be used to build single translations

Yield Parameters:

  • Globalize (*::Translation)

    translation model

Yield Returns:

  • (String)

    content to show as translation

Raises:

  • (ArgumentError)


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/active_admin/globalize/attributes_table_extension.rb', line 42

def translated_row(*args, &block)
  options = args.extract_options!
  options.reverse_merge!(inline: true, locale: I18n.locale)
  field = options[:field] || args.first
  raise ArgumentError, "Field '#{field}' is not translatable" unless translatable?(field)
  # Remove my options from passed options
  row_options = options.symbolize_keys.except(:field, :locale, :inline)
  # Append remaining options to original args
  args.push(row_options) unless row_options.empty?
  # Render the table row with translations
  row(*args) do
    if options[:inline]
      ''.html_safe.tap do |value|
        # Add selectors for inline locale
        value << inline_locale_selectors
        # Build translations spans
        value << field_translations(field, :span, options[:locale], &block)
      end
    else
      (:div, class: 'activeadmin-translations') do
        ''.html_safe.tap do |value|
          # Render selectors as in translation ui
          value << block_locale_selectors
          # Build translations divs for actual translations
          value << field_translations(field, :div, options[:locale], &block)
        end
      end
    end
  end
end