seedomatic 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -14,8 +14,8 @@ module SeedOMatic
14
14
  new_records = 0
15
15
  updated_records = 0
16
16
 
17
- items.each do |i|
18
- model = model_class.send(create_method, *create_args(i))
17
+ items.each do |attrs|
18
+ model = model_class.send(create_method, *create_args(attrs))
19
19
 
20
20
  if model.new_record?
21
21
  new_records += 1
@@ -24,7 +24,7 @@ module SeedOMatic
24
24
  end
25
25
 
26
26
  if model.new_record? || seed_mode == 'always'
27
- model.attributes = i
27
+ model.attributes = process_lookups(attrs)
28
28
  model.save!
29
29
  end
30
30
  end
@@ -34,6 +34,16 @@ module SeedOMatic
34
34
 
35
35
  protected
36
36
 
37
+ def process_lookups(attrs)
38
+ attrs.select{|k| k.ends_with? "_lookup"}.each do |key, value|
39
+ association = key.gsub("_lookup", "")
40
+ lookup_class = model_class.reflect_on_association(association).klass
41
+
42
+ attrs[association] = lookup_class.where(value).first
43
+ end
44
+ attrs
45
+ end
46
+
37
47
  def create_method
38
48
  match_on.empty? ? 'new' : "find_or_initialize_by_#{match_on.join('_and_')}"
39
49
  end
@@ -1,3 +1,3 @@
1
1
  module Seedomatic
2
- VERSION = "0.4.0"
2
+ VERSION = "0.5.0"
3
3
  end
data/readme.markdown CHANGED
@@ -14,6 +14,8 @@ specify another directory when you run your seeds. Here's the structure of a typ
14
14
  items:
15
15
  - name: My Model 1
16
16
  code: my_model
17
+ other_model_lookup:
18
+ code: om_1
17
19
  - name: My Model 2
18
20
  code: my_model_2
19
21
  other_model:
@@ -63,6 +65,27 @@ fields). The following options are available:
63
65
 
64
66
  The default seed mode is `always`.
65
67
 
68
+ ### Lookups
69
+
70
+ If your seeds depend on other seeded data, you can use *lookups* to locate other records. Just add _lookup to the name of any property, and then provide parameters to look up that data. Seed-O-Matic will search for a record that matches the criteria you specified. Seed-O-Matic will automatically determine the type of record to look up based on the association in your model.
71
+
72
+ *Warning*: Currently, lookups will only work correctly if the record exists at the time that the seed was run. If the item you're looking up is located in another seed file, ensure that it is imported first (by putting it in a higher alphabetical order).
73
+
74
+ Examples:
75
+
76
+ #### With key / value lookup parameters
77
+
78
+ category_lookup:
79
+ code: lookup_code
80
+
81
+ This will set the `category` of the lookup to the first category with a `code` of `lookup_code`
82
+
83
+ #### With a string
84
+
85
+ category_lookup: name ilike '%code%'
86
+
87
+ This will look for category records matching the SQL fragment you passed in.
88
+
66
89
  ### Importing specific directories, or specific files.
67
90
 
68
91
  To load seed files from a specific directory, call:
@@ -1,6 +1,9 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe SeedOMatic::Seeder do
4
+
5
+ before { MyModel.reset }
6
+
4
7
  let!(:mock_model) { mock(MyModel).as_null_object }
5
8
  let(:existing_mock) { MyModel.create(:name => 'Existing') }
6
9
  let(:new_mock) { MyModel.new(:name => 'New') }
@@ -38,6 +41,17 @@ describe SeedOMatic::Seeder do
38
41
  MyModel[1].name.should == 'bar'
39
42
  end
40
43
  end
44
+
45
+ context "lookup fields" do
46
+ let(:items) { [{'name' => 'foo', 'category_lookup' => { 'code' => 'bar' }}]}
47
+ let(:category) { MyCategory.new }
48
+ specify {
49
+ MyModel.should_receive(:reflect_on_association).with('category').and_return(OpenStruct.new(:klass => MyCategory))
50
+ MyCategory.should_receive(:where).with(hash_including('code' => 'bar')).and_return(OpenStruct.new(:first => category))
51
+ subject
52
+ MyModel[0].category.should == category
53
+ }
54
+ end
41
55
  end
42
56
 
43
57
  describe "checking uniqueness on a specified field" do
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,4 @@
1
1
  require 'seedomatic'
2
2
 
3
- require 'support/my_model.rb'
3
+ require 'support/my_model.rb'
4
+ require 'ostruct'
@@ -1,13 +1,22 @@
1
+ class MyCategory
2
+
3
+ end
4
+
1
5
  class MyModel
2
- attr_accessor :name, :new_record
6
+ attr_accessor :name, :new_record, :category
3
7
 
4
8
  @@models = []
5
9
 
6
10
  def initialize(params = {})
7
11
  self.name = params['name']
12
+ self.category = params['category']
8
13
  self.new_record = true
9
14
  end
10
15
 
16
+ def self.reset
17
+ @@models = []
18
+ end
19
+
11
20
  def self.create(params = {})
12
21
  @@models << m = new(params)
13
22
  m.new_record = false
@@ -16,6 +25,7 @@ class MyModel
16
25
 
17
26
  def attributes=(attr)
18
27
  self.name = attr['name']
28
+ self.category = attr['category']
19
29
  end
20
30
 
21
31
  def save!
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.4.0
4
+ version: 0.5.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-26 00:00:00.000000000 Z
12
+ date: 2012-04-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &70296477233420 !ruby/object:Gem::Requirement
16
+ requirement: &70286621376940 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 2.9.0
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70296477233420
24
+ version_requirements: *70286621376940
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: activesupport
27
- requirement: &70296477231760 !ruby/object:Gem::Requirement
27
+ requirement: &70286621376520 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70296477231760
35
+ version_requirements: *70286621376520
36
36
  description: Create repeatable seed fixtures that you can continually use and deploy
37
37
  to multiple environments and tenants.
38
38
  email: