Commit 9e0ec38e by Kurtis Rainbolt-Greene

Merge pull request #305 from rbr/scope-without-deleted

Add scope without_deleted
parents aa04d5d9 aa73c8f3
...@@ -105,7 +105,7 @@ end ...@@ -105,7 +105,7 @@ end
``` ```
If you want to skip adding the default scope If you want to skip adding the default scope:
``` ruby ``` ruby
class Client < ActiveRecord::Base class Client < ActiveRecord::Base
...@@ -139,6 +139,12 @@ If you want to find all records, even those which are deleted: ...@@ -139,6 +139,12 @@ If you want to find all records, even those which are deleted:
Client.with_deleted Client.with_deleted
``` ```
If you want to exclude deleted records, when not able to use the default_scope (e.g. when using without_default_scope):
``` ruby
Client.without_deleted
```
If you want to find only the deleted records: If you want to find only the deleted records:
``` ruby ``` ruby
......
...@@ -219,6 +219,7 @@ class ActiveRecord::Base ...@@ -219,6 +219,7 @@ class ActiveRecord::Base
def self.paranoia_scope def self.paranoia_scope
where(paranoia_column => paranoia_sentinel_value) where(paranoia_column => paranoia_sentinel_value)
end end
class << self; alias_method :without_deleted, :paranoia_scope end
unless options[:without_default_scope] unless options[:without_default_scope]
default_scope { paranoia_scope } default_scope { paranoia_scope }
......
...@@ -186,8 +186,10 @@ class ParanoiaTest < test_framework ...@@ -186,8 +186,10 @@ class ParanoiaTest < test_framework
assert_equal 0, parent1.paranoid_models.count assert_equal 0, parent1.paranoid_models.count
assert_equal 1, parent1.paranoid_models.only_deleted.count assert_equal 1, parent1.paranoid_models.only_deleted.count
assert_equal 1, parent1.paranoid_models.deleted.count assert_equal 1, parent1.paranoid_models.deleted.count
assert_equal 0, parent1.paranoid_models.without_deleted.count
p3 = ParanoidModel.create(:parent_model => parent1) p3 = ParanoidModel.create(:parent_model => parent1)
assert_equal 2, parent1.paranoid_models.with_deleted.count assert_equal 2, parent1.paranoid_models.with_deleted.count
assert_equal 1, parent1.paranoid_models.without_deleted.count
assert_equal [p1,p3], parent1.paranoid_models.with_deleted assert_equal [p1,p3], parent1.paranoid_models.with_deleted
end end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment