From 6b9605ed6fa599578fd36065ac17e6b2b93a8378 Mon Sep 17 00:00:00 2001 From: Chris Salzberg Date: Wed, 22 Mar 2017 12:50:50 +0900 Subject: [PATCH] Use data_source_exists? in generators and add column backend spec --- .../rails/mobility/backend_generators/base.rb | 12 +- .../backend_generators/column_backend.rb | 10 +- .../backend_generators/table_backend.rb | 2 +- .../mobility/translations_generator_spec.rb | 103 ++++++++++++++++++ 4 files changed, 115 insertions(+), 12 deletions(-) create mode 100644 spec/generators/rails/mobility/translations_generator_spec.rb diff --git a/lib/generators/rails/mobility/backend_generators/base.rb b/lib/generators/rails/mobility/backend_generators/base.rb index 1e60bfd98..0b40b9da8 100644 --- a/lib/generators/rails/mobility/backend_generators/base.rb +++ b/lib/generators/rails/mobility/backend_generators/base.rb @@ -31,8 +31,14 @@ def attributes_with_index private - def table_exists? - connection.table_exists?(table_name) + def check_data_source! + unless data_source_exists? + raise NoTableDefined, "The table #{table_name} does not exist. Create it first before generating translated columns." + end + end + + def data_source_exists? + connection.data_source_exists?(table_name) end delegate :connection, to: ::ActiveRecord::Base @@ -57,5 +63,7 @@ def migration_file "create_#{file_name}_#{attributes.map(&:name).join('_and_')}_translations_for_mobility_#{backend}_backend".freeze end end + + class NoTableDefined < StandardError; end end end diff --git a/lib/generators/rails/mobility/backend_generators/column_backend.rb b/lib/generators/rails/mobility/backend_generators/column_backend.rb index 39b89f444..0e74c4538 100644 --- a/lib/generators/rails/mobility/backend_generators/column_backend.rb +++ b/lib/generators/rails/mobility/backend_generators/column_backend.rb @@ -8,16 +8,8 @@ class ColumnBackend < Mobility::BackendGenerators::Base def initialize(*args) super - unless table_exists? - raise NoTableDefined, "The table #{table_name} does not exist. Create it first before generating translated columns." - end - unless I18n.available_locales.present? - raise NoAvailableLocales, "You must set I18n.available_locales to use the column backend generator." - end + check_data_source! end end - - class NoTableDefined < StandardError; end - class NoAvailableLocales < StandardError; end end end diff --git a/lib/generators/rails/mobility/backend_generators/table_backend.rb b/lib/generators/rails/mobility/backend_generators/table_backend.rb index bd3081b6e..5edd4c758 100644 --- a/lib/generators/rails/mobility/backend_generators/table_backend.rb +++ b/lib/generators/rails/mobility/backend_generators/table_backend.rb @@ -7,7 +7,7 @@ class TableBackend < Mobility::BackendGenerators::Base source_root File.expand_path("../../templates", __FILE__) def create_migration_file - if table_exists? && !self.class.migration_exists?(migration_dir, migration_file) + if data_source_exists? && !self.class.migration_exists?(migration_dir, migration_file) migration_template "#{backend}_migration.rb", "db/migrate/#{migration_file}.rb" else super diff --git a/spec/generators/rails/mobility/translations_generator_spec.rb b/spec/generators/rails/mobility/translations_generator_spec.rb new file mode 100644 index 000000000..b64e271b5 --- /dev/null +++ b/spec/generators/rails/mobility/translations_generator_spec.rb @@ -0,0 +1,103 @@ +require "spec_helper" + +describe Mobility::TranslationsGenerator, type: :generator, orm: :active_record do + require "generator_spec/test_case" + include GeneratorSpec::TestCase + require "generators/rails/mobility/translations_generator" + + destination File.expand_path("../tmp", __FILE__) + + after(:all) { prepare_destination } + + describe "--backend=table" do + before(:all) do + prepare_destination + run_generator %w(Post title:string:index content:text --backend=table) + end + + context "translations table does not yet exist" do + it "generates table translations migration creating translations table" do + expect(destination_root).to have_structure do + directory "db" do + directory "migrate" do + migration "create_post_title_and_content_translations_for_mobility_table_backend" do + contains "class CreatePostTitleAndContentTranslationsForMobilityTableBackend < ActiveRecord::Migration[5.0]" + contains "def change" + contains "create_table :post_translations" + contains "t.string :title" + contains "t.text :content" + contains "t.string :locale, null: false" + contains "t.integer :post_id, null: false" + contains "t.timestamps null: false" + 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" + end + end + end + end + end + end + + context "translation table already exists" do + before { ActiveRecord::Base.connection.create_table :post_translations } + + it "generates table translations migration adding columns to existing translations table" do + expect(destination_root).to have_structure do + directory "db" do + directory "migrate" do + migration "create_post_title_and_content_translations_for_mobility_table_backend" do + contains "class CreatePostTitleAndContentTranslationsForMobilityTableBackend < ActiveRecord::Migration[5.0]" + contains "add_column :post_translations, :title, :string" + contains "add_index :post_translations, :title" + contains "add_column :post_translations, :content, :text" + end + end + end + end + end + end + end + + describe "--backend=column" do + before { prepare_destination } + + context "model table does not exist" do + it "raises NoTableDefined error" do + expect { run_generator %w(Foo title:string:index content:text --backend=column) }.to raise_error(Mobility::BackendGenerators::NoTableDefined) + end + end + + context "model table exists" do + before do + ActiveRecord::Base.connection.create_table :foos + available_locales = I18n.available_locales + I18n.available_locales = [:en, :ja, :de] + run_generator %w(Foo title:string:index content:text --backend=column) + I18n.available_locales = available_locales + end + + it "generates column translations migration adding columns for each locale to model table" do + expect(destination_root).to have_structure do + directory "db" do + directory "migrate" do + migration "create_foo_title_and_content_translations_for_mobility_column_backend" do + contains "class CreateFooTitleAndContentTranslationsForMobilityColumnBackend < ActiveRecord::Migration[5.0]" + contains "add_column :foos, :title_en, :string" + contains "add_index :foos, :title_en" + contains "add_column :foos, :title_ja, :string" + contains "add_index :foos, :title_ja" + contains "add_column :foos, :title_de, :string" + contains "add_index :foos, :title_de" + contains "add_column :foos, :content_en, :text" + contains "add_column :foos, :content_ja, :text" + contains "add_column :foos, :content_de, :text" + end + end + end + end + end + end + end +end if Mobility::Loaded::Rails