diff --git a/README.md b/README.md index 629538bc..f550b048 100644 --- a/README.md +++ b/README.md @@ -108,6 +108,7 @@ You can override this setting in `config/initializers/data_migrate.rb` ```ruby DataMigrate.configure do |config| config.data_migrations_path = 'db/awesomepath/' + config.data_template_path = Rails.root.join("lib", "awesomepath", "custom_data_migration.rb") config.db_configuration = { 'host' => '127.0.0.1', 'database' => 'awesome_database', diff --git a/lib/data_migrate.rb b/lib/data_migrate.rb index 47096479..39ce3a88 100644 --- a/lib/data_migrate.rb +++ b/lib/data_migrate.rb @@ -19,4 +19,7 @@ end module DataMigrate + def self.root + File.dirname(__FILE__) + end end diff --git a/lib/data_migrate/config.rb b/lib/data_migrate/config.rb index 78ec34d2..a7aff17a 100644 --- a/lib/data_migrate/config.rb +++ b/lib/data_migrate/config.rb @@ -12,12 +12,21 @@ def config end class Config - attr_accessor :data_migrations_path, :db_configuration, :spec_name + attr_accessor :data_migrations_path, :data_template_path, :db_configuration, :spec_name + + DEFAULT_DATA_TEMPLATE_PATH = "data_migration.rb" def initialize @data_migrations_path = "db/data/" + @data_template_path = DEFAULT_DATA_TEMPLATE_PATH @db_configuration = nil @spec_name = nil end + + def data_template_path=(value) + @data_template_path = value.tap do |path| + raise ArgumentError, "File not found: '#{path}'" unless path == DEFAULT_DATA_TEMPLATE_PATH || File.exists?(path) + end + end end end diff --git a/lib/generators/data_migrate.rb b/lib/generators/data_migrate.rb index 2aad73f6..30b9de9c 100644 --- a/lib/generators/data_migrate.rb +++ b/lib/generators/data_migrate.rb @@ -1,9 +1,22 @@ require 'rails/generators/named_base' + module DataMigrate module Generators class DataMigrationGenerator < Rails::Generators::NamedBase #:nodoc: - def self.source_root - @_data_migrate_source_root ||= File.expand_path(File.join(File.dirname(__FILE__), generator_name, 'templates')) + class << self + def source_root + build_data_migrate_source_root + end + + private + + def build_data_migrate_source_root + if DataMigrate.config.data_template_path == DataMigrate::Config::DEFAULT_DATA_TEMPLATE_PATH + File.expand_path(File.join(File.dirname(__FILE__), generator_name, 'templates')) + else + File.expand_path(File.dirname(DataMigrate.config.data_template_path)) + end + end end end end diff --git a/lib/generators/data_migration/data_migration_generator.rb b/lib/generators/data_migration/data_migration_generator.rb index f8279545..b488ec83 100644 --- a/lib/generators/data_migration/data_migration_generator.rb +++ b/lib/generators/data_migration/data_migration_generator.rb @@ -14,7 +14,7 @@ class DataMigrationGenerator < Rails::Generators::NamedBase def create_data_migration set_local_assigns! - migration_template "data_migration.rb", data_migrations_file_path + migration_template template_path, data_migrations_file_path end protected @@ -26,6 +26,10 @@ def set_local_assigns! end end + def template_path + DataMigrate.config.data_template_path + end + def migration_base_class_name "ActiveRecord::Migration[#{ActiveRecord::Migration.current_version}]" end diff --git a/spec/data_migrate/config_spec.rb b/spec/data_migrate/config_spec.rb index 58e5caec..60cd0db9 100644 --- a/spec/data_migrate/config_spec.rb +++ b/spec/data_migrate/config_spec.rb @@ -1,12 +1,16 @@ require "spec_helper" describe DataMigrate::Config do - it "sets default data_migrations_path path", :no_override do expect(DataMigrate.config.data_migrations_path).to eq "db/data/" end + it "sets default data_template_path path", :no_override do + expect(DataMigrate.config.data_template_path).to eq DataMigrate::Config::DEFAULT_DATA_TEMPLATE_PATH + end + describe "data migration path configured" do + subject { DataMigrate.config.data_migrations_path } before do @before = DataMigrate.config.data_migrations_path DataMigrate.configure do |config| @@ -20,8 +24,43 @@ end end - it do - expect(DataMigrate.config.data_migrations_path).to eq "db/awesome/" + it "equals the custom data migration path" do + is_expected.to eq "db/awesome/" + end + end + + describe "data template path configured" do + subject { DataMigrate.config.data_template_path } + + before do + @before = DataMigrate.config.data_template_path + DataMigrate.configure do |config| + config.data_template_path = data_template_path + end + end + + let(:data_template_path) do + File.join(DataMigrate.root, "generators", "data_migration", "templates", "data_migration.rb") + end + + after do + DataMigrate.configure do |config| + config.data_template_path = @before + end + end + + it "equals the custom data template path" do + is_expected.to eq data_template_path + end + + context "when path does not exist" do + subject { DataMigrate.config.data_template_path = invalid_path } + + let(:invalid_path) { "lib/awesome/templates/data_migration.rb" } + + it "checks that file exists on setting config var" do + expect { subject }.to raise_error { ArgumentError.new("File not found: '#{data_template_path}'") } + end end end end diff --git a/spec/generators/data_migration/data_migration_generator_spec.rb b/spec/generators/data_migration/data_migration_generator_spec.rb index aa870651..47547154 100644 --- a/spec/generators/data_migration/data_migration_generator_spec.rb +++ b/spec/generators/data_migration/data_migration_generator_spec.rb @@ -4,7 +4,8 @@ require 'generators/data_migration/data_migration_generator' describe DataMigrate::Generators::DataMigrationGenerator do - let(:subject) { DataMigrate::Generators::DataMigrationGenerator } + subject { DataMigrate::Generators::DataMigrationGenerator } + describe :next_migration_number do it "next migration" do Timecop.freeze("2016-12-03 22:15:26 -0800") do @@ -19,14 +20,18 @@ end describe :migration_base_class_name do - let(:subject) { DataMigrate::Generators::DataMigrationGenerator.new(['my_migration']) } + subject { generator.send(:migration_base_class_name) } + + let(:generator) { DataMigrate::Generators::DataMigrationGenerator.new(['my_migration']) } + it "returns the correct base class name" do - expect(subject.send(:migration_base_class_name)).to eq("ActiveRecord::Migration[#{ActiveRecord::Migration.current_version}]") + is_expected.to eq("ActiveRecord::Migration[#{ActiveRecord::Migration.current_version}]") end end describe :create_data_migration do - let(:subject) { DataMigrate::Generators::DataMigrationGenerator.new(['my_migration']) } + subject { DataMigrate::Generators::DataMigrationGenerator.new(['my_migration']) } + let(:data_migrations_file_path) { 'abc/my_migration.rb' } context 'when custom data migrations path has a trailing slash' do @@ -35,7 +40,7 @@ end it 'returns correct file path' do - expect(subject).to receive(:migration_template).with( + is_expected.to receive(:migration_template).with( 'data_migration.rb', data_migrations_file_path ) @@ -49,7 +54,7 @@ end it 'returns correct file path' do - expect(subject).to receive(:migration_template).with( + is_expected.to receive(:migration_template).with( 'data_migration.rb', data_migrations_file_path ) @@ -57,4 +62,40 @@ end end end + + describe ".source_root" do + subject { described_class.source_root } + + let(:default_source_root) do + File.expand_path( + File.dirname(File.join(DataMigrate.root, "generators", "data_migration", "templates", "data_migration.rb")) + ) + end + + it { is_expected.to eq default_source_root } + + context "when DateMigrate.config.data_template_path is set" do + before do + @before = DataMigrate.config.data_template_path + DataMigrate.configure do |config| + config.data_template_path = data_template_path + end + end + + let(:data_template_path) do + File.join(DataMigrate.root, "generators", "data_migration", "templates", "data_migration.rb") + end + let(:expected_source_root) { File.dirname(data_template_path) } + + after do + DataMigrate.configure do |config| + config.data_template_path = @before + end + end + + it "reads directory from config data template path" do + is_expected.to eq expected_source_root + end + end + end end