Commit 9dac34c8 by Washington Luiz Committed by Ryan Bigg

Added support for Rails 4.1.

Fixes #95
parent dddc727c
......@@ -4,4 +4,8 @@ rvm:
- 2.0.0
- 2.1.0
- jruby-19mode
- rbx
\ No newline at end of file
- rbx
env:
- RAILS='~> 4.0.2'
- RAILS='~> 4.1.0.beta1'
......@@ -9,5 +9,9 @@ platforms :rbx do
gem 'rubinius-developer_tools'
end
rails = ENV['RAILS'] || '~> 4.0.2'
gem 'rails', rails
# Specify your gem's dependencies in paranoia.gemspec
gemspec
......@@ -8,7 +8,11 @@ module Paranoia
def paranoid? ; true ; end
def with_deleted
all.tap { |x| x.default_scoped = false }
if ActiveRecord::VERSION::STRING >= "4.1"
unscope where: paranoia_column
else
all.tap { |x| x.default_scoped = false }
end
end
def only_deleted
......@@ -47,6 +51,16 @@ module Paranoia
run_callbacks(:destroy) { touch_paranoia_column(true) }
end
# As of Rails 4.1.0 +destroy!+ will no longer remove the record from the db
# unless you touch the paranoia column before.
# We need to override it here otherwise children records might be removed
# when they shouldn't
if ActiveRecord::VERSION::STRING >= "4.1"
def destroy!
destroyed? ? super : destroy || raise(ActiveRecord::RecordNotDestroyed)
end
end
def delete
return if new_record?
touch_paranoia_column(false)
......@@ -106,7 +120,7 @@ class ActiveRecord::Base
class_attribute :paranoia_column
self.paranoia_column = options[:column] || :deleted_at
default_scope { where(self.quoted_table_name + ".#{paranoia_column} IS NULL") }
default_scope { where(paranoia_column => nil) }
before_restore {
self.class.notify_observers(:before_restore, self) if self.class.respond_to?(:notify_observers)
......
require 'test/unit'
require 'active_record'
require File.expand_path(File.dirname(__FILE__) + "/../lib/paranoia")
DB_FILE = 'tmp/test_db'
FileUtils.mkdir_p File.dirname(DB_FILE)
FileUtils.rm_f DB_FILE
test_framework = if ActiveRecord::VERSION::STRING >= "4.1"
require 'minitest/autorun'
MiniTest::Test
else
require 'test/unit'
Test::Unit::TestCase
end
require File.expand_path(File.dirname(__FILE__) + "/../lib/paranoia")
ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => DB_FILE
ActiveRecord::Base.establish_connection :adapter => 'sqlite3', database: ':memory:'
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))'
......@@ -20,7 +22,13 @@ ActiveRecord::Base.connection.execute 'CREATE TABLE jobs (id INTEGER NOT NULL PR
ActiveRecord::Base.connection.execute 'CREATE TABLE custom_column_models (id INTEGER NOT NULL PRIMARY KEY, destroyed_at DATETIME)'
ActiveRecord::Base.connection.execute 'CREATE TABLE non_paranoid_models (id INTEGER NOT NULL PRIMARY KEY, parent_model_id INTEGER)'
class ParanoiaTest < Test::Unit::TestCase
class ParanoiaTest < test_framework
def setup
ActiveRecord::Base.connection.tables.each do |table|
ActiveRecord::Base.connection.execute "DELETE FROM #{table}"
end
end
def test_plain_model_class_is_not_paranoid
assert_equal false, PlainModel.paranoid?
end
......@@ -44,7 +52,7 @@ class ParanoiaTest < Test::Unit::TestCase
model.destroy
assert_not_equal nil, model.to_param
assert model.to_param
assert_equal to_param, model.to_param
end
......@@ -106,7 +114,6 @@ class ParanoiaTest < Test::Unit::TestCase
end
def test_scoping_behavior_for_paranoid_models
ParanoidModel.unscoped.delete_all
parent1 = ParentModel.create
parent2 = ParentModel.create
p1 = ParanoidModel.create(:parent_model => parent1)
......@@ -267,11 +274,19 @@ class ParanoiaTest < Test::Unit::TestCase
def test_real_destroy
model = ParanoidModel.new
model.save
model.destroy!
model.really_destroy!
refute ParanoidModel.unscoped.exists?(model.id)
end
if ActiveRecord::VERSION::STRING < "4.1"
def test_real_destroy
model = ParanoidModel.new
model.save
model.destroy!
refute ParanoidModel.unscoped.exists?(model.id)
end
end
def test_real_delete
model = ParanoidModel.new
model.save
......@@ -368,10 +383,6 @@ end
# Helper classes
class ParentModel < ActiveRecord::Base
has_many :paranoid_models
end
class ParanoidModel < ActiveRecord::Base
belongs_to :parent_model
acts_as_paranoid
......@@ -404,6 +415,7 @@ end
class ParentModel < ActiveRecord::Base
acts_as_paranoid
has_many :paranoid_models
has_many :related_models
has_many :very_related_models, :class_name => 'RelatedModel', dependent: :destroy
has_many :non_paranoid_models, dependent: :destroy
......
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