Skip to content

Commit

Permalink
Fix nil polymorphic belongs_to
Browse files Browse the repository at this point in the history
  • Loading branch information
Péricles Dias committed May 16, 2012
1 parent 77f0d9b commit 49a3df7
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 @@ -16,8 +16,10 @@ def belongs_to_with_deleted(target, options = {})
result.options[:with_deleted] = 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_belongs_to_options
paranoid_time = ParanoidHasManyDependant.reflections[:paranoid_time]
assert_equal :belongs_to, paranoid_time.macro
Expand Down

0 comments on commit 49a3df7

Please sign in to comment.