Permalink
Browse files

returning nil instead of raising ActiveRecord::RecordNotFound when pa…

…rent_with_destroyed is not found
  • Loading branch information...
Jeffrey Chupp
Jeffrey Chupp committed May 13, 2009
1 parent 31e871d commit e280eaf2e522c1c2888151f7fd053c554c27342d
Showing with 18 additions and 8 deletions.
  1. +9 −6 lib/is_paranoid.rb
  2. +9 −2 spec/is_paranoid_spec.rb
View
@@ -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)
View
@@ -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

0 comments on commit e280eaf

Please sign in to comment.