Permalink
Browse files

Added support for Rails 4.1.

Fixes #95
  • Loading branch information...
1 parent dddc727 commit 9dac34c85e9d01b7119148d28418648f1c355867 @huoxito huoxito committed with radar Dec 27, 2013
Showing with 53 additions and 19 deletions.
  1. +5 −1 .travis.yml
  2. +4 −0 Gemfile
  3. +16 −2 lib/paranoia.rb
  4. +28 −16 test/paranoia_test.rb
View
6 .travis.yml
@@ -4,4 +4,8 @@ rvm:
- 2.0.0
- 2.1.0
- jruby-19mode
- - rbx
+ - rbx
+
+env:
+ - RAILS='~> 4.0.2'
+ - RAILS='~> 4.1.0.beta1'
View
4 Gemfile
@@ -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
View
18 lib/paranoia.rb
@@ -8,7 +8,11 @@ module Query
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 @@ def destroy
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 @@ def self.acts_as_paranoid(options={})
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)
View
44 test/paranoia_test.rb
@@ -1,13 +1,15 @@
-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 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 @@ def test_paranoid_models_to_param
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 @@ def test_destroy_behavior_for_paranoid_models
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 @@ def test_restore_behavior_for_callbacks
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 @@ def get_featureful_model
# 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 @@ def remove_called_variables
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

0 comments on commit 9dac34c

Please sign in to comment.