Skip to content

Commit

Permalink
add support for Calculations [Adam Stubblefield]
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.techno-weenie.net/projects/plugins/acts_as_paranoid@1049 567b1171-46fb-0310-a4c9-b4bef9110e78
  • Loading branch information
technoweenie committed Apr 10, 2006
1 parent 7d966ee commit 0179bd8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
5 changes: 5 additions & 0 deletions lib/caboose/acts/paranoid.rb
Expand Up @@ -46,6 +46,7 @@ def acts_as_paranoid
class << self
alias_method :find_with_deleted, :find
alias_method :count_with_deleted, :count
alias_method :calculate_with_deleted, :calculate
end
end
include InstanceMethods
Expand All @@ -70,6 +71,10 @@ def count(*args)
with_deleted_scope { count_with_deleted(*args) }
end

def calculate(*args)
with_deleted_scope { calculate_with_deleted(*args) }
end

protected
def with_deleted_scope(&block)
with_scope({:find => { :conditions => "#{table_name}.deleted_at IS NULL" } }, :merge, &block)
Expand Down
18 changes: 9 additions & 9 deletions test/paranoid_test.rb
Expand Up @@ -33,8 +33,8 @@ def test_should_set_deleted_at
widgets(:widget_1).destroy
assert_equal 0, Widget.count
assert_equal 0, Category.count
assert_equal 2, Widget.count_with_deleted
assert_equal 4, Category.count_with_deleted
assert_equal 2, Widget.calculate_with_deleted(:count, :all)
assert_equal 4, Category.calculate_with_deleted(:count, :all)
end

def test_should_destroy
Expand All @@ -43,15 +43,15 @@ def test_should_destroy
widgets(:widget_1).destroy!
assert_equal 0, Widget.count
assert_equal 0, Category.count
assert_equal 1, Widget.count_with_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.count_with_deleted
assert_equal 4, Category.calculate_with_deleted(:count, :all)
end

def test_should_not_count_deleted
assert_equal 1, Widget.count
assert_equal 1, Widget.count(['title=?', 'widget 1'])
assert_equal 2, Widget.count_with_deleted
assert_equal 2, Widget.calculate_with_deleted(:count, :all)
end

def test_should_not_find_deleted
Expand All @@ -60,7 +60,7 @@ def test_should_not_find_deleted
end

def test_should_not_find_deleted_associations
assert_equal 2, Category.count_with_deleted('widget_id=1')
assert_equal 2, Category.calculate_with_deleted(:count, :all, :conditions => 'widget_id=1')

assert_equal 1, widgets(:widget_1).categories.size
assert_equal [categories(:category_1)], widgets(:widget_1).categories
Expand Down Expand Up @@ -94,7 +94,7 @@ def test_should_ignore_multiple_includes
def test_should_not_override_scopes_when_counting
assert_equal 1, Widget.with_scope(:find => { :conditions => "title = 'widget 1'" }) { Widget.count }
assert_equal 0, Widget.with_scope(:find => { :conditions => "title = 'deleted widget 2'" }) { Widget.count }
assert_equal 1, Widget.with_scope(:find => { :conditions => "title = 'deleted widget 2'" }) { Widget.count_with_deleted }
assert_equal 1, Widget.with_scope(:find => { :conditions => "title = 'deleted widget 2'" }) { Widget.calculate_with_deleted(:count, :all) }
end

def test_should_not_override_scopes_when_finding
Expand All @@ -114,8 +114,8 @@ def test_should_allow_multiple_scoped_calls_when_finding

def test_should_allow_multiple_scoped_calls_when_counting
Widget.with_scope(:find => { :conditions => "title = 'deleted widget 2'" }) do
assert_equal 1, Widget.count_with_deleted
assert_equal 1, Widget.count_with_deleted, "clobbers the constrain on the unmodified find"
assert_equal 1, Widget.calculate_with_deleted(:count, :all)
assert_equal 1, Widget.calculate_with_deleted(:count, :all), "clobbers the constrain on the unmodified find"
assert_equal 0, Widget.count
assert_equal 0, Widget.count, 'clobbers the constrain on a paranoid find'
end
Expand Down

0 comments on commit 0179bd8

Please sign in to comment.