-
Notifications
You must be signed in to change notification settings - Fork 91
Enable to use customized file name #71
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
base: master
Are you sure you want to change the base?
Changes from all commits
088d9a9
bd545e5
e6ef199
b7d569f
a78bbf7
5be4b43
04ed030
1dc052b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ | |
|
||
module Seedbank | ||
class << self | ||
attr_writer :application_root, :seeds_root, :nesting, :matcher | ||
attr_writer :application_root, :seeds_root, :nesting, :matcher, :original_seeds_file | ||
|
||
def application_root | ||
@application_root ||= Pathname.new(Rake.application.original_dir) | ||
|
@@ -21,10 +21,14 @@ def nesting | |
def matcher | ||
@matcher ||= '*.seeds.rb' | ||
end | ||
|
||
def original_seeds_file | ||
@original_seeds_file ||= File.join(application_root, 'db', 'seeds.rb') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping. |
||
end | ||
end | ||
|
||
def self.load_tasks | ||
Dir[File.expand_path('../tasks/*.rake', __FILE__)].each { |ext| load ext } | ||
Dir[File.expand_path('tasks/*.rake', __dir__)].each { |ext| load ext } | ||
end | ||
|
||
require 'seedbank/railtie' if defined?(Rails) && Rails::VERSION::MAJOR >= 3 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,8 +17,9 @@ def seed_tasks_matching(*pattern) | |
end | ||
|
||
def seed_task_from_file(seed_file) | ||
scopes = scope_from_seed_file(seed_file) | ||
fq_name = scopes.push(File.basename(seed_file, '.seeds.rb')).join(':') | ||
scopes = scope_from_seed_file(seed_file) | ||
suffix = Seedbank.matcher.gsub(/\A\*/, '') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping. |
||
fq_name = scopes.push(File.basename(seed_file, suffix)).join(':') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping. |
||
|
||
define_seed_task(seed_file, fq_name) | ||
end | ||
|
@@ -40,11 +41,11 @@ def define_seed_task(seed_file, *args) | |
end | ||
|
||
def original_seeds_file | ||
@_seedbank_original ||= existent(Pathname.new('../seeds.rb').expand_path(seeds_root)) | ||
@original_seeds_file ||= existent(original_seeds_file_expanded(Seedbank.original_seeds_file, seeds_root)) | ||
end | ||
|
||
def seeds_root | ||
Pathname.new Seedbank.seeds_root | ||
Pathname.new(Seedbank.seeds_root) | ||
end | ||
|
||
private | ||
|
@@ -71,7 +72,7 @@ def add_environment_dependency(task) | |
end | ||
|
||
def runner | ||
@_seedbank_runner ||= Seedbank::Runner.new | ||
@runner ||= Seedbank::Runner.new | ||
end | ||
|
||
def add_comment_to(seed_task, comment) | ||
|
@@ -81,6 +82,10 @@ def add_comment_to(seed_task, comment) | |
seed_task.send :instance_variable_set, '@full_comment', comment | ||
end | ||
end | ||
|
||
def original_seeds_file_expanded(filename, root_dir) | ||
Pathname.new(filename).expand_path(root_dir) | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,12 @@ | ||
# frozen_string_literal: true | ||
if defined?(Rails) | ||
initializer = Rails.root.join('config', 'initializers', 'seedbank.rb') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping. |
||
require initializer if initializer.exist? | ||
end | ||
|
||
namespace :db do | ||
using Seedbank::DSL | ||
|
||
override_dependency = ['db:seed:common'] | ||
|
||
namespace :seed do | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,49 +4,31 @@ | |
using Seedbank::DSL | ||
|
||
describe Seedbank::DSL do | ||
# TODO: This is private so should really be tested indirectly. | ||
describe 'scope_from_seed_file' do | ||
subject { scope_from_seed_file(seed_file) } | ||
|
||
describe 'in an environment directory' do | ||
let(:seed_file) { File.expand_path('development/users.seeds.rb', Seedbank.seeds_root) } | ||
let(:seed_namespace) { %w[development] } | ||
|
||
it 'returns the enviroment scope' do | ||
subject.must_equal seed_namespace | ||
end | ||
end | ||
describe 'override_seed_task' do | ||
describe 'when no task exists to override' do | ||
let(:task_name) { 'my_task' } | ||
let(:dependencies) { ['db:abort_if_pending_migrations'] } | ||
|
||
describe 'in a nested directory' do | ||
let(:seed_file) { File.expand_path('development/shared/accounts.seeds.rb', Seedbank.seeds_root) } | ||
let(:seed_namespace) { %w[development shared] } | ||
it 'creates a new task' do | ||
Seedbank::DSL.override_seed_task(task_name => dependencies) | ||
|
||
it 'returns the nested scope' do | ||
subject.must_equal seed_namespace | ||
Rake::Task[task_name].wont_be_nil | ||
end | ||
end | ||
|
||
describe 'in seeds root' do | ||
let(:seed_file) { File.expand_path('no_block.seeds.rb', Seedbank.seeds_root) } | ||
|
||
it 'returns an array' do | ||
subject.must_be_instance_of Array | ||
end | ||
it 'applies the dependencies' do | ||
expected_dependencies = dependencies.map { |dependency| Rake::Task[dependency] } | ||
Seedbank::DSL.override_seed_task(task_name => dependencies) | ||
|
||
it 'must be empty' do | ||
subject.must_be_empty | ||
Rake::Task[task_name].prerequisite_tasks.must_equal expected_dependencies | ||
end | ||
end | ||
end | ||
|
||
describe 'seeds_root' do | ||
let(:seeds_root) { '/my/seeds/directory' } | ||
it 'applies the description' do | ||
description = 'Expected Description' | ||
Rake.application.last_description = description | ||
|
||
subject { Seedbank::DSL.seeds_root } | ||
Seedbank::DSL.override_seed_task(task_name => dependencies) | ||
|
||
it 'returns a Pathname' do | ||
Seedbank.stub(:seeds_root, seeds_root) do | ||
subject.must_equal Pathname.new(seeds_root) | ||
Rake::Task[task_name].full_comment.must_equal description | ||
end | ||
end | ||
end | ||
|
@@ -135,32 +117,61 @@ def define_prerequisite_task | |
end | ||
end | ||
|
||
describe 'override_seed_task' do | ||
describe 'when no task exists to override' do | ||
let(:task_name) { 'my_task' } | ||
let(:dependencies) { ['db:abort_if_pending_migrations'] } | ||
describe 'seeds_root' do | ||
let(:seeds_root) { '/my/seeds/directory' } | ||
|
||
it 'creates a new task' do | ||
Seedbank::DSL.override_seed_task(task_name => dependencies) | ||
subject { Seedbank::DSL.seeds_root } | ||
|
||
Rake::Task[task_name].wont_be_nil | ||
it 'returns a Pathname' do | ||
Seedbank.stub(:seeds_root, seeds_root) do | ||
subject.must_equal Pathname.new(seeds_root) | ||
end | ||
end | ||
end | ||
|
||
it 'applies the dependencies' do | ||
expected_dependencies = dependencies.map { |dependency| Rake::Task[dependency] } | ||
Seedbank::DSL.override_seed_task(task_name => dependencies) | ||
# TODO: This is private so should really be tested indirectly. | ||
describe 'scope_from_seed_file' do | ||
subject { scope_from_seed_file(seed_file) } | ||
|
||
Rake::Task[task_name].prerequisite_tasks.must_equal expected_dependencies | ||
describe 'in an environment directory' do | ||
let(:seed_file) { File.expand_path('development/users.seeds.rb', Seedbank.seeds_root) } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping. |
||
let(:seed_namespace) { %w[development] } | ||
|
||
it 'returns the enviroment scope' do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping. |
||
subject.must_equal seed_namespace | ||
end | ||
end | ||
|
||
describe 'in a nested directory' do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping. |
||
let(:seed_file) { File.expand_path('development/shared/accounts.seeds.rb', Seedbank.seeds_root) } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping. |
||
let(:seed_namespace) { %w[development shared] } | ||
|
||
it 'returns the nested scope' do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping. |
||
subject.must_equal seed_namespace | ||
end | ||
end | ||
|
||
it 'applies the description' do | ||
description = 'Expected Description' | ||
Rake.application.last_description = description | ||
describe 'in seeds root' do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping. |
||
let(:seed_file) { File.expand_path('no_block.seeds.rb', Seedbank.seeds_root) } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping. |
||
|
||
Seedbank::DSL.override_seed_task(task_name => dependencies) | ||
it 'returns an array' do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping. |
||
subject.must_be_instance_of Array | ||
end | ||
|
||
Rake::Task[task_name].full_comment.must_equal description | ||
it 'must be empty' do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping. |
||
subject.must_be_empty | ||
end | ||
end | ||
end | ||
|
||
describe 'original_seeds_file_expanded' do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping. |
||
subject { original_seeds_file_expanded(filename, root) } | ||
|
||
let(:filename) { '../seeds_original.rb' } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping. |
||
let(:root) { '/my/seeds/directory' } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping. |
||
|
||
it 'returns an expanded path name' do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping. |
||
subject.must_equal Pathname.new('/my/seeds/seeds_original.rb') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping. |
||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,15 +10,15 @@ | |
|
||
Rails.backtrace_cleaner.remove_silencers! | ||
|
||
Seedbank.application_root = Pathname.new(File.expand_path('../dummy', __FILE__)) | ||
Seedbank.application_root = Pathname.new(File.expand_path('dummy', __dir__)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping. |
||
|
||
class Seedbank::Spec < MiniTest::Spec | ||
def setup | ||
silence_warnings do | ||
Rake.application = Rake::Application.new | ||
Dummy::Application.load_tasks | ||
Object.const_set :FakeModel, MiniTest::Mock.new | ||
TOPLEVEL_BINDING.eval('self').send(:instance_variable_set, :@_seedbank_runner, Seedbank::Runner.new) | ||
TOPLEVEL_BINDING.eval('self').send(:instance_variable_set, :@runner, Seedbank::Runner.new) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping. |
||
end | ||
|
||
super | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.