seedomatic 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,12 +1,13 @@
1
1
  module SeedOMatic
2
2
  class Seeder
3
3
 
4
- attr_accessor :model_name, :items, :match_on
4
+ attr_accessor :model_name, :items, :match_on, :seed_mode
5
5
 
6
6
  def initialize(data)
7
7
  @model_name = data[:model_name]
8
8
  @items = data[:items]
9
9
  @match_on = [*data[:match_on]]
10
+ @seed_mode = data[:seed_mode] || "always"
10
11
  end
11
12
 
12
13
  def import
@@ -22,8 +23,10 @@ module SeedOMatic
22
23
  updated_records += 1
23
24
  end
24
25
 
25
- model.attributes = i
26
- model.save!
26
+ if model.new_record? || seed_mode == 'always'
27
+ model.attributes = i
28
+ model.save!
29
+ end
27
30
  end
28
31
 
29
32
  { :count => items.length, :new => new_records, :updated => updated_records}
@@ -1,3 +1,3 @@
1
1
  module Seedomatic
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
data/readme.markdown CHANGED
@@ -39,6 +39,16 @@ Running seeds is as easy as calling:
39
39
 
40
40
  This will look through the directory `config/seeds`, and import every file in that directory.
41
41
 
42
+ ### Seed Mode
43
+
44
+ You can set a *seed mode* to control how SeedOMatic handles entries that already exist (i.e. models that matched the match_on
45
+ fields). The following options are available:
46
+
47
+ * `once` - Create new models, but don't update any existing models.
48
+ * `always` - Always update, even if the model already exists.
49
+
50
+ The default seed mode is `always`.
51
+
42
52
  ### Importing specific directories, or specific files.
43
53
 
44
54
  To load seed files from a specific directory, call:
@@ -59,7 +69,5 @@ You can load tags using the `tagged_with`, and `not_tagged_with` options:
59
69
 
60
70
  ## TODO
61
71
 
62
- * Support for JSON formatted seed files
63
- * Allowing multiple models defined per file
64
72
  * Referencing lookups from other seed files
65
73
  * Selective updating of data
data/seedomatic.gemspec CHANGED
@@ -20,4 +20,5 @@ Gem::Specification.new do |s|
20
20
 
21
21
  # specify any dependencies here; for example:
22
22
  s.add_development_dependency "rspec", '~> 2.9.0'
23
+ s.add_runtime_dependency "activesupport"
23
24
  end
@@ -2,13 +2,16 @@ require 'spec_helper'
2
2
 
3
3
  describe SeedOMatic::Seeder do
4
4
  let!(:mock_model) { mock(MyModel).as_null_object }
5
+ let(:existing_mock) { MyModel.create(:name => 'Existing') }
6
+ let(:new_mock) { MyModel.new(:name => 'New') }
5
7
 
6
8
  let(:seeder) { described_class.new(data) }
7
- let(:data) { { :model_name => model_name, :items => items, :match_on => match_on } }
9
+ let(:data) { { :model_name => model_name, :items => items, :seed_mode => seed_mode, :match_on => match_on } }
8
10
  let(:model_name) { "MyModel" }
9
11
  let(:items) {
10
12
  [{'name' => 'foo', 'code' => 'uniquecode', 'code_category' => 'more_unique'} ]
11
13
  }
14
+ let(:seed_mode) { nil }
12
15
  let(:match_on) { nil }
13
16
 
14
17
  describe "import" do
@@ -76,6 +79,38 @@ describe SeedOMatic::Seeder do
76
79
  end
77
80
  end
78
81
 
82
+ describe "seed_mode" do
83
+ subject { seeder.import }
84
+
85
+ let(:items) {
86
+ [{'name' => 'foo', 'code' => 'existing'}, {'name' => 'bar', 'code' => 'new'} ]
87
+ }
88
+ let(:match_on) { 'code' }
89
+ before {
90
+ MyModel.stub(:find_or_initialize_by_code).with('existing').and_return(existing_mock)
91
+ MyModel.stub(:find_or_initialize_by_code).with('new').and_return(new_mock)
92
+ }
93
+
94
+ context "once" do
95
+ let(:seed_mode) { 'once' }
96
+
97
+ it "should only save new models" do
98
+ existing_mock.should_not_receive(:save!)
99
+ new_mock.should_receive(:save!)
100
+ subject
101
+ end
102
+ end
103
+ context "always" do
104
+ let(:seed_mode) { 'always' }
105
+
106
+ it "should save multiple models" do
107
+ existing_mock.should_receive(:save!)
108
+ new_mock.should_receive(:save!)
109
+ subject
110
+ end
111
+ end
112
+ end
113
+
79
114
  describe "create_args" do
80
115
  subject { seeder.send(:create_args, {'code' => 1, 'code_category' => 2}) }
81
116
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: seedomatic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-23 00:00:00.000000000 Z
12
+ date: 2012-04-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &70143477327600 !ruby/object:Gem::Requirement
16
+ requirement: &70123092620800 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,7 +21,18 @@ dependencies:
21
21
  version: 2.9.0
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70143477327600
24
+ version_requirements: *70123092620800
25
+ - !ruby/object:Gem::Dependency
26
+ name: activesupport
27
+ requirement: &70123096919180 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70123096919180
25
36
  description: Create repeatable seed fixtures that you can continually use and deploy
26
37
  to multiple environments and tenants.
27
38
  email: