Module: Spree::Translations

Defined in:
lib/spree/translations.rb,
app/models/spree/translations/batch.rb

Overview

Builds the admin translation matrix, field-discovery metadata, and the public translatable-resource registry for records in Spree.translatable_resources. Stateless helper shared by the dedicated translations endpoint, the ?expand=translations serializer attribute, the batch write controller, and the discovery endpoint.

Defined Under Namespace

Classes: Batch

Class Method Summary collapse

Class Method Details

.field_type(klass, field) ⇒ String

The editor type for a translatable field: html when the model declares it as rich text (drives a rich-text editor in the SPA), else string.

Parameters:

  • klass (Class)

    a translatable model

  • field (String, Symbol)

    public field name

Returns:

  • (String)


76
77
78
# File 'lib/spree/translations.rb', line 76

def field_type(klass, field)
  klass.translatable_rich_text_fields.map(&:to_sym).include?(field.to_sym) ? 'html' : 'string'
end

.fields_for(record) ⇒ Array<Hash>

Returns [{ "key" => "name", "type" => "string", "source" => "Espresso Machine" }, ...].

Returns:

  • (Array<Hash>)

    [{ "key" => "name", "type" => "string", "source" => "Espresso Machine" }, ...]



24
25
26
27
28
29
30
# File 'lib/spree/translations.rb', line 24

def fields_for(record)
  Mobility.with_locale(default_locale(record)) do
    field_keys(record).map do |field|
      { 'key' => field, 'type' => field_type(record.class, field), 'source' => record.public_send(field) }
    end
  end
end

.matrix_for(record, locales: nil) ⇒ Hash{String=>Hash}

Returns locale => { field => value, "translated_field_count" => Integer }.

Returns:

  • (Hash{String=>Hash})

    locale => { field => value, "translated_field_count" => Integer }



13
14
15
16
17
18
19
20
21
# File 'lib/spree/translations.rb', line 13

def matrix_for(record, locales: nil)
  fields = field_keys(record)
  locales ||= non_default_locales(record)

  locales.index_with do |locale|
    translated = field_values(record, locale, fields)
    translated.merge('translated_field_count' => translated.count { |_k, v| v.present? })
  end
end

.public_resource_type(klass) ⇒ String

The PUBLIC resource-type token used in read (document/registry) and write (batch) payloads. Taxon is exposed as "category" (routes use the 5.5 rename) while the model element stays "taxon"; this keeps the read and write contracts consistent.

Returns:

  • (String)


53
54
55
56
57
# File 'lib/spree/translations.rb', line 53

def public_resource_type(klass)
  return 'category' if klass <= Spree::Taxon

  resource_type(klass)
end

.registryArray<Hash>

Returns registry made public: [{ "resource_type" => "product", "fields" => [key,type] }].

Returns:

  • (Array<Hash>)

    registry made public: [{ "resource_type" => "product", "fields" => [key,type] }]



33
34
35
36
37
38
39
40
# File 'lib/spree/translations.rb', line 33

def registry
  Spree.translatable_resources.map do |klass|
    {
      'resource_type' => public_resource_type(klass),
      'fields' => klass.public_translatable_fields.map { |f| { 'key' => f.to_s, 'type' => field_type(klass, f) } }
    }
  end
end

.resource_class(token) ⇒ Class?

Inverse of public_resource_type: maps a public token to its registered translatable class, or nil if not translatable.

Parameters:

  • token (String, Symbol)

    e.g. "product", "option_value", "category"

Returns:

  • (Class, nil)


64
65
66
67
68
69
# File 'lib/spree/translations.rb', line 64

def resource_class(token)
  # Recomputed per call (not memoized) so a dev-mode class reload of a
  # registry member doesn't leave a stale class reference behind.
  map = Spree.translatable_resources.index_by { |klass| public_resource_type(klass) }
  map[token.to_s]
end

.resource_type(klass) ⇒ String

Returns underscored, demodulized model name (e.g. "option_type").

Returns:

  • (String)

    underscored, demodulized model name (e.g. "option_type")



43
44
45
# File 'lib/spree/translations.rb', line 43

def resource_type(klass)
  klass.name.demodulize.underscore
end