Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make it rails independent #57

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

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 }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [93/80]


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__))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought I should prefer single quotes 😄

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I definitely prefer single quotes unless interpolation, it's a good signpost to look closer when there are double quotes.


class Seedbank::Spec < MiniTest::Spec
def setup
Expand Down