jcnetdev-seed-fu 1.0.200807042 → 1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,17 +3,23 @@ module SeedFu
3
3
  def self.plant(model_class, *constraints, &block)
4
4
  constraints = [:id] if constraints.empty?
5
5
  seed = Seeder.new(model_class)
6
+
7
+ options = constraints.last if (constraints.last.is_a? Hash)
8
+ constraints.delete_at(*constraints.length-1) if (constraints.last.is_a? Hash)
9
+
10
+ insert_only = constraints.last.is_a? TrueClass
11
+ constraints.delete_at(*constraints.length-1) if (constraints.last.is_a? TrueClass or constraints.last.is_a? FalseClass)
6
12
  seed.set_constraints(*constraints)
7
13
  yield seed
8
- seed.plant!
14
+ seed.plant!({:insert_only => insert_only}.merge(options||{}))
9
15
  end
10
-
16
+
11
17
  def initialize(model_class)
12
18
  @model_class = model_class
13
19
  @constraints = [:id]
14
20
  @data = {}
15
21
  end
16
-
22
+
17
23
  def set_constraints(*constraints)
18
24
  raise "You must set at least one constraint." if constraints.empty?
19
25
  @constraints = []
@@ -22,21 +28,24 @@ module SeedFu
22
28
  @constraints << constraint.to_sym
23
29
  end
24
30
  end
25
-
31
+
26
32
  def set_attribute(name, value)
27
33
  @data[name.to_sym] = value
28
34
  end
29
-
30
- def plant!
35
+
36
+ def plant!(options)
37
+ insert_only = options[:insert_only].nil? ? false : options[:insert_only]
38
+ enable_validation = options[:validate].nil? ? true : options[:validate]
31
39
  record = get
40
+ return if !record.new_record? and insert_only
32
41
  @data.each do |k, v|
33
42
  record.send("#{k}=", v)
34
43
  end
35
- record.save!
36
- puts " - #{@model_class} #{condition_hash.inspect}"
44
+ record.save(enable_validation) || raise(RecordNotSaved)
45
+ puts " - #{@model_class} #{condition_hash.inspect}"
37
46
  record
38
47
  end
39
-
48
+
40
49
  def method_missing(method_name, *args) #:nodoc:
41
50
  if (match = method_name.to_s.match(/(.*)=$/)) && args.size == 1
42
51
  set_attribute(match[1], args.first)
@@ -44,9 +53,9 @@ module SeedFu
44
53
  super
45
54
  end
46
55
  end
47
-
56
+
48
57
  protected
49
-
58
+
50
59
  def get
51
60
  records = @model_class.find(:all, :conditions => condition_hash)
52
61
  raise "Given constraints yielded multiple records." unless records.size < 2
@@ -56,7 +65,7 @@ module SeedFu
56
65
  return @model_class.new
57
66
  end
58
67
  end
59
-
68
+
60
69
  def condition_hash
61
70
  @data.reject{|a,v| !@constraints.include?(a)}
62
71
  end
@@ -73,4 +82,15 @@ class ActiveRecord::Base
73
82
  def self.seed(*constraints, &block)
74
83
  SeedFu::Seeder.plant(self, *constraints, &block)
75
84
  end
85
+
86
+ def self.seed_many(*constraints)
87
+ seeds = constraints.pop
88
+ seeds.each do |seed_data|
89
+ seed(*constraints) do |s|
90
+ seed_data.each_pair do |k,v|
91
+ s.send "#{k}=", v
92
+ end
93
+ end
94
+ end
95
+ end
76
96
  end
@@ -1,9 +1,9 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'seed-fu'
3
- s.version = '1.0.200807042'
4
- s.date = '2008-07-04'
3
+ s.version = '1.1'
4
+ s.date = '2008-09-17'
5
5
 
6
- s.summary = "Allows easier database seeding of tables"
6
+ s.summary = "Allows easier database seeding of tables."
7
7
  s.description = "Seed Fu is an attempt to once and for all solve the problem of inserting and maintaining seed data in a database. It uses a variety of techniques gathered from various places around the web and combines them to create what is hopefully the most robust seed data system around."
8
8
 
9
9
  s.authors = ["Michael Bleigh"]
@@ -43,6 +43,18 @@ describe SeedFu::Seeder do
43
43
  SeededModel.find_by_login("bob").first_name.should == "Steve"
44
44
  end
45
45
 
46
+ it "should be able to create models from an array of seed attributes" do
47
+ SeededModel.seed_many(:title, :login, [
48
+ {:login => "bob", :title => "Peon", :first_name => "Steve"},
49
+ {:login => "frank", :title => "Peasant", :first_name => "Francis"},
50
+ {:login => "harry", :title => "Noble", :first_name => "Harry"}
51
+ ])
52
+
53
+ SeededModel.find_by_login("bob").first_name.should == "Steve"
54
+ SeededModel.find_by_login("frank").first_name.should == "Francis"
55
+ SeededModel.find_by_login("harry").first_name.should == "Harry"
56
+ end
57
+
46
58
  #it "should raise an error if constraints are not unique" do
47
59
  # SeededModel.create(:login => "bob", :first_name => "Bob", :title => "Peon")
48
60
  # SeededModel.create(:login => "bob", :first_name => "Robert", :title => "Manager")
@@ -1,12 +1,13 @@
1
1
  namespace :db do
2
2
  desc "Loads seed data from db/fixtures for the current environment."
3
3
  task :seed => :environment do
4
- Dir[File.join(RAILS_ROOT, 'db', 'fixtures', '*.rb')].sort.each { |fixture|
4
+ fixture_path = ENV["FIXTURE_PATH"] ? ENV["FIXTURE_PATH"] : "db/fixtures"
5
+ Dir[File.join(RAILS_ROOT, fixture_path, '*.rb')].sort.each { |fixture|
5
6
  puts "\n== Seeding from #{File.split(fixture).last} " + ("=" * (60 - (17 + File.split(fixture).last.length)))
6
7
  load fixture
7
8
  puts "=" * 60 + "\n"
8
9
  }
9
- Dir[File.join(RAILS_ROOT, 'db', 'fixtures', RAILS_ENV, '*.rb')].sort.each { |fixture|
10
+ Dir[File.join(RAILS_ROOT, fixture_path, RAILS_ENV, '*.rb')].sort.each { |fixture|
10
11
  puts "\n== [#{RAILS_ENV}] Seeding from #{File.split(fixture).last} " + ("=" * (60 - (20 + File.split(fixture).last.length + RAILS_ENV.length)))
11
12
  load fixture
12
13
  puts "=" * 60 + "\n"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jcnetdev-seed-fu
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.200807042
4
+ version: "1.1"
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Bleigh
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-07-04 00:00:00 -07:00
12
+ date: 2008-09-17 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -67,6 +67,6 @@ rubyforge_project:
67
67
  rubygems_version: 1.2.0
68
68
  signing_key:
69
69
  specification_version: 2
70
- summary: Allows easier database seeding of tables
70
+ summary: Allows easier database seeding of tables.
71
71
  test_files: []
72
72