From e280eaf2e522c1c2888151f7fd053c554c27342d Mon Sep 17 00:00:00 2001 From: Jeffrey Chupp Date: Tue, 12 May 2009 19:12:41 -0500 Subject: [PATCH] returning nil instead of raising ActiveRecord::RecordNotFound when parent_with_destroyed is not found --- lib/is_paranoid.rb | 15 +++++++++------ spec/is_paranoid_spec.rb | 11 +++++++++-- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/lib/is_paranoid.rb b/lib/is_paranoid.rb index 3fe1faa..cad76db 100644 --- a/lib/is_paranoid.rb +++ b/lib/is_paranoid.rb @@ -119,12 +119,15 @@ def method_missing name, *args self.class.send( :include, - Module.new{ # Example: - define_method name do |*args| # def android_with_destroyed - parent_klass.find_with_destroyed( # Android.find_with_destroyed( - self.send(assoc.primary_key_name) # self.send(:android_id) - ) # ) - end # end + Module.new{ # Example: + define_method name do |*args| # def android_with_destroyed + parent_klass.first_with_destroyed( # Android.first_with_destroyed( + :conditions => { # :conditions => { + parent_klass.primary_key => # :id => + self.send(assoc.primary_key_name) # self.send(:android_id) + } # } + ) # ) + end # end } ) self.send(name, *args) diff --git a/spec/is_paranoid_spec.rb b/spec/is_paranoid_spec.rb index 39dc65c..21e606e 100644 --- a/spec/is_paranoid_spec.rb +++ b/spec/is_paranoid_spec.rb @@ -167,8 +167,8 @@ end end - describe 'accessing destroyed parent models' do - it "should be able to access destroyed parents via parent_with_destroyed" do + describe '(parent)_with_destroyed' do + it "should be able to access destroyed parents" do # Memory is has_many with a non-default primary key # Sticker is a has_one with a default primary key [Memory, Sticker].each do |klass| @@ -182,6 +182,13 @@ instance.android_with_destroyed.should == parent end end + + it "should return nil if no destroyed parent exists" do + sticker = Sticker.new(:name => 'Rainbows') + # because the default relationship works this way, i.e. + sticker.android.should == nil + sticker.android_with_destroyed.should == nil + end end describe 'alternate fields and field values' do