seedify 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +32 -7
- data/lib/seedify/logger.rb +12 -2
- data/lib/seedify/railtie.rb +4 -0
- data/lib/seedify/storage.rb +5 -5
- data/lib/seedify/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1cb5a60e7806659a97c2a2931e0e1e1920306613
|
4
|
+
data.tar.gz: 6eee00b3687a000ae17412d92687a344bcade309
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 08a8b1e06d6c7a749ec5fa000dd6c591b47f68f6034e35628a1007f7401eda172fb6d55495721038e0bd4594ae325e326bb99ee44c293bd1746fe91423fd3d3d
|
7
|
+
data.tar.gz: cc5d449ed7b3a37740eba73e6e2fc47d64c10f735797c7d5bd8aaa6c2dec97190f8d376199e5f7d1328a5e6b3b5a0416b0ad4d948e439061f19ff61521564539
|
data/README.md
CHANGED
@@ -6,17 +6,31 @@
|
|
6
6
|
[](https://codeclimate.com/github/visualitypl/seedify)
|
7
7
|
[](https://codeclimate.com/github/visualitypl/seedify)
|
8
8
|
|
9
|
-
Let your seed code become a first-class member of the Rails app and put it into seed objects. **seedify** allows to implement and organize seeds in object-oriented fashion, putting them into `
|
9
|
+
Let your seed code become a first-class member of the Rails app and put it into seed objects. **seedify** allows to implement and organize seeds in object-oriented fashion, putting them into `db/seeds` which organizes seeds much like `app/models` does with models etc. It also provides handy syntax for command line parameters and progress logging.
|
10
10
|
|
11
11
|
Here's an overview of what you can achieve with **seedify**:
|
12
12
|
|
13
13
|
- organize seed code in object-oriented, Rails convention fitting fashion
|
14
14
|
- take advantage of inheritance and modularization when writing seeds
|
15
|
-
- invoke seeds as rake tasks or from within the
|
15
|
+
- invoke seeds as rake tasks or from within the db/console
|
16
16
|
- allow to specify custom parameters for your seeds, typed and with defaults
|
17
17
|
- log the seed progress (e.g. mass creation) without effort
|
18
18
|
- combine with [factory_girl](https://github.com/thoughtbot/factory_girl) to simplify object creation and share fixtures with specs
|
19
19
|
|
20
|
+
## Installation
|
21
|
+
|
22
|
+
First, add **seedify** to `Gemfile`. Be sure to add it just to group(s) you'll actually use it in:
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
gem 'seedify', group: 'development'
|
26
|
+
```
|
27
|
+
|
28
|
+
Then run:
|
29
|
+
|
30
|
+
```sh
|
31
|
+
bundle install
|
32
|
+
```
|
33
|
+
|
20
34
|
## Usage
|
21
35
|
|
22
36
|
### Basic example
|
@@ -24,7 +38,7 @@ Here's an overview of what you can achieve with **seedify**:
|
|
24
38
|
You should start by implementing the `ApplicationSeed` class:
|
25
39
|
|
26
40
|
```ruby
|
27
|
-
#
|
41
|
+
# db/seeds/application_seed.rb
|
28
42
|
class ApplicationSeed < Seedify::Base
|
29
43
|
def call
|
30
44
|
if Admin.empty?
|
@@ -183,7 +197,7 @@ end
|
|
183
197
|
You can separate the seed code into domains that fit your application best (including modules). In this case, we'll create model-oriented seeds for `Admin` and `User` models and keep application-wide code and param readers in application seed:
|
184
198
|
|
185
199
|
```ruby
|
186
|
-
#
|
200
|
+
# db/seeds/application_seed.rb
|
187
201
|
class ApplicationSeed < Seed::Base
|
188
202
|
include FactoryGirl::Syntax::Methods
|
189
203
|
|
@@ -208,7 +222,7 @@ end
|
|
208
222
|
```
|
209
223
|
|
210
224
|
```ruby
|
211
|
-
#
|
225
|
+
# db/seeds/admin_seed.rb
|
212
226
|
class AdminSeed < ApplicationSeed
|
213
227
|
param_reader :admin_email, default: 'admin@example.com'
|
214
228
|
|
@@ -223,7 +237,7 @@ end
|
|
223
237
|
```
|
224
238
|
|
225
239
|
```ruby
|
226
|
-
#
|
240
|
+
# db/seeds/user_seed.rb
|
227
241
|
class UserSeed < ApplicationSeed
|
228
242
|
param_reader :user_prefix, default: 'user'
|
229
243
|
param_reader :user_count, type: :integer, default: 5
|
@@ -254,12 +268,23 @@ You should try to write each seed object in a way that makes it possible to use
|
|
254
268
|
You can customize sub-seed invocation with a syntax you already know - the call parameters:
|
255
269
|
|
256
270
|
```ruby
|
257
|
-
# excerpt from
|
271
|
+
# excerpt from db/seeds/application_seed.rb
|
258
272
|
UserSeed.call user_prefix: 'user_generated_by_app_seed'
|
259
273
|
```
|
260
274
|
|
261
275
|
This way, within **UserSeed**, the `user_prefix` param will equal to *user_generated_by_app_seed* regardless of the one specified when calling the application seed from console or command-line.
|
262
276
|
|
277
|
+
## Configuration
|
278
|
+
|
279
|
+
You can override the default `db/seeds` seed directory if you want to place your seed objects in different place in the project:
|
280
|
+
|
281
|
+
```ruby
|
282
|
+
# excerpt from config/application.rb
|
283
|
+
config.seedify_seed_directory = "#{config.root}/app/seeds"
|
284
|
+
```
|
285
|
+
|
286
|
+
Remember that everything you put into `app/*` gets preloaded in production so putting your seeds there makes sense only if you actually want to use them in production. That's why they live in `db/seeds` by default.
|
287
|
+
|
263
288
|
## Contributing
|
264
289
|
|
265
290
|
1. Fork it (https://github.com/visualitypl/seedify/fork)
|
data/lib/seedify/logger.rb
CHANGED
@@ -2,6 +2,16 @@ module Seedify
|
|
2
2
|
class Logger
|
3
3
|
attr_reader :context
|
4
4
|
|
5
|
+
def self.seed_name_formatter(name)
|
6
|
+
name.to_s.sub(/Seed$/, '')
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.max_seed_name_length
|
10
|
+
@max_seed_name_length ||= Seedify::Storage.seed_list.map do |name|
|
11
|
+
seed_name_formatter(name).length
|
12
|
+
end.max
|
13
|
+
end
|
14
|
+
|
5
15
|
def initialize(context)
|
6
16
|
@context = context
|
7
17
|
end
|
@@ -73,8 +83,8 @@ module Seedify
|
|
73
83
|
end
|
74
84
|
|
75
85
|
def name
|
76
|
-
text = context.class
|
77
|
-
text = (' ' * [0,
|
86
|
+
text = self.class.seed_name_formatter(context.class)
|
87
|
+
text = (' ' * [0, self.class.max_seed_name_length - text.length].max) + text
|
78
88
|
|
79
89
|
colorize(text, 32)
|
80
90
|
end
|
data/lib/seedify/railtie.rb
CHANGED
@@ -2,6 +2,10 @@ module Seedify
|
|
2
2
|
class Railtie < Rails::Railtie
|
3
3
|
railtie_name :seedify
|
4
4
|
|
5
|
+
initializer 'seedify.autoload', before: :set_autoload_paths do |app|
|
6
|
+
app.config.autoload_paths << Seedify::Storage.seed_directory
|
7
|
+
end
|
8
|
+
|
5
9
|
rake_tasks do
|
6
10
|
load 'tasks/seedify.rake'
|
7
11
|
end
|
data/lib/seedify/storage.rb
CHANGED
@@ -2,7 +2,11 @@ module Seedify
|
|
2
2
|
module Storage
|
3
3
|
class << self
|
4
4
|
def seed_directory
|
5
|
-
|
5
|
+
begin
|
6
|
+
Rails.application.config.seedify_seed_directory || raise
|
7
|
+
rescue
|
8
|
+
Rails.root.join('db', 'seeds')
|
9
|
+
end
|
6
10
|
end
|
7
11
|
|
8
12
|
def seed_list
|
@@ -10,10 +14,6 @@ module Seedify
|
|
10
14
|
ActiveSupport::Inflector.classify(file.sub(/^#{seed_directory.to_s + "/"}/, '').sub(/#{".rb"}$/, ''))
|
11
15
|
end
|
12
16
|
end
|
13
|
-
|
14
|
-
def max_seed_name_length
|
15
|
-
@max_seed_name_length ||= seed_list.map(&:length).max
|
16
|
-
end
|
17
17
|
end
|
18
18
|
end
|
19
19
|
end
|
data/lib/seedify/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: seedify
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Karol Słuszniak
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-04-
|
11
|
+
date: 2015-04-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -163,7 +163,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
163
163
|
version: '0'
|
164
164
|
requirements: []
|
165
165
|
rubyforge_project:
|
166
|
-
rubygems_version: 2.
|
166
|
+
rubygems_version: 2.4.6
|
167
167
|
signing_key:
|
168
168
|
specification_version: 4
|
169
169
|
summary: Implement and organize seeds in Rail-ish, object-oriented fashion. Handy
|