Skip to content

Commit

Permalink
make it rails independent
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanovaleksey committed Sep 4, 2016
1 parent 03888f0 commit 322c013
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 13 deletions.
12 changes: 8 additions & 4 deletions lib/seedbank.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -19,8 +23,8 @@ 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
end
end
4 changes: 1 addition & 3 deletions lib/seedbank/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 1 addition & 3 deletions lib/seedbank/railtie.rb
Original file line number Diff line number Diff line change
@@ -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
end
3 changes: 1 addition & 2 deletions lib/tasks/seed.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 322c013

Please sign in to comment.