seed-fu-ndo 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -13,15 +13,6 @@ module SeedFuNdo
13
13
  seeders << seeder
14
14
  end
15
15
 
16
- # Unseed the data of all the recorded seeders in reverse order.
17
- def unseed
18
- ActiveRecord::Base.transaction do
19
- seeders.reverse.each do |seeder|
20
- seeder.unseed
21
- end
22
- end
23
- end
24
-
25
16
  end
26
17
 
27
18
  end
@@ -3,6 +3,8 @@ module SeedFu
3
3
  # Enhanced class from Seed Fu to allow for recording and destroying seed data.
4
4
  class Seeder
5
5
 
6
+ attr_reader :model_class
7
+
6
8
  # Record instead of inserting the data if in recording mode.
7
9
  def seed_with_undo
8
10
  if r = SeedFuNdo.recorder
@@ -19,17 +21,19 @@ module SeedFu
19
21
  # Destroy the seed data.
20
22
  def unseed
21
23
  @model_class.transaction do
22
- @data.reverse.map { |record_data| unseed_record(record_data.symbolize_keys) }
24
+ existing_records.reverse.each { |record| unseed_record(record) }
23
25
  end
24
26
  end
25
27
 
28
+ # List of the seeded records that exist in the database.
29
+ def existing_records
30
+ @data.map { |record_data| find_record(record_data.symbolize_keys) }.compact
31
+ end
32
+
26
33
  private
27
34
 
28
- def unseed_record(data)
29
- record = find_record(data)
30
- return if record.nil?
31
-
32
- puts " - Remove #{@model_class} #{data.inspect}" unless @options[:quiet]
35
+ def unseed_record(record)
36
+ puts " - Remove #{record.inspect}" unless @options[:quiet]
33
37
 
34
38
  record.destroy || raise(ActiveRecord::ActiveRecordError, record.errors.full_messages.join(", ").presence)
35
39
  end
@@ -1,4 +1,4 @@
1
1
  module SeedFuNdo
2
2
  # The current version of Seed Fu-ndo
3
- VERSION = '0.0.1'
3
+ VERSION = '0.0.2'
4
4
  end
data/lib/seed-fu-ndo.rb CHANGED
@@ -11,11 +11,30 @@ module SeedFuNdo
11
11
  # [fixture_paths] The paths to look for seed files in.
12
12
  # [filter] A regexp. If given, only filenames matching this expression will be loaded.
13
13
  def self.unseed(fixture_paths = SeedFu.fixture_paths, filter = nil)
14
+ capture(fixture_paths, filter).reverse.each do |seeder|
15
+ seeder.unseed
16
+ end
17
+ end
18
+
19
+ # Returns all seed records from the given fixture_paths that currently
20
+ # exist in the database. No changes to the database are performed.
21
+ def self.existing_seeds(fixture_paths = SeedFu.fixture_paths, filter = nil)
22
+ existing = Hash.new {|h, k| h[k] = [] }
23
+ capture(fixture_paths, filter).each do |seeder|
24
+ existing[seeder.model_class] += seeder.existing_records
25
+ end
26
+ existing
27
+ end
28
+
29
+ # Captures the seed fixtures without actually inserting anything.
30
+ # Returns a list of seeder objects.
31
+ def self.capture(fixture_paths = SeedFu.fixture_paths, filter = nil)
14
32
  @@recorder = Recorder.new
15
33
  SeedFu::Runner.new(fixture_paths, filter).run
16
- @@recorder.unseed
34
+ @@recorder.seeders
17
35
  ensure
18
36
  @@recorder = nil
19
37
  end
20
38
 
39
+
21
40
  end
data/spec/seeder_spec.rb CHANGED
@@ -44,15 +44,20 @@ describe SeedFu::Seeder do
44
44
  end
45
45
 
46
46
  it "should ignore non-existing records" do
47
- SeedFuNdo.recorder.unseed
47
+ unseed
48
48
  SeededModel.count.should == 0
49
49
  end
50
50
 
51
51
  it "should unseed existing records" do
52
52
  SeededModel.create!(:title => 'Foo')
53
- SeedFuNdo.recorder.unseed
53
+ unseed
54
54
  SeededModel.count.should == 0
55
55
  end
56
+
57
+ it "should find existing records" do
58
+ rec = SeededModel.create!(:title => 'Foo')
59
+ SeedFuNdo.recorder.seeders.first.existing_records.should == [rec]
60
+ end
56
61
  end
57
62
 
58
63
  context "for multiple seeds" do
@@ -71,10 +76,14 @@ describe SeedFu::Seeder do
71
76
  it "should rollback if one record cannot be destroyed" do
72
77
  SeededModel.create!(:title => 'Foo', :fail_to_destroy => true)
73
78
  SeededModel.create!(:title => 'Bar')
74
- expect { SeedFuNdo.recorder.unseed }.to raise_error(ActiveRecord::ActiveRecordError)
79
+ expect { unseed }.to raise_error(ActiveRecord::ActiveRecordError)
75
80
  SeededModel.count.should == 2
76
81
  end
77
82
  end
78
83
 
79
84
  end
80
85
  end
86
+
87
+ def unseed
88
+ SeedFuNdo.recorder.seeders.reverse.each {|s| s.unseed }
89
+ end
data/spec/test.sqlite3 CHANGED
Binary file
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: seed-fu-ndo
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Pascal Zumkehr
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-06-06 00:00:00 Z
18
+ date: 2012-06-14 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  version_requirements: &id001 !ruby/object:Gem::Requirement