Permalink
Browse files

Merge branch 'master' into brentd/master

  • Loading branch information...
2 parents 4beab16 + 3a5d0c7 commit 6fe738d55f9e8ad0c1265082968b3951e20f5c20 Jeffrey Chupp committed Jun 13, 2009
Showing with 26 additions and 1 deletion.
  1. +14 −0 lib/is_paranoid.rb
  2. +12 −1 spec/is_paranoid_spec.rb
View
14 lib/is_paranoid.rb
@@ -123,6 +123,20 @@ def method_missing name, *args, &block
end
end
+ # with_exclusive_scope is used internally by ActiveRecord when preloading
+ # associations. Unfortunately this is problematic for is_paranoid since we
+ # want preloaded is_paranoid items to still be scoped to their deleted conditions.
+ # so we override that here.
+ def with_exclusive_scope(method_scoping = {}, &block)
+ # this is rather hacky, suggestions for improvements appreciated... the idea
+ # is that when the caller includes the method preload_associations, we want
+ # to apply our is_paranoid conditions
+ if caller.any?{|c| c =~ /\d+:in `preload_associations'$/}
+ method_scoping.deep_merge!(:find => {:conditions => {destroyed_field => field_not_destroyed} })
+ end
+ super method_scoping, &block
+ end
+
protected
def should_restore?(association_name, dependent_relationship, options) #:nodoc:
View
13 spec/is_paranoid_spec.rb
@@ -1,13 +1,15 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
require File.expand_path(File.dirname(__FILE__) + '/models')
+LUKE = 'Luke Skywalker'
+
describe IsParanoid do
before(:each) do
Android.delete_all
Person.delete_all
Component.delete_all
- @luke = Person.create(:name => 'Luke Skywalker')
+ @luke = Person.create(:name => LUKE)
@r2d2 = Android.create(:name => 'R2D2', :owner_id => @luke.id)
@c3p0 = Android.create(:name => 'C3P0', :owner_id => @luke.id)
@@ -82,6 +84,15 @@
Android.all_destroyed_only.size.should == 1
Android.first_destroyed_only.should == @r2d2
end
+
+ it "should not show destroyed models via :include" do
+ Person.first(:conditions => {:name => LUKE}, :include => :androids).androids.size.should == 2
+ @r2d2.destroy
+ person = Person.first(:conditions => {:name => LUKE}, :include => :androids)
+ # ensure that we're using the preload and not loading it via a find
+ Android.should_not_receive(:find)
+ person.androids.size.should == 1
+ end
end
describe 'calculations' do

0 comments on commit 6fe738d

Please sign in to comment.