Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added without_default_scope option #301

Merged
merged 4 commits into from Feb 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Expand Up @@ -104,6 +104,17 @@ class Client < ActiveRecord::Base
end
```


If you want to skip adding the default scope

``` ruby
class Client < ActiveRecord::Base
acts_as_paranoid without_default_scope: true

...
end
```

If you want to access soft-deleted associations, override the getter method:

``` ruby
Expand Down
5 changes: 4 additions & 1 deletion lib/paranoia.rb
Expand Up @@ -219,7 +219,10 @@ def self.acts_as_paranoid(options={})
def self.paranoia_scope
where(paranoia_column => paranoia_sentinel_value)
end
default_scope { paranoia_scope }

unless options[:without_default_scope]
default_scope { paranoia_scope }
end

before_restore {
self.class.notify_observers(:before_restore, self) if self.class.respond_to?(:notify_observers)
Expand Down
15 changes: 14 additions & 1 deletion test/paranoia_test.rb
Expand Up @@ -40,7 +40,8 @@ def setup!
'namespaced_paranoid_belongs_tos' => 'deleted_at DATETIME, paranoid_has_one_id INTEGER',
'unparanoid_unique_models' => 'name VARCHAR(32), paranoid_with_unparanoids_id INTEGER',
'active_column_models' => 'deleted_at DATETIME, active BOOLEAN',
'active_column_model_with_uniqueness_validations' => 'name VARCHAR(32), deleted_at DATETIME, active BOOLEAN'
'active_column_model_with_uniqueness_validations' => 'name VARCHAR(32), deleted_at DATETIME, active BOOLEAN',
'without_default_scope_models' => 'deleted_at DATETIME'
}.each do |table_name, columns_as_sql_string|
ActiveRecord::Base.connection.execute "CREATE TABLE #{table_name} (id INTEGER NOT NULL PRIMARY KEY, #{columns_as_sql_string})"
end
Expand Down Expand Up @@ -204,6 +205,14 @@ def test_default_sentinel_value
assert_equal nil, ParanoidModel.paranoia_sentinel_value
end

def test_without_default_scope_option
model = WithoutDefaultScopeModel.create
model.destroy
assert_equal 1, model.class.count
assert_equal 1, model.class.only_deleted.count
assert_equal 0, model.class.where(deleted_at: nil).count
end

def test_active_column_model
model = ActiveColumnModel.new
assert_equal 0, model.class.count
Expand Down Expand Up @@ -1042,6 +1051,10 @@ class CustomSentinelModel < ActiveRecord::Base
acts_as_paranoid sentinel_value: DateTime.new(0)
end

class WithoutDefaultScopeModel < ActiveRecord::Base
acts_as_paranoid without_default_scope: true
end

class ActiveColumnModel < ActiveRecord::Base
acts_as_paranoid column: :active, sentinel_value: true

Expand Down