mongoid_seeder 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +2 -2
- data/README.md +159 -2
- data/lib/mongoid_seeder.rb +1 -1
- data/lib/mongoid_seeder/version.rb +1 -1
- metadata +4 -4
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
mongoid_seeder (0.0.
|
4
|
+
mongoid_seeder (0.0.4)
|
5
5
|
mixlib-config (~> 1.1)
|
6
6
|
mongoid (~> 3.1)
|
7
7
|
mongoid-shell (= 0.2.0)
|
@@ -27,7 +27,7 @@ GEM
|
|
27
27
|
i18n
|
28
28
|
mongoid
|
29
29
|
moped (1.5.1)
|
30
|
-
multi_json (1.8.
|
30
|
+
multi_json (1.8.1)
|
31
31
|
origin (1.1.0)
|
32
32
|
rake (10.1.0)
|
33
33
|
tzinfo (0.3.37)
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# MongoidSeeder
|
2
2
|
|
3
|
-
|
3
|
+
Provides basic behavior to seed data while using Mongoid.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -18,7 +18,164 @@ Or install it yourself as:
|
|
18
18
|
|
19
19
|
## Usage
|
20
20
|
|
21
|
-
|
21
|
+
Place an output of `mongodump` in `db/seed_data/`. This will be used to seed your database. *View the Custom Behavior section to learn how to override this path.*
|
22
|
+
|
23
|
+
### Rake Task
|
24
|
+
|
25
|
+
#### Rails
|
26
|
+
|
27
|
+
In rails, add this to `db/seeds.rb` to bootstrap the default seed rake tasks:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
require 'mongoid_seeder'
|
31
|
+
|
32
|
+
puts "Loading Seed Data"
|
33
|
+
|
34
|
+
if some_condition_to_decide_to_seed_or_not
|
35
|
+
puts "Importing data at #{MongoidSeeder.db_path}\n"
|
36
|
+
MongoidSeeder.seed
|
37
|
+
puts "\nFinished.\n"
|
38
|
+
else
|
39
|
+
puts "Data already exists, no need to seed."
|
40
|
+
end
|
41
|
+
```
|
42
|
+
|
43
|
+
#### Ruby
|
44
|
+
|
45
|
+
If you are not using rails, or would like more functionality with rake, add the following to `lib/tasks/db.rake`:
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
namespace :db do
|
49
|
+
desc 'Seed the database'
|
50
|
+
task :seed => :prepare do
|
51
|
+
puts "Loading Seed Data"
|
52
|
+
|
53
|
+
if some_condition_to_decide_to_seed_or_not
|
54
|
+
MongoidSeeder.seed
|
55
|
+
puts "Importing data at #{MongoidSeeder.db_path}"
|
56
|
+
else
|
57
|
+
puts "Data already exists, no need to seed."
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
desc 'Drops the database'
|
62
|
+
task :drop => :prepare do
|
63
|
+
MongoidSeeder.drop
|
64
|
+
end
|
65
|
+
|
66
|
+
desc 'Dumps the database to the seed location'
|
67
|
+
task :dump => :prepare do
|
68
|
+
MongoidSeeder.dump
|
69
|
+
puts "Dumping data to #{MongoidSeeder.db_path}"
|
70
|
+
end
|
71
|
+
|
72
|
+
desc 'Drops db and seeds it'
|
73
|
+
task :reseed => [:drop, :seed]
|
74
|
+
|
75
|
+
task :prepare do
|
76
|
+
require 'mongoid_seeder'
|
77
|
+
# Do stuff to setup your app environment
|
78
|
+
end
|
79
|
+
end
|
80
|
+
```
|
81
|
+
|
82
|
+
### Rspec
|
83
|
+
|
84
|
+
Use within your rspec tests like so:
|
85
|
+
|
86
|
+
```ruby
|
87
|
+
require 'mongoid_seeder'
|
88
|
+
|
89
|
+
RSpec.configure do |config|
|
90
|
+
config.before(:suite) do
|
91
|
+
MongoidSeeder.before_suite
|
92
|
+
end
|
93
|
+
|
94
|
+
config.before(:each) do
|
95
|
+
MongoidSeeder.before_each
|
96
|
+
end
|
97
|
+
|
98
|
+
config.after(:each) do
|
99
|
+
MongoidSeeder.after_each
|
100
|
+
end
|
101
|
+
|
102
|
+
config.after(:suite) do
|
103
|
+
MongoidSeeder.after_suite
|
104
|
+
end
|
105
|
+
end
|
106
|
+
```
|
107
|
+
|
108
|
+
### Cucumber
|
109
|
+
|
110
|
+
```ruby
|
111
|
+
require 'mongoid_seeder'
|
112
|
+
|
113
|
+
Before do
|
114
|
+
MongoidSeeder.before_suite unless @cucumber_started # Only happen once
|
115
|
+
@cucumber_started = true
|
116
|
+
MongoidSeeder.before_each
|
117
|
+
end
|
118
|
+
|
119
|
+
After do
|
120
|
+
MongoidSeeder.after_each
|
121
|
+
end
|
122
|
+
|
123
|
+
at_exit do
|
124
|
+
MongoidSeeder.after_suite
|
125
|
+
end
|
126
|
+
```
|
127
|
+
|
128
|
+
## Default Behavior
|
129
|
+
|
130
|
+
By default these methods run the following:
|
131
|
+
|
132
|
+
Method | Default Behavior
|
133
|
+
---|---
|
134
|
+
`before_suite` | Seeds the database
|
135
|
+
`before_each` | no-op
|
136
|
+
`after_each` | no-op
|
137
|
+
`after_suite` | Drops the database
|
138
|
+
|
139
|
+
## Custom Behavior
|
140
|
+
|
141
|
+
### Bootstrapping the mongoid seeder
|
142
|
+
|
143
|
+
To bootstrap the seeder with custom configuration, place all configuration in a file located at `config/mongoid_seeder.rb`. This file will be loaded when the seeder is ran if it exists.
|
144
|
+
|
145
|
+
### DB Path
|
146
|
+
|
147
|
+
You can configure the location where the seed data is stored by doing the following:
|
148
|
+
|
149
|
+
```ruby
|
150
|
+
require_relative '../config/environment'
|
151
|
+
|
152
|
+
MongoidSeeder::Config.seed_dir = "/path/to/my/seeds"
|
153
|
+
```
|
154
|
+
|
155
|
+
You may want to do this if you want to use different sets of seed data for different environments.
|
156
|
+
|
157
|
+
### Callback Methods
|
158
|
+
|
159
|
+
*The default behavior cannot be overridden, you can only prepend to the behavior*
|
160
|
+
|
161
|
+
This means that you can add additional steps to be ran for each method if you would like.
|
162
|
+
|
163
|
+
```
|
164
|
+
MongoidSeeder::Config.before_suite = -> do
|
165
|
+
MyApp.load_env
|
166
|
+
end
|
167
|
+
|
168
|
+
# Must use old lambda syntax because of the argument
|
169
|
+
# This will drop all collections that includes "v2" in the collection name
|
170
|
+
# after each test is ran
|
171
|
+
MongoidSeeder::Config.condition_to_drop_collection = lambda do |a|
|
172
|
+
a.name.include?('v2')
|
173
|
+
end
|
174
|
+
```
|
175
|
+
|
176
|
+
## Owners
|
177
|
+
|
178
|
+
Developed by [@coffeencoke](http://github.com/coffeencoke), [@dcameronmauch](http://github.com/dcameronmauch), devs on a team at [Asynchrony](http://asynchrony.com).
|
22
179
|
|
23
180
|
## Contributing
|
24
181
|
|
data/lib/mongoid_seeder.rb
CHANGED
@@ -47,6 +47,6 @@ module MongoidSeeder
|
|
47
47
|
end
|
48
48
|
|
49
49
|
def self.after_tests
|
50
|
-
Mongoid.session(:default).collections.select{|a| Config.condition_to_drop_collection.call(a) }.each
|
50
|
+
Mongoid.session(:default).collections.select{|a| Config.condition_to_drop_collection.call(a) }.each{|a| a.where.remove_all }
|
51
51
|
end
|
52
52
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid_seeder
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -11,7 +11,7 @@ authors:
|
|
11
11
|
autorequire:
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
|
-
date: 2013-09
|
14
|
+
date: 2013-10-09 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: bundler
|
@@ -126,7 +126,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
126
126
|
version: '0'
|
127
127
|
segments:
|
128
128
|
- 0
|
129
|
-
hash:
|
129
|
+
hash: 4468195172537555349
|
130
130
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
131
131
|
none: false
|
132
132
|
requirements:
|
@@ -135,7 +135,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
135
135
|
version: '0'
|
136
136
|
segments:
|
137
137
|
- 0
|
138
|
-
hash:
|
138
|
+
hash: 4468195172537555349
|
139
139
|
requirements: []
|
140
140
|
rubyforge_project:
|
141
141
|
rubygems_version: 1.8.25
|