Skip to content

Commit

Permalink
Adds a way to prevent the default_scope when desired
Browse files Browse the repository at this point in the history
  • Loading branch information
hannesfostie committed Jun 3, 2015
1 parent 3a3a099 commit bc515d1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/paranoia.rb
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def really_destroy!
def self.paranoia_scope
where(paranoia_column => paranoia_sentinel_value)
end
default_scope { paranoia_scope }
default_scope { paranoia_scope } unless options[:default_scope] == false

before_restore {
self.class.notify_observers(:before_restore, self) if self.class.respond_to?(:notify_observers)
Expand Down
26 changes: 26 additions & 0 deletions test/paranoia_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ def setup!
'parent_model_with_counter_cache_columns' => 'related_models_count INTEGER DEFAULT 0',
'parent_models' => 'deleted_at DATETIME',
'paranoid_models' => 'parent_model_id INTEGER, deleted_at DATETIME',
'default_scope_true_models' => 'deleted_at DATETIME',
'default_scope_false_models' => 'deleted_at DATETIME',
'paranoid_model_with_belongs' => 'parent_model_id INTEGER, deleted_at DATETIME, paranoid_model_with_has_one_id INTEGER',
'paranoid_model_with_build_belongs' => 'parent_model_id INTEGER, deleted_at DATETIME, paranoid_model_with_has_one_and_build_id INTEGER, name VARCHAR(32)',
'paranoid_model_with_anthor_class_name_belongs' => 'parent_model_id INTEGER, deleted_at DATETIME, paranoid_model_with_has_one_id INTEGER',
Expand Down Expand Up @@ -244,6 +246,22 @@ def test_only_destroyed_scope_for_paranoid_models
assert_equal false, ParanoidModel.only_deleted.include?(model2)
end

def test_omittance_of_default_scope
model = DefaultScopeFalseModel.new
model.save
model.destroy

assert_equal true, DefaultScopeFalseModel.all.include?(model)
end

def test_default_scope
model = DefaultScopeTrueModel.new
model.save
model.destroy

assert_equal false, DefaultScopeTrueModel.all.include?(model)
end

def test_default_scope_for_has_many_relationships
parent = ParentModel.create
assert_equal 0, parent.related_models.count
Expand Down Expand Up @@ -918,6 +936,14 @@ class CustomSentinelModel < ActiveRecord::Base
acts_as_paranoid sentinel_value: DateTime.new(0)
end

class DefaultScopeTrueModel < ActiveRecord::Base
acts_as_paranoid default_scope: true
end

class DefaultScopeFalseModel < ActiveRecord::Base
acts_as_paranoid default_scope: false
end

class NonParanoidModel < ActiveRecord::Base
end

Expand Down

0 comments on commit bc515d1

Please sign in to comment.