Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed bug with double `belongs_to :assoc, :with_deleted => true`

  • Loading branch information...
commit f0382a880d55991d494a7a536745f775a7576c2c 1 parent 4746980
Anton Dieterle adie authored
20 lib/acts_as_paranoid/associations.rb
View
@@ -14,15 +14,17 @@ def belongs_to_with_deleted(target, options = {})
if with_deleted
result.options[:with_deleted] = with_deleted
- class_eval <<-RUBY, __FILE__, __LINE__
- def #{target}_with_unscoped(*args)
- association = association(:#{target})
- return nil if association.options[:polymorphic] && association.klass.nil?
- return #{target}_without_unscoped(*args) unless association.klass.paranoid?
- association.klass.with_deleted.scoping { #{target}_without_unscoped(*args) }
- end
- alias_method_chain :#{target}, :unscoped
- RUBY
+ unless method_defined? "#{target}_with_unscoped"
+ class_eval <<-RUBY, __FILE__, __LINE__
+ def #{target}_with_unscoped(*args)
+ association = association(:#{target})
+ return nil if association.options[:polymorphic] && association.klass.nil?
+ return #{target}_without_unscoped(*args) unless association.klass.paranoid?
+ association.klass.with_deleted.scoping { #{target}_without_unscoped(*args) }
+ end
+ alias_method_chain :#{target}, :unscoped
+ RUBY
+ end
end
result
74 test/test_associations.rb
View
@@ -8,7 +8,7 @@ def test_removal_with_associations
paranoid_company_2 = ParanoidDeleteCompany.create! :name => "ParanoidDestroyCompany #1"
paranoid_company_1.paranoid_products.create! :name => "ParanoidProduct #1"
paranoid_company_2.paranoid_products.create! :name => "ParanoidProduct #2"
-
+
assert_equal 1, ParanoidDestroyCompany.count
assert_equal 1, ParanoidDeleteCompany.count
assert_equal 2, ParanoidProduct.count
@@ -18,13 +18,13 @@ def test_removal_with_associations
assert_equal 1, ParanoidProduct.count
assert_equal 1, ParanoidDestroyCompany.with_deleted.count
assert_equal 2, ParanoidProduct.with_deleted.count
-
+
ParanoidDestroyCompany.with_deleted.first.destroy!
assert_equal 0, ParanoidDestroyCompany.count
assert_equal 1, ParanoidProduct.count
assert_equal 0, ParanoidDestroyCompany.with_deleted.count
assert_equal 1, ParanoidProduct.with_deleted.count
-
+
ParanoidDeleteCompany.with_deleted.first.destroy!
assert_equal 0, ParanoidDeleteCompany.count
assert_equal 0, ParanoidProduct.count
@@ -40,7 +40,7 @@ def test_belongs_to_with_deleted
assert_equal paranoid_time, paranoid_has_many_dependant.paranoid_time_with_deleted
paranoid_time.destroy
-
+
assert_nil paranoid_has_many_dependant.paranoid_time(true)
assert_equal paranoid_time, paranoid_has_many_dependant.paranoid_time_with_deleted(true)
end
@@ -53,7 +53,7 @@ def test_belongs_to_polymorphic_with_deleted
assert_equal paranoid_time, paranoid_has_many_dependant.paranoid_time_polymorphic_with_deleted
paranoid_time.destroy
-
+
assert_nil paranoid_has_many_dependant.paranoid_time(true)
assert_equal paranoid_time, paranoid_has_many_dependant.paranoid_time_polymorphic_with_deleted(true)
end
@@ -66,7 +66,7 @@ def test_belongs_to_nil_polymorphic_with_deleted
assert_nil paranoid_has_many_dependant.paranoid_time_polymorphic_with_deleted
paranoid_time.destroy
-
+
assert_nil paranoid_has_many_dependant.paranoid_time(true)
assert_nil paranoid_has_many_dependant.paranoid_time_polymorphic_with_deleted(true)
end
@@ -93,14 +93,14 @@ def test_only_find_associated_records_when_finding_with_paranoid_deleted
parent = ParanoidBelongsDependant.create
child = ParanoidHasManyDependant.create
parent.paranoid_has_many_dependants << child
-
+
unrelated_parent = ParanoidBelongsDependant.create
unrelated_child = ParanoidHasManyDependant.create
unrelated_parent.paranoid_has_many_dependants << unrelated_child
-
+
child.destroy
assert_paranoid_deletion(child)
-
+
assert_equal [child], parent.paranoid_has_many_dependants.with_deleted.to_a
end
@@ -108,96 +108,92 @@ def test_cannot_find_a_paranoid_deleted_many_many_association
left = ParanoidManyManyParentLeft.create
right = ParanoidManyManyParentRight.create
left.paranoid_many_many_parent_rights << right
-
- child = left.paranoid_many_many_children.first
left.paranoid_many_many_parent_rights.delete(right)
left.reload
-
+
assert_equal [], left.paranoid_many_many_children, "Linking objects not deleted"
assert_equal [], left.paranoid_many_many_parent_rights, "Associated objects not unlinked"
assert_equal right, ParanoidManyManyParentRight.find(right.id), "Associated object deleted"
end
-
+
def test_cannot_find_a_paranoid_destroyed_many_many_association
left = ParanoidManyManyParentLeft.create
right = ParanoidManyManyParentRight.create
left.paranoid_many_many_parent_rights << right
-
- child = left.paranoid_many_many_children.first
left.paranoid_many_many_parent_rights.destroy(right)
-
+
left.reload
-
+
assert_equal [], left.paranoid_many_many_children, "Linking objects not deleted"
assert_equal [], left.paranoid_many_many_parent_rights, "Associated objects not unlinked"
assert_equal right, ParanoidManyManyParentRight.find(right.id), "Associated object deleted"
end
-
+
def test_cannot_find_a_has_many_through_object_when_its_linking_object_is_paranoid_destroyed
left = ParanoidManyManyParentLeft.create
right = ParanoidManyManyParentRight.create
left.paranoid_many_many_parent_rights << right
-
+
child = left.paranoid_many_many_children.first
child.destroy
-
+
left.reload
-
+
assert_equal [], left.paranoid_many_many_parent_rights, "Associated objects not deleted"
end
-
+
def test_cannot_find_a_paranoid_deleted_model
model = ParanoidBelongsDependant.create
model.destroy
-
+
assert_raises ActiveRecord::RecordNotFound do
ParanoidBelongsDependant.find(model.id)
end
end
-
+
def test_bidirectional_has_many_through_association_clear_is_paranoid
left = ParanoidManyManyParentLeft.create
right = ParanoidManyManyParentRight.create
left.paranoid_many_many_parent_rights << right
-
+
child = left.paranoid_many_many_children.first
assert_equal left, child.paranoid_many_many_parent_left, "Child's left parent is incorrect"
assert_equal right, child.paranoid_many_many_parent_right, "Child's right parent is incorrect"
-
+
left.paranoid_many_many_parent_rights.clear
-
+
assert_paranoid_deletion(child)
end
-
+
def test_bidirectional_has_many_through_association_destroy_is_paranoid
left = ParanoidManyManyParentLeft.create
right = ParanoidManyManyParentRight.create
left.paranoid_many_many_parent_rights << right
-
+
child = left.paranoid_many_many_children.first
assert_equal left, child.paranoid_many_many_parent_left, "Child's left parent is incorrect"
assert_equal right, child.paranoid_many_many_parent_right, "Child's right parent is incorrect"
-
+
left.paranoid_many_many_parent_rights.destroy(right)
-
+
assert_paranoid_deletion(child)
end
-
+
def test_bidirectional_has_many_through_association_delete_is_paranoid
left = ParanoidManyManyParentLeft.create
right = ParanoidManyManyParentRight.create
left.paranoid_many_many_parent_rights << right
-
+
child = left.paranoid_many_many_children.first
assert_equal left, child.paranoid_many_many_parent_left, "Child's left parent is incorrect"
assert_equal right, child.paranoid_many_many_parent_right, "Child's right parent is incorrect"
-
+
left.paranoid_many_many_parent_rights.delete(right)
-
+
assert_paranoid_deletion(child)
end
@@ -208,4 +204,12 @@ def test_belongs_to_on_normal_model_is_paranoid
assert not_paranoid.save
assert_not_nil not_paranoid.paranoid_time
end
+
+ def test_double_belongs_to_with_deleted
+ not_paranoid = DoubleHasOneNotParanoid.create
+ not_paranoid.paranoid_time = ParanoidTime.create
+
+ assert not_paranoid.save
+ assert_not_nil not_paranoid.paranoid_time
+ end
end
5 test/test_helper.rb
View
@@ -209,6 +209,11 @@ class HasOneNotParanoid < ActiveRecord::Base
belongs_to :paranoid_time, :with_deleted => true
end
+class DoubleHasOneNotParanoid < HasOneNotParanoid
+ belongs_to :paranoid_time, :with_deleted => true
+ belongs_to :paranoid_time, :with_deleted => true
+end
+
class ParanoidHasManyDependant < ActiveRecord::Base
acts_as_paranoid
belongs_to :paranoid_time
Please sign in to comment.
Something went wrong with that request. Please try again.