Class: Mobility::TranslationsGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Defined in:
lib/rails/generators/mobility/translations_generator.rb

Overview

Generator to create translation tables or add translation columns to a model table, for either Table or Column backends.

Usage

To add translations for a string attribute title to a model Post, call the generator with:

rails generate mobility:translations post title:string

Here, the backend is implicit in the value of Mobility.default_backend, but it can be explicitly set using the backend option:

rails generate mobility:translations post title:string --backend=table

For the table backend, the generator will either create a translation table (in this case, post_translations) or add columns to the table if it already exists.

For the column backend, the generator will add columns for all locales in Mobility.available_locales. If some columns already exist, they will simply be skipped.

Other backends are not supported, for obvious reasons:

  • the key_value backend does not need any model-specific migrations, simply run the install generator.

  • json, jsonb, hstore, serialized, and container backends simply require a single column on a model table, which can be added with the normal Rails migration generator.

Constant Summary collapse

SUPPORTED_BACKENDS =
%w[column table].freeze
BACKEND_OPTIONS =
{ type: :string, desc: "Backend to use for translations (defaults to Mobility.default_backend)" }.freeze

Class Method Summary collapse

Class Method Details

.class_options(options = nil) ⇒ Object



45
46
47
48
49
# File 'lib/rails/generators/mobility/translations_generator.rb', line 45

def self.class_options(options = nil)
  super
  @class_options[:backend] = Thor::Option.new(:backend, BACKEND_OPTIONS.merge(default: Mobility.default_backend.to_s.freeze))
  @class_options
end

.prepare_for_invocation(name, value) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rails/generators/mobility/translations_generator.rb', line 51

def self.prepare_for_invocation(name, value)
  if name == :backend
    if SUPPORTED_BACKENDS.include?(value)
      require_relative "./backend_generators/#{value}_backend"
      Mobility::BackendGenerators.const_get("#{value}_backend".camelcase.freeze)
    else
      begin
        require "mobility/backends/#{value}"
        raise Thor::Error, "The #{value} backend does not have a translations generator."
      rescue LoadError => e
        raise unless e.message =~ /#{value}/
        raise Thor::Error, "#{value} is not a Mobility backend."
      end
    end
  else
    super
  end
end