Commit 26cfd54a by Ryan Bigg

Merge pull request #61 from delba/rails4-upstream

Use update_column and where.not
parents c1d4f029 31b0665f
...@@ -7,7 +7,7 @@ module Paranoia ...@@ -7,7 +7,7 @@ module Paranoia
def paranoid? ; true ; end def paranoid? ; true ; end
def only_deleted def only_deleted
all.tap { |x| x.default_scoped = false }.where("#{self.table_name}.deleted_at is not null") all.tap { |x| x.default_scoped = false }.where.not(deleted_at: nil)
end end
def with_deleted def with_deleted
...@@ -21,24 +21,17 @@ module Paranoia ...@@ -21,24 +21,17 @@ module Paranoia
def delete def delete
return if new_record? or destroyed? return if new_record? or destroyed?
update_attribute_or_column :deleted_at, Time.now update_column :deleted_at, Time.now
end end
def restore! def restore!
update_attribute_or_column :deleted_at, nil update_column :deleted_at, nil
end end
def destroyed? def destroyed?
!self.deleted_at.nil? !self.deleted_at.nil?
end end
alias :deleted? :destroyed? alias :deleted? :destroyed?
private
# Rails 3.1 adds update_column. Rails > 3.2.6 deprecates update_attribute, gone in Rails 4.
def update_attribute_or_column(*args)
respond_to?(:update_column) ? update_column(*args) : update_attribute(*args)
end
end end
class ActiveRecord::Base class ActiveRecord::Base
......
...@@ -14,7 +14,7 @@ Gem::Specification.new do |s| ...@@ -14,7 +14,7 @@ Gem::Specification.new do |s|
s.required_rubygems_version = ">= 1.3.6" s.required_rubygems_version = ">= 1.3.6"
s.rubyforge_project = "paranoia" s.rubyforge_project = "paranoia"
s.add_dependency "activerecord", ">= 3.1.0" s.add_dependency "activerecord", ">= 4.0.0.beta1"
s.add_development_dependency "bundler", ">= 1.0.0" s.add_development_dependency "bundler", ">= 1.0.0"
s.add_development_dependency "sqlite3" s.add_development_dependency "sqlite3"
......
...@@ -103,7 +103,7 @@ class ParanoiaTest < Test::Unit::TestCase ...@@ -103,7 +103,7 @@ class ParanoiaTest < Test::Unit::TestCase
# Regression test for #24 # Regression test for #24
def test_chaining_for_paranoid_models def test_chaining_for_paranoid_models
scope = FeaturefulModel.where(:name => "foo").only_deleted scope = FeaturefulModel.where(:name => "foo").only_deleted
assert_equal "foo", scope.where_values_hash[:name] assert_equal "foo", scope.where_values_hash['name']
assert_equal 2, scope.where_values.count assert_equal 2, scope.where_values.count
end end
...@@ -193,7 +193,7 @@ class ParanoiaTest < Test::Unit::TestCase ...@@ -193,7 +193,7 @@ class ParanoiaTest < Test::Unit::TestCase
model.save model.save
model.destroy! model.destroy!
assert_equal false, ParanoidModel.unscoped.exists?(model.id) refute ParanoidModel.unscoped.exists?(model.id)
end end
def test_real_delete def test_real_delete
...@@ -201,7 +201,7 @@ class ParanoiaTest < Test::Unit::TestCase ...@@ -201,7 +201,7 @@ class ParanoiaTest < Test::Unit::TestCase
model.save model.save
model.delete! model.delete!
assert_equal false, ParanoidModel.unscoped.exists?(model.id) refute ParanoidModel.unscoped.exists?(model.id)
end end
private private
......
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