Skip to content

Commit

Permalink
Merge branch 'rails-independent' of https://github.com/ivanovaleksey/…
Browse files Browse the repository at this point in the history
…seedbank into ivanovaleksey-rails-independent
  • Loading branch information
James McCarthy committed Feb 12, 2017
2 parents 8c9d625 + 322c013 commit 959a80c
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 11 deletions.
10 changes: 7 additions & 3 deletions lib/seedbank.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@

module Seedbank
class << self
attr_writer :seeds_root, :nesting, :matcher
attr_writer :application_root, :seeds_root, :nesting, :matcher

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
Expand All @@ -20,7 +24,7 @@ def matcher
end

def self.load_tasks
Dir[File.expand_path('tasks/*.rake', File.dirname(__FILE__))].each { |ext| load ext }
Dir[File.expand_path('../tasks/*.rake', __FILE__)].each { |ext| load ext }
end

require 'seedbank/railtie' if defined?(Rails) && Rails::VERSION::MAJOR >= 3
Expand Down
4 changes: 1 addition & 3 deletions lib/seedbank/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,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)
Expand Down
1 change: 0 additions & 1 deletion lib/seedbank/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
module Seedbank
class Railtie < Rails::Railtie
rake_tasks do
Seedbank.seeds_root = File.expand_path('db/seeds', Rails.root)
Seedbank.load_tasks
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/tasks/seed.rake
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ namespace :db do

environment_dependencies = seed_tasks_matching(environment, Seedbank.matcher)

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
end
end

# Override db:seed to run all the common and environments seeds plus the original db:seed.
desc %{Load the seed data from db/seeds.rb, db/seeds/#{Seedbank.matcher} and db/seeds/ENVIRONMENT/#{Seedbank.matcher}
(ENVIRONMENT is the current RAILS_ENV).}
desc %(Load the seed data from db/seeds.rb, db/seeds/#{Seedbank.matcher} and db/seeds/ENVIRONMENT/#{Seedbank.matcher}.
ENVIRONMENT is the current Rails.env.)
override_seed_task seed: override_dependency
end
2 changes: 1 addition & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,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
Expand Down

0 comments on commit 959a80c

Please sign in to comment.