Skip to content

Commit

Permalink
returning nil instead of raising ActiveRecord::RecordNotFound when pa…
Browse files Browse the repository at this point in the history
…rent_with_destroyed is not found
  • Loading branch information
Jeffrey Chupp committed May 13, 2009
1 parent 31e871d commit e280eaf
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
15 changes: 9 additions & 6 deletions lib/is_paranoid.rb
Expand Up @@ -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)
Expand Down
11 changes: 9 additions & 2 deletions spec/is_paranoid_spec.rb
Expand Up @@ -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|
Expand All @@ -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
Expand Down

0 comments on commit e280eaf

Please sign in to comment.