From b40601e3c7fab5bd2a1a3ef809cd83939bbdecd4 Mon Sep 17 00:00:00 2001 From: Aleksey Ivanov Date: Sun, 4 Sep 2016 16:32:56 +0300 Subject: [PATCH] make it rails independent --- lib/seedbank.rb | 10 +++++++--- lib/seedbank/dsl.rb | 4 +--- lib/seedbank/railtie.rb | 4 +--- lib/tasks/seed.rake | 3 +-- test/test_helper.rb | 2 +- 5 files changed, 11 insertions(+), 12 deletions(-) diff --git a/lib/seedbank.rb b/lib/seedbank.rb index 9801801..4f58dc4 100644 --- a/lib/seedbank.rb +++ b/lib/seedbank.rb @@ -3,10 +3,14 @@ module Seedbank class << self - attr_writer :seeds_root, :nesting, :matcher + attr_writer :application_root + + def application_root + @application_root ||= Pathname.new(Rake.application.original_dir) + end def seeds_root - @seeds_root ||= 'db/seeds' + @seeds_root ||= File.join(application_root, 'db', 'seeds') end def nesting @@ -23,4 +27,4 @@ def self.load_tasks end require 'seedbank/railtie' if defined?(Rails) && Rails::VERSION::MAJOR >= 3 -end \ No newline at end of file +end diff --git a/lib/seedbank/dsl.rb b/lib/seedbank/dsl.rb index 610b267..e81823b 100644 --- a/lib/seedbank/dsl.rb +++ b/lib/seedbank/dsl.rb @@ -25,9 +25,7 @@ def define_seed_task(seed_file, *args) runner.evaluate(seed_task, seed_file) if File.exist?(seed_file) end - # TODO: Stop being so dependent on Rails.root - # perhaps set Seedbank.application_root instead - relative_file = Pathname.new(seed_file).relative_path_from(Rails.root) + relative_file = Pathname.new(seed_file).relative_path_from(Seedbank.application_root) task.add_description "Load the seed data from #{relative_file}" add_environment_dependency(task) diff --git a/lib/seedbank/railtie.rb b/lib/seedbank/railtie.rb index 8465fb7..f884f3f 100644 --- a/lib/seedbank/railtie.rb +++ b/lib/seedbank/railtie.rb @@ -1,9 +1,7 @@ module Seedbank class Railtie < Rails::Railtie - rake_tasks do - Seedbank.seeds_root = File.expand_path('db/seeds', Rails.root) Seedbank.load_tasks end end -end \ No newline at end of file +end diff --git a/lib/tasks/seed.rake b/lib/tasks/seed.rake index f7813a6..8295405 100644 --- a/lib/tasks/seed.rake +++ b/lib/tasks/seed.rake @@ -20,10 +20,9 @@ namespace :db do # Then create a task for the environment glob_seed_files_matching('/*/').each do |directory| environment = File.basename(directory) - environment_dependencies = glob_seed_files_matching(environment, Seedbank.matcher).sort.map { |seed_file| seed_task_from_file(seed_file) } - desc "Load the seed data from db/seeds.rb, db/seeds/*.seeds.rb and db/seeds/#{environment}/#{Seedbank.matcher}." + desc "Load the seed data from db/seeds.rb, db/seeds/#{Seedbank.matcher} and db/seeds/#{environment}/#{Seedbank.matcher}." task environment => ['db:seed:common'] + environment_dependencies override_dependency << "db:seed:#{environment}" if defined?(Rails) && Rails.env == environment diff --git a/test/test_helper.rb b/test/test_helper.rb index 6d5b591..51e3b1a 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -9,7 +9,7 @@ Rails.backtrace_cleaner.remove_silencers! -Seedbank.seeds_root = File.expand_path('dummy/db/seeds', __FILE__) +Seedbank.application_root = Pathname.new(File.expand_path('../dummy', __FILE__)) class Seedbank::Spec < MiniTest::Spec def setup