Skip to content

Commit

Permalink
Fix nil polymorphic, with_deleted, belongs_to
Browse files Browse the repository at this point in the history
When association is polymorphic and association is not set, finder
should return nil
  • Loading branch information
Péricles Dias committed May 16, 2012
1 parent 7bd6d9f commit 225b551
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/acts_as_paranoid/associations.rb
Expand Up @@ -15,8 +15,10 @@ def belongs_to_with_deleted(target, options = {})
if with_deleted
class_eval <<-RUBY, __FILE__, __LINE__
def #{target}_with_unscoped(*args)
return #{target}_without_unscoped(*args) unless association(:#{target}).klass.paranoid?
association(:#{target}).klass.with_deleted.scoping { #{target}_without_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
Expand Down
13 changes: 13 additions & 0 deletions test/test_associations.rb
Expand Up @@ -58,6 +58,19 @@ def test_belongs_to_polymorphic_with_deleted
assert_equal paranoid_time, paranoid_has_many_dependant.paranoid_time_polymorphic_with_deleted(true)
end

def test_belongs_to_nil_polymorphic_with_deleted
paranoid_time = ParanoidTime.first
paranoid_has_many_dependant = ParanoidHasManyDependant.create!(:name => 'dependant!', :paranoid_time_polymorphic_with_deleted => nil)

assert_nil paranoid_has_many_dependant.paranoid_time
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

def test_only_find_associated_records_when_finding_with_paranoid_deleted
parent = ParanoidBelongsDependant.create
child = ParanoidHasManyDependant.create
Expand Down

0 comments on commit 225b551

Please sign in to comment.