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
def paranoid? ; true ; end
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
def with_deleted
......@@ -21,24 +21,17 @@ module Paranoia
def delete
return if new_record? or destroyed?
update_attribute_or_column :deleted_at, Time.now
update_column :deleted_at, Time.now
end
def restore!
update_attribute_or_column :deleted_at, nil
update_column :deleted_at, nil
end
def destroyed?
!self.deleted_at.nil?
end
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
class ActiveRecord::Base
......
......@@ -13,13 +13,13 @@ Gem::Specification.new do |s|
s.required_rubygems_version = ">= 1.3.6"
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 "sqlite3"
s.add_development_dependency "rake", "0.8.7"
s.files = `git ls-files`.split("\n")
s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
s.require_path = 'lib'
......
......@@ -103,7 +103,7 @@ class ParanoiaTest < Test::Unit::TestCase
# Regression test for #24
def test_chaining_for_paranoid_models
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
end
......@@ -193,7 +193,7 @@ class ParanoiaTest < Test::Unit::TestCase
model.save
model.destroy!
assert_equal false, ParanoidModel.unscoped.exists?(model.id)
refute ParanoidModel.unscoped.exists?(model.id)
end
def test_real_delete
......@@ -201,7 +201,7 @@ class ParanoiaTest < Test::Unit::TestCase
model.save
model.delete!
assert_equal false, ParanoidModel.unscoped.exists?(model.id)
refute ParanoidModel.unscoped.exists?(model.id)
end
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