soft_destroyable 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -24,17 +24,3 @@ Rake::RDocTask.new(:rdoc) do |rdoc|
24
24
  rdoc.rdoc_files.include('lib/**/*.rb')
25
25
  end
26
26
 
27
- begin
28
- require 'jeweler'
29
- Jeweler::Tasks.new do |gem|
30
- gem.name = "soft_destroyable"
31
- gem.summary = "Rails 3 ActiveRecord compatible soft destroy implementation"
32
- gem.description = "Rails 3 ActiveRecord compatible soft destroy implementation supporting dependent associations"
33
- gem.email = "rockrep@yahoo.com"
34
- gem.homepage = "http://github.com/rockrep/soft_destroyable"
35
- gem.authors = ["Michael Kintzer"]
36
- end
37
- Jeweler::GemcutterTasks.new
38
- rescue LoadError
39
- puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
40
- end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.5
1
+ 0.1.6
@@ -229,7 +229,7 @@ module SoftDestroyable
229
229
  when :has_many
230
230
  restrict_on_non_empty_has_many(reflection, association)
231
231
  when :has_one
232
- restrict_on_nil_has_one(reflection, association)
232
+ restrict_on_non_nil_has_one(reflection, association)
233
233
  else
234
234
  end
235
235
  end
@@ -278,11 +278,21 @@ module SoftDestroyable
278
278
 
279
279
  def restrict_on_non_empty_has_many(reflection, association)
280
280
  return unless association
281
- raise ActiveRecord::DeleteRestrictionError.new(reflection) if !association.empty?
281
+ association.each {|assoc_obj|
282
+ if assoc_obj.respond_to?(:deleted?)
283
+ raise ActiveRecord::DeleteRestrictionError.new(reflection) if !assoc_obj.deleted?
284
+ else
285
+ raise ActiveRecord::DeleteRestrictionError.new(reflection)
286
+ end
287
+ }
282
288
  end
283
289
 
284
- def restrict_on_nil_has_one(reflection, association)
285
- raise ActiveRecord::DeleteRestrictionError.new(reflection) if !association.nil?
290
+ def restrict_on_non_nil_has_one(reflection, association)
291
+ if association.respond_to?(:deleted?)
292
+ raise ActiveRecord::DeleteRestrictionError.new(reflection) if !association.nil? && !association.deleted?
293
+ else
294
+ raise ActiveRecord::DeleteRestrictionError.new(reflection) if !association.nil?
295
+ end
286
296
  end
287
297
 
288
298
  end
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{soft_destroyable}
8
- s.version = "0.1.5"
8
+ s.version = "0.1.6"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Michael Kintzer"]
@@ -14,6 +14,16 @@ class DependentRestrictTest < Test::Unit::TestCase
14
14
  SoftRestrictOne.delete_all
15
15
  end
16
16
 
17
+ def test_destroy_no_restrict_or_soft_restrict_children
18
+ @fred.destroy
19
+ assert_equal true, @fred.deleted?
20
+ end
21
+
22
+ def test_destroy_bang_no_restrict_or_soft_restrict_children
23
+ @fred.destroy!
24
+ assert_nil Parent.find_by_id(@fred.id)
25
+ end
26
+
17
27
  def test_destroy_has_many_restrict_soft_children
18
28
  @fred.soft_restrict_children << pebbles = SoftRestrictChild.new(:name => "pebbles")
19
29
  @fred.soft_restrict_children << bambam = SoftRestrictChild.new(:name => "bambam")
@@ -26,6 +36,17 @@ class DependentRestrictTest < Test::Unit::TestCase
26
36
  assert_equal false, bambam.reload.deleted?
27
37
  end
28
38
 
39
+ def test_destroy_has_many_restrict_soft_children_which_are_deleted
40
+ @fred.soft_restrict_children << pebbles = SoftRestrictChild.new(:name => "pebbles")
41
+ @fred.soft_restrict_children << bambam = SoftRestrictChild.new(:name => "bambam")
42
+ assert_equal 2, @fred.reload.soft_restrict_children.count
43
+ pebbles.destroy
44
+ bambam.destroy
45
+ assert_equal 0, @fred.soft_restrict_children.not_deleted.count
46
+ @fred.destroy
47
+ assert_equal true, @fred.deleted?
48
+ end
49
+
29
50
  def test_destroy_has_many_restrict_children
30
51
  @fred.restrict_children << pebbles = RestrictChild.new(:name => "pebbles")
31
52
  @fred.restrict_children << bambam = RestrictChild.new(:name => "bambam")
@@ -50,6 +71,17 @@ class DependentRestrictTest < Test::Unit::TestCase
50
71
  assert_equal false, bambam.reload.deleted?
51
72
  end
52
73
 
74
+ def test_destroy_bang_has_many_restrict_soft_children_which_are_deleted
75
+ @fred.soft_restrict_children << pebbles = SoftRestrictChild.new(:name => "pebbles")
76
+ @fred.soft_restrict_children << bambam = SoftRestrictChild.new(:name => "bambam")
77
+ assert_equal 2, @fred.reload.soft_restrict_children.count
78
+ pebbles.destroy
79
+ bambam.destroy
80
+ assert_equal 0, @fred.soft_restrict_children.not_deleted.count
81
+ @fred.destroy!
82
+ assert_nil Parent.find_by_id(@fred.id)
83
+ end
84
+
53
85
  def test_destroy_bang_has_many_restrict_children
54
86
  @fred.restrict_children << pebbles = RestrictChild.new(:name => "pebbles")
55
87
  @fred.restrict_children << bambam = RestrictChild.new(:name => "bambam")
@@ -62,7 +94,7 @@ class DependentRestrictTest < Test::Unit::TestCase
62
94
  assert_not_nil RestrictChild.find_by_name("bambam")
63
95
  end
64
96
 
65
- def test_destroys_has_soft_restrict_ones
97
+ def test_destroy_has_soft_restrict_ones
66
98
  @fred.soft_restrict_one = SoftRestrictOne.new(:name => "bambam")
67
99
  assert_equal @fred.reload.soft_restrict_one, SoftRestrictOne.where(:name => "bambam", :parent_id => @fred.id).first
68
100
  assert_raise ActiveRecord::DeleteRestrictionError do
@@ -72,7 +104,17 @@ class DependentRestrictTest < Test::Unit::TestCase
72
104
  assert_not_nil SoftRestrictOne.find_by_name("bambam")
73
105
  end
74
106
 
75
- def test_destroys_has_restrict_ones
107
+ def test_destroy_has_soft_restrict_ones_which_is_deleted
108
+ @fred.soft_restrict_one = bambam = SoftRestrictOne.new(:name => "bambam")
109
+ assert_equal @fred.reload.soft_restrict_one, SoftRestrictOne.where(:name => "bambam", :parent_id => @fred.id).first
110
+ bambam.destroy
111
+ assert_equal true, @fred.reload.soft_restrict_one.deleted?
112
+ @fred.destroy
113
+ assert_equal true, @fred.deleted?
114
+ assert_not_nil SoftRestrictOne.find_by_name("bambam")
115
+ end
116
+
117
+ def test_destroy_has_restrict_ones
76
118
  @fred.restrict_one = RestrictOne.new(:name => "bambam")
77
119
  assert_equal @fred.reload.restrict_one, RestrictOne.where(:name => "bambam", :parent_id => @fred.id).first
78
120
  assert_raise ActiveRecord::DeleteRestrictionError do
@@ -92,6 +134,15 @@ class DependentRestrictTest < Test::Unit::TestCase
92
134
  assert_not_nil SoftRestrictOne.find_by_name("bambam")
93
135
  end
94
136
 
137
+ def test_destroy_bang_has_soft_restrict_ones_which_is_deleted
138
+ @fred.soft_restrict_one = bambam = SoftRestrictOne.new(:name => "bambam")
139
+ assert_equal @fred.reload.soft_restrict_one, SoftRestrictOne.where(:name => "bambam", :parent_id => @fred.id).first
140
+ bambam.destroy
141
+ assert_equal true, @fred.reload.soft_restrict_one.deleted?
142
+ @fred.destroy!
143
+ assert_nil Parent.find_by_id(@fred.id)
144
+ end
145
+
95
146
  def test_destroy_bang_has_restrict_ones
96
147
  @fred.restrict_one = RestrictOne.new(:name => "bambam")
97
148
  assert_equal @fred.reload.restrict_one, RestrictOne.where(:name => "bambam", :parent_id => @fred.id).first
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: soft_destroyable
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 5
10
- version: 0.1.5
9
+ - 6
10
+ version: 0.1.6
11
11
  platform: ruby
12
12
  authors:
13
13
  - Michael Kintzer