public
Rubygem
Description: Advanced seed data handling for Rails, combining the best practices of several methods together.
Homepage: http://mbleigh.lighthouseapp.com/projects/10223-seed-fu
Clone URL: git://github.com/mbleigh/seed-fu.git
added .seed_many method to allow you to seed several records through one call
zdennis (author)
Wed Jul 16 13:34:27 -0700 2008
commit  9f7e8b27b7cb175911474a898ea282771ab2a5c3
tree    f5d7f7eb5f1b8d34d4e318e85647125943f84530
parent  93e54062cec39aad5249c4dbcc35bcaa8efcf633
...
32
33
34
35
36
37
38
...
70
71
72
 
 
 
 
 
 
 
 
 
 
 
 
73
74
...
32
33
34
 
35
36
37
...
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
0
@@ -32,7 +32,6 @@ class Seeder
0
       record.send("#{k}=", v)
0
     end
0
     record.save!
0
-    puts " - #{@model_class} #{condition_hash.inspect}"
0
     record
0
   end
0
   
0
@@ -70,4 +69,16 @@ class ActiveRecord::Base
0
   def self.seed(*constraints, &block)
0
     Seeder.plant(self, *constraints, &block)
0
   end
0
+  
0
+  def self.seed_many(*constraints)
0
+    seeds = constraints.pop
0
+    seeds.each do |seed_data|
0
+      seed(*constraints) do |s|
0
+        seed_data.each_pair do |k,v|
0
+          s.send "#{k}=", v
0
+        end
0
+      end
0
+    end
0
+  end
0
+  
0
 end
0
\ No newline at end of file
...
43
44
45
 
 
 
 
 
 
 
 
 
 
 
 
46
47
48
...
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
0
@@ -43,6 +43,18 @@ describe Seeder do
0
     SeededModel.find_by_login("bob").first_name.should == "Steve"
0
   end
0
   
0
+  it "should be able to create models from an array of seed attributes" do
0
+    SeededModel.seed_many(:title, :login, [
0
+      {:login => "bob", :title => "Peon", :first_name => "Steve"},
0
+      {:login => "frank", :title => "Peasant", :first_name => "Francis"},
0
+      {:login => "harry", :title => "Noble", :first_name => "Harry"}
0
+    ])
0
+    
0
+    SeededModel.find_by_login("bob").first_name.should == "Steve"
0
+    SeededModel.find_by_login("frank").first_name.should == "Francis"
0
+    SeededModel.find_by_login("harry").first_name.should == "Harry"
0
+  end
0
+  
0
   #it "should raise an error if constraints are not unique" do
0
   #  SeededModel.create(:login => "bob", :first_name => "Bob", :title => "Peon")
0
   #  SeededModel.create(:login => "bob", :first_name => "Robert", :title => "Manager")

Comments