seedify 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1cb5a60e7806659a97c2a2931e0e1e1920306613
4
- data.tar.gz: 6eee00b3687a000ae17412d92687a344bcade309
3
+ metadata.gz: 966953a8b2503201652c70f8eec6ad1138602ef3
4
+ data.tar.gz: ccb99890b1f877f49550266a780f0b4b22558680
5
5
  SHA512:
6
- metadata.gz: 08a8b1e06d6c7a749ec5fa000dd6c591b47f68f6034e35628a1007f7401eda172fb6d55495721038e0bd4594ae325e326bb99ee44c293bd1746fe91423fd3d3d
7
- data.tar.gz: cc5d449ed7b3a37740eba73e6e2fc47d64c10f735797c7d5bd8aaa6c2dec97190f8d376199e5f7d1328a5e6b3b5a0416b0ad4d948e439061f19ff61521564539
6
+ metadata.gz: 091a2840a37eec2966cf5b848a39f2009cf9824c3637cc8442fe153b14b7c6b32ef0dc1002284c45c05369572ff642d58cb89f8b856766e5ad1c1315cbc2a2fd
7
+ data.tar.gz: 9b497bbe567544ca8038ab4d2b25981fa8f0dd2de99305806da3a49eabe09e62977858a272c10094c136aa889ca67a1d2276e43d9eb426eaa028867121dbbdc5
data/README.md CHANGED
@@ -12,7 +12,7 @@ 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 db/console
15
+ - invoke seeds as rake tasks or from within the app/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
@@ -274,6 +274,38 @@ UserSeed.call user_prefix: 'user_generated_by_app_seed'
274
274
 
275
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.
276
276
 
277
+ ### Callbacks and multi-tenancy
278
+
279
+ If you have code that needs to be run before whole seed suite (similar to `before(:all)` in RSpec), you can use the `before_all` method. It'll be called just once - the first time seed using it or its descendants will be called. Common use case is to pre-set proper schema with multi-tenant application using the [apartment](https://github.com/influitive/apartment) gem:
280
+
281
+ ```ruby
282
+ class TenantSeed < Seedify::Base
283
+ param_reader :tenant
284
+
285
+ before_all :switch_tenant
286
+
287
+ def call
288
+ SomeSeed.call
289
+ OtherSeed.call
290
+ end
291
+
292
+ protected
293
+
294
+ def switch_tenant
295
+ log "switching to *#{tenant}* schema"
296
+ Apartment::Tenant.switch!(tenant)
297
+ end
298
+ end
299
+
300
+ class SomeSeed < TenantSeed
301
+ end
302
+
303
+ class OtherSeed < TenantSeed
304
+ end
305
+ ```
306
+
307
+ You can still call tenant seed or some/other seeds separately and proper schema will always be picked.
308
+
277
309
  ## Configuration
278
310
 
279
311
  You can override the default `db/seeds` seed directory if you want to place your seed objects in different place in the project:
@@ -1,6 +1,7 @@
1
1
  require 'active_support'
2
2
 
3
3
  require 'seedify/param_reader'
4
+ require 'seedify/callbacks'
4
5
  require 'seedify/base'
5
6
  require 'seedify/logger'
6
7
  require 'seedify/storage'
@@ -1,6 +1,7 @@
1
1
  module Seedify
2
2
  class Base
3
3
  extend Seedify::ParamReader
4
+ extend Seedify::Callbacks
4
5
 
5
6
  attr_reader :params
6
7
 
@@ -3,7 +3,15 @@ module Seedify
3
3
  class << self
4
4
  def call(proc)
5
5
  stack.push(proc)
6
- proc.call
6
+
7
+ if proc.class.respond_to?(:get_callbacks)
8
+ Seedify::Callbacks.with_callbacks(proc) do
9
+ proc.call
10
+ end
11
+ else
12
+ proc.call
13
+ end
14
+
7
15
  stack.pop
8
16
  end
9
17
 
@@ -0,0 +1,41 @@
1
+ module Seedify
2
+ module Callbacks
3
+ TYPES = %w{before_all}
4
+
5
+ def self.with_callbacks(proc)
6
+ @finished_before_all ||= []
7
+
8
+ proc.class.get_callbacks(:before_all).each do |callback|
9
+ unless @finished_before_all.include?(callback)
10
+ @finished_before_all << callback
11
+
12
+ proc.send(callback)
13
+ end
14
+ end
15
+
16
+ yield
17
+ end
18
+
19
+ def inherited(subclass)
20
+ super
21
+
22
+ TYPES.each do |type|
23
+ get_callbacks(type).each do |callback|
24
+ subclass.send(type, callback)
25
+ end
26
+ end
27
+ end
28
+
29
+ TYPES.each do |type|
30
+ define_method type do |callback|
31
+ @callbacks ||= {}
32
+ @callbacks[type] ||= []
33
+ @callbacks[type] << callback
34
+ end
35
+ end
36
+
37
+ def get_callbacks(type)
38
+ Array((@callbacks || {})[type.to_s])
39
+ end
40
+ end
41
+ end
@@ -1,6 +1,8 @@
1
1
  module Seedify
2
2
  module ParamReader
3
3
  def inherited(subclass)
4
+ super
5
+
4
6
  get_param_readers.each do |param_name, options|
5
7
  subclass.param_reader param_name, options
6
8
  end
@@ -1,3 +1,3 @@
1
1
  module Seedify
2
- VERSION = '0.2.0'
2
+ VERSION = '0.3.0'
3
3
  end
@@ -14,7 +14,7 @@ namespace :seedify do
14
14
  next if seed == 'ApplicationSeed'
15
15
 
16
16
  desc "Call the #{seed.underscore.humanize}"
17
- task seed.underscore.gsub('/', ':').sub(/_seed$/, '') => :environment do
17
+ task seed.underscore.gsub('/', ':').sub(/_seed$/, '').sub(/:base$/, '') => :environment do
18
18
  seed.constantize.call(task: true)
19
19
  end
20
20
  end
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.2.0
4
+ version: 0.3.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-24 00:00:00.000000000 Z
11
+ date: 2015-05-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -136,6 +136,7 @@ files:
136
136
  - lib/seedify.rb
137
137
  - lib/seedify/base.rb
138
138
  - lib/seedify/call_stack.rb
139
+ - lib/seedify/callbacks.rb
139
140
  - lib/seedify/logger.rb
140
141
  - lib/seedify/param_reader.rb
141
142
  - lib/seedify/param_value.rb