Skip to content
This repository has been archived by the owner on Jun 30, 2021. It is now read-only.

Add method getting non deleted record after unscoped #92

Open
wants to merge 1 commit into
base: rails3.2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/acts_as_paranoid/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ def only_deleted
end
end

def active_on_unscoped
self.unscoped.where(paranoid_default_scope_sql)
end

def delete_all!(conditions = nil)
without_paranoid_default_scope.delete_all!(conditions)
end
Expand Down
38 changes: 38 additions & 0 deletions test/test_core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def test_paranoid?
assert_raise(NoMethodError) { NotParanoid.first.destroy! }
assert_raise(NoMethodError) { NotParanoid.with_deleted }
assert_raise(NoMethodError) { NotParanoid.only_deleted }
assert_raise(NoMethodError) { NotParanoid.active_on_unscoped }

assert ParanoidTime.paranoid?
end
Expand Down Expand Up @@ -38,6 +39,9 @@ def test_fake_removal
assert_equal 3, ParanoidTime.with_deleted.count
assert_equal 3, ParanoidBoolean.with_deleted.count
assert_equal 1, ParanoidString.with_deleted.count
assert_equal 2, ParanoidTime.active_on_unscoped.count
assert_equal 1, ParanoidBoolean.active_on_unscoped.count
assert_equal 0, ParanoidString.active_on_unscoped.count
end

def test_real_removal
Expand All @@ -53,6 +57,9 @@ def test_real_removal
assert_equal 0, ParanoidTime.only_deleted.count
assert_equal 0, ParanoidBoolean.only_deleted.count
assert_equal 0, ParanoidString.only_deleted.count
assert_equal 2, ParanoidTime.active_on_unscoped.count
assert_equal 1, ParanoidBoolean.active_on_unscoped.count
assert_equal 0, ParanoidString.active_on_unscoped.count

ParanoidTime.first.destroy
ParanoidTime.only_deleted.first.destroy
Expand Down Expand Up @@ -333,4 +340,35 @@ def test_string_type_with_no_nil_value_after_destroyed_twice
2.times { ps.destroy }
assert_equal 0, ParanoidString.with_deleted.where(:id => ps).count
end

def test_active_on_unscoped
assert_equal 3, ParanoidTime.count
assert_equal 3, ParanoidBoolean.count
assert_equal 1, ParanoidString.count

ParanoidTime.send(:scope, :excluded, ->{ ParanoidTime.where('name != ?','paranoid') })
ParanoidBoolean.send(:scope, :excluded, ->{ ParanoidBoolean.where('name != ?','paranoid') })
ParanoidString.send(:scope, :excluded, ->{ ParanoidString.where('name != ?','strings can be paranoid') })

assert_equal 2, ParanoidTime.excluded.count
assert_equal 2, ParanoidBoolean.excluded.count
assert_equal 0, ParanoidString.excluded.count

ParanoidTime.where(:name => 'really paranoid').first.destroy
ParanoidBoolean.where(:name => 'paranoid').first.destroy
ParanoidString.where(:name => 'strings can be paranoid').first.destroy

assert_equal 1, ParanoidTime.excluded.count
assert_equal 3, ParanoidTime.excluded.unscoped.count
assert_equal 2, ParanoidTime.excluded.active_on_unscoped.count

assert_equal 2, ParanoidBoolean.excluded.count
assert_equal 3, ParanoidBoolean.excluded.unscoped.count
assert_equal 2, ParanoidBoolean.excluded.active_on_unscoped.count

assert_equal 0, ParanoidString.excluded.count
assert_equal 1, ParanoidString.excluded.unscoped.count
assert_equal 0, ParanoidString.excluded.active_on_unscoped.count
end

end