Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve translations generators #196

Merged
merged 5 commits into from
Mar 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ def change
<% I18n.available_locales.each do |locale| -%>
<% column_name = Mobility.normalize_locale_accessor(attribute.name, locale) -%>
<% if connection.column_exists?(table_name, column_name) -%>
<% warn "#{column_name} already exists, skipping." %>
<% warn "#{column_name} already exists, skipping." -%>
<% else -%>
add_column :<%= table_name %>, :<%= column_name %>, :<%= attribute.type %><%= attribute.inject_options %>
<%- if attribute.has_index? -%>
Expand Down
2 changes: 1 addition & 1 deletion lib/rails/generators/mobility/templates/table_migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def change
<%- else -%>
add_column :<%= table_name %>, :<%= attribute.name %>, :<%= attribute.type %><%= attribute.inject_options %>
<%- if attribute.has_index? -%>
add_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %>
add_index :<%= table_name %>, [:<%= attribute.index_name %><%= attribute.inject_index_options %>, :locale]
<%- end -%>
<%- end -%>
<% end -%>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def change
add_index :<%= table_name %>, [:<%= foreign_key %>, :locale], name: :<%= translation_unique_index_name %>, unique: true

<%- attributes_with_index.each do |attribute| -%>
add_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %>
add_index :<%= table_name %>, [:<%= attribute.index_name %><%= attribute.inject_index_options %>, :locale]
<%- end -%>
end
end
11 changes: 6 additions & 5 deletions lib/rails/generators/mobility/translations_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,17 @@ module Mobility
Other backends are not supported, for obvious reasons:
* the +key_value+ backend does not need any model-specific migrations, simply
run the install generator.
* +jsonb+, +hstore+ and +serialized+ backends simply require a single column on
a model table, which can be added with the normal Rails migration 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.

=end
class TranslationsGenerator < ::Rails::Generators::NamedBase
SUPPORTED_BACKENDS = %w[column table]
BACKEND_OPTIONS = { type: :string, desc: "Backend to use for translations (defaults to Mobility.default_backend)" }
SUPPORTED_BACKENDS = %w[column table].freeze
BACKEND_OPTIONS = { type: :string, desc: "Backend to use for translations (defaults to Mobility.default_backend)" }.freeze
argument :attributes, type: :array, default: [], banner: "field[:type][:index] field[:type][:index]"

class_option(:backend, BACKEND_OPTIONS)
class_option(:backend, BACKEND_OPTIONS.dup)
invoke_from_option :backend

def self.class_options(options = nil)
Expand Down
4 changes: 2 additions & 2 deletions spec/generators/rails/mobility/translations_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
contains "add_index :post_translations, :post_id, name: :index_post_translations_on_post_id"
contains "add_index :post_translations, :locale, name: :index_post_translations_on_locale"
contains "add_index :post_translations, [:post_id, :locale], name: :index_post_translations_on_post_id_and_locale, unique: true"
contains "add_index :post_translations, :title"
contains "add_index :post_translations, [:title, :locale]"
end
end
end
Expand All @@ -66,7 +66,7 @@
contains "class CreatePostTitleAndContentTranslationsForMobilityTableBackend < ActiveRecord::Migration[#{version_string_}]"
end
contains "add_column :post_translations, :title, :string"
contains "add_index :post_translations, :title"
contains "add_index :post_translations, [:title, :locale]"
contains "add_column :post_translations, :content, :text"
end
end
Expand Down