Skip to content

Commit

Permalink
Use data_source_exists? in generators and add column backend spec
Browse files Browse the repository at this point in the history
  • Loading branch information
shioyama committed Mar 22, 2017
1 parent 4f93fc4 commit 6b9605e
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 12 deletions.
12 changes: 10 additions & 2 deletions lib/generators/rails/mobility/backend_generators/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
103 changes: 103 additions & 0 deletions spec/generators/rails/mobility/translations_generator_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 6b9605e

Please sign in to comment.