seedbank 0.2.1 → 0.3.0.pre
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/README.md +3 -1
- data/lib/seedbank.rb +0 -2
- data/lib/seedbank/dsl.rb +14 -9
- data/lib/seedbank/version.rb +1 -1
- data/lib/tasks/seed.rake +4 -2
- data/test/dummy/db/seeds.rb +1 -0
- data/test/lib/seedbank/dsl_test.rb +46 -12
- data/test/lib/seedbank/runner_test.rb +8 -1
- data/test/lib/tasks/seed_rake_test.rb +2 -2
- metadata +4 -10
- data/lib/seedbank/rename_task.rb +0 -9
- data/lib/seedbank/task_manager.rb +0 -21
- data/test/lib/seedbank/rename_task_test.rb +0 -19
- data/test/lib/seedbank/task_manager_test.rb +0 -104
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -79,7 +79,7 @@ If you vendor the gem you'll need to change the require to the specific path.
|
|
79
79
|
Usage
|
80
80
|
=====
|
81
81
|
|
82
|
-
Seeds files are just plain old Ruby executed in your rails application environment so anything you could type into the rails console will work in your seeds.
|
82
|
+
Seeds files are just plain old Ruby executed in your rails application environment so anything you could type into the rails console will work in your seeds. Seeds files have to be named with the '.seeds.rb' extension.
|
83
83
|
|
84
84
|
db/seeds/companies.seeds.rb
|
85
85
|
```ruby
|
@@ -124,6 +124,8 @@ after "development:companies" do
|
|
124
124
|
end
|
125
125
|
```
|
126
126
|
|
127
|
+
*Note* - If you experience any errors like `Don't know how to build task 'db:seed:users'`. Ensure your specifying `after 'development:companies'` like the above example. This is the usual culprit (YMMV).
|
128
|
+
|
127
129
|
Contributors
|
128
130
|
============
|
129
131
|
```shell
|
data/lib/seedbank.rb
CHANGED
data/lib/seedbank/dsl.rb
CHANGED
@@ -1,21 +1,18 @@
|
|
1
1
|
module Seedbank
|
2
2
|
module DSL
|
3
3
|
|
4
|
-
def
|
5
|
-
Rake
|
6
|
-
Rake::
|
7
|
-
|
8
|
-
|
9
|
-
def override_seed_task(*args, &block)
|
10
|
-
Rake.application.override_task(*args, &block)
|
4
|
+
def override_seed_task(*args)
|
5
|
+
task_name, arg_names, deps = Rake.application.resolve_args(args)
|
6
|
+
seed_task = Rake::Task.task_defined?(task_name) ? Rake::Task[task_name].clear : Rake::Task.define_task(task_name)
|
7
|
+
seed_task.send :instance_variable_set, '@full_comment', Rake.application.last_description
|
8
|
+
seed_task.enhance deps
|
11
9
|
end
|
12
10
|
|
13
11
|
def seed_task_from_file(seed_file)
|
14
12
|
scopes = scope_from_seed_file(seed_file)
|
15
13
|
fq_name = scopes.push(File.basename(seed_file, '.seeds.rb')).join(':')
|
16
|
-
args = Rake::Task.task_defined?('db:abort_if_pending_migrations') ? { fq_name => 'db:abort_if_pending_migrations' } : { fq_name => :environment }
|
17
14
|
|
18
|
-
define_seed_task(seed_file,
|
15
|
+
define_seed_task(seed_file, fq_name)
|
19
16
|
end
|
20
17
|
|
21
18
|
def glob_seed_files_matching(*args, &block)
|
@@ -26,7 +23,15 @@ module Seedbank
|
|
26
23
|
task = Rake::Task.define_task(*args) do |seed_task|
|
27
24
|
Seedbank::Runner.new(seed_task).module_eval(File.read(seed_file), seed_file) if File.exist?(seed_file)
|
28
25
|
end
|
26
|
+
|
29
27
|
task.add_description "Load the seed data from #{seed_file}"
|
28
|
+
|
29
|
+
if Rake::Task.task_defined?('db:abort_if_pending_migrations')
|
30
|
+
task.enhance(['db:abort_if_pending_migrations'])
|
31
|
+
elsif Rake::Task.task_defined?(':environment')
|
32
|
+
task.enhance([':environment'])
|
33
|
+
end
|
34
|
+
|
30
35
|
task.name
|
31
36
|
end
|
32
37
|
|
data/lib/seedbank/version.rb
CHANGED
data/lib/tasks/seed.rake
CHANGED
@@ -25,10 +25,12 @@ namespace :db do
|
|
25
25
|
|
26
26
|
override_dependency << "db:seed:#{environment}" if defined?(Rails) && Rails.env == environment
|
27
27
|
end
|
28
|
+
|
29
|
+
original_seeds_file = Rails.application.paths["db/seeds"].existent.first
|
30
|
+
define_seed_task original_seeds_file, :original if original_seeds_file
|
28
31
|
end
|
29
32
|
|
30
33
|
# Override db:seed to run all the common and environments seeds plus the original db:seed.
|
31
|
-
desc
|
34
|
+
desc 'Load the seed data from db/seeds.rb, db/seeds/*.seeds.rb and db/seeds/ENVIRONMENT/*.seeds.rb. ENVIRONMENT is the current environment in Rails.env.'
|
32
35
|
override_seed_task :seed => override_dependency
|
33
|
-
|
34
36
|
end
|
data/test/dummy/db/seeds.rb
CHANGED
@@ -0,0 +1 @@
|
|
1
|
+
FakeModel.seed('db/seeds.rb')
|
@@ -54,7 +54,7 @@ describe Seedbank::DSL do
|
|
54
54
|
|
55
55
|
let(:seeds_root) { '/my/seeds/directory' }
|
56
56
|
|
57
|
-
subject { Seedbank::DSL.seeds_root }
|
57
|
+
subject { Seedbank::DSL.send(:seeds_root) }
|
58
58
|
|
59
59
|
it "returns a Pathname" do
|
60
60
|
flexmock(Seedbank).should_receive(:seeds_root).and_return(seeds_root).by_default
|
@@ -111,12 +111,6 @@ describe Seedbank::DSL do
|
|
111
111
|
Rake::Task[name].wont_be_nil
|
112
112
|
end
|
113
113
|
|
114
|
-
it "sets Rake Task dependencies" do
|
115
|
-
Seedbank::DSL.define_seed_task(seed_file, name => dependencies)
|
116
|
-
|
117
|
-
Rake::Task[name].prerequisite_tasks.must_equal dependencies.map { |dependency| Rake::Task[dependency] }
|
118
|
-
end
|
119
|
-
|
120
114
|
it "sets Rake Task description" do
|
121
115
|
Seedbank::DSL.define_seed_task(seed_file, name => dependencies)
|
122
116
|
|
@@ -130,17 +124,57 @@ describe Seedbank::DSL do
|
|
130
124
|
|
131
125
|
Rake::Task[name].invoke
|
132
126
|
end
|
127
|
+
|
128
|
+
describe "when db:abort_if_pending_migrations exists" do
|
129
|
+
it "sets Rake Task dependencies" do
|
130
|
+
Seedbank::DSL.define_seed_task(seed_file, name => dependencies)
|
131
|
+
expected_dependencies = dependencies.map { |dependency| Rake::Task[dependency] }
|
132
|
+
expected_dependencies << Rake::Task['db:abort_if_pending_migrations']
|
133
|
+
|
134
|
+
Rake::Task[name].prerequisite_tasks.must_equal expected_dependencies
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
describe "when db:abort_if_pending_migrations does not exist" do
|
139
|
+
it "sets Rake Task dependencies" do
|
140
|
+
flexmock(Rake::Task).should_receive(:task_defined?).and_return(false).by_default
|
141
|
+
|
142
|
+
Seedbank::DSL.define_seed_task(seed_file, name => dependencies)
|
143
|
+
|
144
|
+
Rake::Task[name].prerequisite_tasks.must_equal dependencies.map { |dependency| Rake::Task[dependency] }
|
145
|
+
end
|
146
|
+
end
|
133
147
|
end
|
134
148
|
|
135
149
|
describe "override_seed_task" do
|
136
150
|
|
137
|
-
|
151
|
+
describe "when no task exists to override" do
|
152
|
+
|
153
|
+
let(:name) { 'my_task' }
|
154
|
+
let(:dependencies) { ['seedy:users'] }
|
155
|
+
|
156
|
+
it "creates a new task" do
|
157
|
+
Seedbank::DSL.override_seed_task(name => dependencies)
|
138
158
|
|
139
|
-
|
140
|
-
|
141
|
-
|
159
|
+
Rake::Task[name].wont_be_nil
|
160
|
+
end
|
161
|
+
|
162
|
+
it "applies the dependencies" do
|
163
|
+
expected_dependencies = dependencies.map { |dependency| Rake::Task[dependency] }
|
164
|
+
Seedbank::DSL.override_seed_task(name => dependencies)
|
142
165
|
|
143
|
-
|
166
|
+
Rake::Task[name].prerequisite_tasks.must_equal expected_dependencies
|
167
|
+
end
|
168
|
+
|
169
|
+
it "applies the description" do
|
170
|
+
description = 'Expected Description'
|
171
|
+
Rake.application.last_description = description
|
172
|
+
|
173
|
+
Seedbank::DSL.override_seed_task(name => dependencies)
|
174
|
+
|
175
|
+
Rake::Task[name].full_comment.must_equal description
|
176
|
+
end
|
144
177
|
end
|
178
|
+
|
145
179
|
end
|
146
180
|
end
|
@@ -47,7 +47,7 @@ describe Seedbank::Runner do
|
|
47
47
|
end
|
48
48
|
|
49
49
|
describe "after with no block given" do
|
50
|
-
|
50
|
+
|
51
51
|
subject { Rake::Task['db:seed:no_block'] }
|
52
52
|
|
53
53
|
it "runs the dependencies" do
|
@@ -56,4 +56,11 @@ describe Seedbank::Runner do
|
|
56
56
|
subject.invoke
|
57
57
|
end
|
58
58
|
end
|
59
|
+
|
60
|
+
describe "let" do
|
61
|
+
|
62
|
+
it "does something" do
|
63
|
+
|
64
|
+
end
|
65
|
+
end
|
59
66
|
end
|
@@ -49,7 +49,7 @@ describe 'Seedbank rake.task' do
|
|
49
49
|
subject { Rake::Task['db:seed:original'] }
|
50
50
|
|
51
51
|
it "has no dependencies" do
|
52
|
-
subject.prerequisites.
|
52
|
+
subject.prerequisites.must_equal %w[db:abort_if_pending_migrations]
|
53
53
|
end
|
54
54
|
|
55
55
|
describe "when seeds are reloaded" do
|
@@ -59,7 +59,7 @@ describe 'Seedbank rake.task' do
|
|
59
59
|
end
|
60
60
|
|
61
61
|
it "still has no dependencies" do
|
62
|
-
subject.prerequisites.
|
62
|
+
subject.prerequisites.must_equal %w[db:abort_if_pending_migrations]
|
63
63
|
end
|
64
64
|
end
|
65
65
|
end
|
metadata
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: seedbank
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.3.0.pre
|
5
|
+
prerelease: 6
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- James McCarthy
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2013-04-02 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: minitest
|
@@ -78,9 +78,7 @@ files:
|
|
78
78
|
- lib/seedbank.rb
|
79
79
|
- lib/seedbank/dsl.rb
|
80
80
|
- lib/seedbank/railtie.rb
|
81
|
-
- lib/seedbank/rename_task.rb
|
82
81
|
- lib/seedbank/runner.rb
|
83
|
-
- lib/seedbank/task_manager.rb
|
84
82
|
- lib/seedbank/version.rb
|
85
83
|
- lib/tasks/seed.rake
|
86
84
|
- seedbank.gemspec
|
@@ -113,9 +111,7 @@ files:
|
|
113
111
|
- test/dummy/lib/tasks/dsl.rake
|
114
112
|
- test/dummy/script/rails
|
115
113
|
- test/lib/seedbank/dsl_test.rb
|
116
|
-
- test/lib/seedbank/rename_task_test.rb
|
117
114
|
- test/lib/seedbank/runner_test.rb
|
118
|
-
- test/lib/seedbank/task_manager_test.rb
|
119
115
|
- test/lib/tasks/seed_rake_test.rb
|
120
116
|
- test/test_helper.rb
|
121
117
|
homepage: http://github.com/james2m/seedbank
|
@@ -145,7 +141,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
145
141
|
version: 1.2.0
|
146
142
|
requirements: []
|
147
143
|
rubyforge_project:
|
148
|
-
rubygems_version: 1.8.
|
144
|
+
rubygems_version: 1.8.25
|
149
145
|
signing_key:
|
150
146
|
specification_version: 3
|
151
147
|
summary: Extends Rails seeds to split out complex seeds into their own file and have
|
@@ -180,8 +176,6 @@ test_files:
|
|
180
176
|
- test/dummy/lib/tasks/dsl.rake
|
181
177
|
- test/dummy/script/rails
|
182
178
|
- test/lib/seedbank/dsl_test.rb
|
183
|
-
- test/lib/seedbank/rename_task_test.rb
|
184
179
|
- test/lib/seedbank/runner_test.rb
|
185
|
-
- test/lib/seedbank/task_manager_test.rb
|
186
180
|
- test/lib/tasks/seed_rake_test.rb
|
187
181
|
- test/test_helper.rb
|
data/lib/seedbank/rename_task.rb
DELETED
@@ -1,21 +0,0 @@
|
|
1
|
-
module Seedbank
|
2
|
-
module TaskManager
|
3
|
-
|
4
|
-
def override_task(*args, &block)
|
5
|
-
name, params, deps = resolve_args(args.dup)
|
6
|
-
fq_name = current_scope.push(name).join(':')
|
7
|
-
new_name = "#{fq_name}:original"
|
8
|
-
unless Rake::Task.task_defined?(new_name)
|
9
|
-
rename_task(fq_name, new_name)
|
10
|
-
define_task(Rake::Task, *args, &block)
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
def rename_task(fq_name, new_name)
|
15
|
-
task = @tasks.delete(fq_name)
|
16
|
-
task.rename(new_name)
|
17
|
-
@tasks[new_name] = task
|
18
|
-
end
|
19
|
-
|
20
|
-
end
|
21
|
-
end
|
@@ -1,19 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
describe Seedbank::RenameTask do
|
4
|
-
|
5
|
-
describe "rename" do
|
6
|
-
|
7
|
-
let(:original_name) { 'original_name' }
|
8
|
-
let(:new_name) { 'new_name' }
|
9
|
-
|
10
|
-
subject { Rake::Task.define_task(original_name) }
|
11
|
-
|
12
|
-
before { subject.rename(new_name) }
|
13
|
-
|
14
|
-
it "changes the tasks name" do
|
15
|
-
subject.name.must_equal new_name
|
16
|
-
end
|
17
|
-
|
18
|
-
end
|
19
|
-
end
|
@@ -1,104 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
describe Seedbank::TaskManager do
|
4
|
-
|
5
|
-
describe "override_task" do
|
6
|
-
|
7
|
-
let(:task_name) { 'my_task' }
|
8
|
-
|
9
|
-
describe "with no original version of the task exists" do
|
10
|
-
|
11
|
-
it "appends :original to the task name" do
|
12
|
-
task = Rake::Task.define_task(task_name)
|
13
|
-
|
14
|
-
Rake.application.override_task(task_name)
|
15
|
-
|
16
|
-
task.must_equal Rake.application.lookup('original', [task_name])
|
17
|
-
end
|
18
|
-
|
19
|
-
it "creates a new task in place of the original" do
|
20
|
-
Rake::Task.define_task(task_name)
|
21
|
-
task = Rake.application.override_task(task_name => [:dependency])
|
22
|
-
|
23
|
-
task.must_be_instance_of Rake::Task
|
24
|
-
end
|
25
|
-
|
26
|
-
it "adds the supplied dependencies to the new task" do
|
27
|
-
dependency = ['db:seed:dependency']
|
28
|
-
|
29
|
-
Rake::Task.define_task(task_name)
|
30
|
-
task = Rake.application.override_task(task_name => dependency)
|
31
|
-
|
32
|
-
task.prerequisites.must_equal dependency
|
33
|
-
end
|
34
|
-
|
35
|
-
it "adds the supplied action to the new task" do
|
36
|
-
action = proc {}
|
37
|
-
Rake::Task.define_task(task_name)
|
38
|
-
task = Rake.application.override_task(task_name, &action)
|
39
|
-
|
40
|
-
task.actions.must_equal [action]
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
describe "when the original version of the task exists" do
|
45
|
-
|
46
|
-
let(:original_name) { [task_name, 'original'].join(':') }
|
47
|
-
|
48
|
-
it "leaves the original task in place" do
|
49
|
-
task = Rake::Task.define_task(task_name)
|
50
|
-
original = Rake::Task.define_task(original_name)
|
51
|
-
new_task = Rake.application.override_task(task_name)
|
52
|
-
|
53
|
-
Rake::Task[task_name].must_equal task
|
54
|
-
end
|
55
|
-
|
56
|
-
it "leaves the task:original task in place" do
|
57
|
-
task = Rake::Task.define_task(task_name)
|
58
|
-
original = Rake::Task.define_task(original_name)
|
59
|
-
new_task = Rake.application.override_task(task_name)
|
60
|
-
|
61
|
-
Rake::Task[original].must_equal original
|
62
|
-
end
|
63
|
-
|
64
|
-
it "does not create a new task" do
|
65
|
-
task = Rake::Task.define_task(task_name)
|
66
|
-
original = Rake::Task.define_task(original_name)
|
67
|
-
new_task = Rake.application.override_task(task_name)
|
68
|
-
|
69
|
-
new_task.must_be_nil
|
70
|
-
end
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|
74
|
-
describe "rename_task" do
|
75
|
-
|
76
|
-
let(:original_name) { 'original_name' }
|
77
|
-
let(:new_name) { 'new_name' }
|
78
|
-
|
79
|
-
before do
|
80
|
-
Rake::Task.define_task(original_name)
|
81
|
-
end
|
82
|
-
|
83
|
-
it "changes the tasks name" do
|
84
|
-
task = Rake::Task[original_name]
|
85
|
-
flexmock(task).should_receive(:rename).with(new_name).once
|
86
|
-
|
87
|
-
Rake.application.rename_task(original_name, new_name)
|
88
|
-
end
|
89
|
-
|
90
|
-
it "removes the original task name" do
|
91
|
-
Rake.application.rename_task(original_name, new_name)
|
92
|
-
|
93
|
-
Rake.application.lookup(original_name).must_be_nil
|
94
|
-
end
|
95
|
-
|
96
|
-
it "give the original task the new name" do
|
97
|
-
original_task = Rake::Task[original_name]
|
98
|
-
|
99
|
-
Rake.application.rename_task(original_name, new_name)
|
100
|
-
|
101
|
-
Rake.application.lookup(new_name).must_equal original_task
|
102
|
-
end
|
103
|
-
end
|
104
|
-
end
|