Commit 14ddc9fd by Josh Goebel Committed by Ryan Bigg

paranoia should work well with associations, unscoped is evil

Fixes #32
parent ef8b1f4b
...@@ -91,9 +91,9 @@ end ...@@ -91,9 +91,9 @@ end
You can replace the older acts_as_paranoid methods as follows: You can replace the older acts_as_paranoid methods as follows:
```ruby ```ruby
find_with_deleted(:all) # => unscoped find_with_deleted(:all) # => with_deleted
find_with_deleted(:first) # => unscoped.first find_with_deleted(:first) # => with_deleted.first
find_with_deleted(id) # => unscoped.find(id) find_with_deleted(id) # => with_deleted.find(id)
find_only_deleted(:all) # => only_deleted find_only_deleted(:all) # => only_deleted
find_only_deleted(:first) # => only_deleted.first find_only_deleted(:first) # => only_deleted.first
......
...@@ -7,9 +7,10 @@ module Paranoia ...@@ -7,9 +7,10 @@ module Paranoia
def paranoid? ; true ; end def paranoid? ; true ; end
def only_deleted def only_deleted
unscoped { scoped.tap { |x| x.default_scoped = false }.where("deleted_at is not null")
where("deleted_at is not null") end
} def with_deleted
scoped.tap { |x| x.default_scoped = false }
end end
end end
......
module Paranoia module Paranoia
VERSION = "1.1.0" VERSION = "1.2.0"
end end
...@@ -8,7 +8,8 @@ FileUtils.mkdir_p File.dirname(DB_FILE) ...@@ -8,7 +8,8 @@ FileUtils.mkdir_p File.dirname(DB_FILE)
FileUtils.rm_f DB_FILE FileUtils.rm_f DB_FILE
ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => DB_FILE ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => DB_FILE
ActiveRecord::Base.connection.execute 'CREATE TABLE paranoid_models (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME)' ActiveRecord::Base.connection.execute 'CREATE TABLE parent_models (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME)'
ActiveRecord::Base.connection.execute 'CREATE TABLE paranoid_models (id INTEGER NOT NULL PRIMARY KEY, parent_model_id INTEGER, deleted_at DATETIME)'
ActiveRecord::Base.connection.execute 'CREATE TABLE featureful_models (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME, name VARCHAR(32))' ActiveRecord::Base.connection.execute 'CREATE TABLE featureful_models (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME, name VARCHAR(32))'
ActiveRecord::Base.connection.execute 'CREATE TABLE plain_models (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME)' ActiveRecord::Base.connection.execute 'CREATE TABLE plain_models (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME)'
ActiveRecord::Base.connection.execute 'CREATE TABLE callback_models (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME)' ActiveRecord::Base.connection.execute 'CREATE TABLE callback_models (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME)'
...@@ -69,6 +70,21 @@ class ParanoiaTest < Test::Unit::TestCase ...@@ -69,6 +70,21 @@ class ParanoiaTest < Test::Unit::TestCase
assert_equal 1, model.class.unscoped.count assert_equal 1, model.class.unscoped.count
end end
def test_scoping_behavior_for_paranoid_models
ParanoidModel.unscoped.delete_all
parent1 = ParentModel.create
parent2 = ParentModel.create
p1 = ParanoidModel.create(:parent_model => parent1)
p2 = ParanoidModel.create(:parent_model => parent2)
p1.destroy
p2.destroy
assert_equal 0, parent1.paranoid_models.count
assert_equal 1, parent1.paranoid_models.only_deleted.count
p3 = ParanoidModel.create(:parent_model => parent1)
assert_equal 2, parent1.paranoid_models.with_deleted.count
assert_equal [p1,p3], parent1.paranoid_models.with_deleted
end
def test_destroy_behavior_for_featureful_paranoid_models def test_destroy_behavior_for_featureful_paranoid_models
model = get_featureful_model model = get_featureful_model
...@@ -146,7 +162,12 @@ end ...@@ -146,7 +162,12 @@ end
# Helper classes # Helper classes
class ParentModel < ActiveRecord::Base
has_many :paranoid_models
end
class ParanoidModel < ActiveRecord::Base class ParanoidModel < ActiveRecord::Base
belongs_to :parent_model
acts_as_paranoid acts_as_paranoid
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