Index: test/paranoid_test.rb =================================================================== --- test/paranoid_test.rb (revisiĆ³n: 3169) +++ test/paranoid_test.rb (copia de trabajo) @@ -31,6 +31,7 @@ def test_should_count_with_deleted assert_equal 1, Widget.count assert_equal 2, Widget.count_with_deleted + assert_equal 1, Widget.count_only_deleted assert_equal 2, Widget.calculate_with_deleted(:count, :all) end @@ -50,6 +51,7 @@ widgets(:widget_1).destroy! assert_equal 0, Widget.count assert_equal 0, Category.count + assert_equal 1, Widget.count_only_deleted assert_equal 1, Widget.calculate_with_deleted(:count, :all) # Category doesn't get destroyed because the dependent before_destroy callback uses #destroy assert_equal 4, Category.calculate_with_deleted(:count, :all) @@ -94,8 +96,14 @@ assert_equal 1, Widget.count assert_equal 1, Widget.count(:all, :conditions => ['title=?', 'widget 1']) assert_equal 2, Widget.calculate_with_deleted(:count, :all) + assert_equal 1, Widget.count_only_deleted end + def test_should_find_only_deleted + assert_equal [2], Widget.find_only_deleted(:all).collect { |w| w.id } + assert_equal [1, 2], Widget.find_with_deleted(:all, :order => 'id').collect { |w| w.id } + end + def test_should_not_find_deleted assert_equal [widgets(:widget_1)], Widget.find(:all) assert_equal [1, 2], Widget.find_with_deleted(:all, :order => 'id').collect { |w| w.id } Index: lib/caboose/acts/paranoid.rb =================================================================== --- lib/caboose/acts/paranoid.rb (revisiĆ³n: 3169) +++ lib/caboose/acts/paranoid.rb (copia de trabajo) @@ -87,11 +87,28 @@ end end + def find_only_deleted(*args) + options = extract_options_from_args!(args) + validate_find_options(options) + set_readonly_option!(options) + options[:only_deleted] = true # yuck! + + case args.first + when :first then find_initial(options) + when :all then find_every(options) + else find_from_ids(args, options) + end + end + def count_with_deleted(*args) #calculate_with_deleted(:count, *construct_count_options_from_args(*args)) calculate_with_deleted(:count, *construct_count_options_from_legacy_args(*args)) end + def count_only_deleted(*args) + with_only_deleted_scope { count_with_deleted(*args) } + end + def count(*args) with_deleted_scope { count_with_deleted(*args) } end @@ -113,12 +130,18 @@ with_scope({:find => { :conditions => ["#{table_name}.#{deleted_attribute} IS NULL OR #{table_name}.#{deleted_attribute} > ?", current_time] } }, :merge, &block) end + def with_only_deleted_scope(&block) + with_scope({:find => { :conditions => ["#{table_name}.#{deleted_attribute} IS NOT NULL AND #{table_name}.#{deleted_attribute} <= ?", current_time] } }, :merge, &block) + end + private # all find calls lead here def find_every(options) options.delete(:with_deleted) ? find_every_with_deleted(options) : - with_deleted_scope { find_every_with_deleted(options) } + options.delete(:only_deleted) ? + with_only_deleted_scope { find_every_with_deleted(options) } : + with_deleted_scope { find_every_with_deleted(options) } end end