From 6d0ed05b14da936978d849b631041e82aac9c648 Mon Sep 17 00:00:00 2001 From: Austin Ziegler Date: Mon, 21 Dec 2015 14:00:19 -0500 Subject: [PATCH] Implement Rails 5.0 compatibility --- .travis.yml | 3 ++- lib/paranoia.rb | 14 ------------- lib/paranoia/version.rb | 2 +- paranoia.gemspec | 2 +- test/paranoia_test.rb | 45 +++++++++++++++++++++++------------------ 5 files changed, 29 insertions(+), 37 deletions(-) diff --git a/.travis.yml b/.travis.yml index 5bcb479e..7d62a6bb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,10 +2,11 @@ language: ruby rvm: - 2.0.0 - 2.1.0 - - 2.2.0 + - 2.2.4 - jruby-19mode env: - RAILS='~> 4.0.13' - RAILS='~> 4.1.10' - RAILS='~> 4.2.1' + - RAILS='~> 5.0.beta' diff --git a/lib/paranoia.rb b/lib/paranoia.rb index 1f3138bc..ab039252 100644 --- a/lib/paranoia.rb +++ b/lib/paranoia.rb @@ -255,17 +255,3 @@ def paranoia_sentinel_value end require 'paranoia/rspec' if defined? RSpec - -module ActiveRecord - module Validations - class UniquenessValidator < ActiveModel::EachValidator - protected - def build_relation_with_paranoia(klass, table, attribute, value) - relation = build_relation_without_paranoia(klass, table, attribute, value) - return relation unless klass.respond_to?(:paranoia_column) - relation.and(klass.arel_table[klass.paranoia_column].eq(klass.paranoia_sentinel_value)) - end - alias_method_chain :build_relation, :paranoia - end - end -end diff --git a/lib/paranoia/version.rb b/lib/paranoia/version.rb index 12e29165..2700c7c7 100644 --- a/lib/paranoia/version.rb +++ b/lib/paranoia/version.rb @@ -1,3 +1,3 @@ module Paranoia - VERSION = "2.1.4" + VERSION = "3.0.0.beta1" end diff --git a/paranoia.gemspec b/paranoia.gemspec index 4698ae3e..f55786ba 100644 --- a/paranoia.gemspec +++ b/paranoia.gemspec @@ -14,7 +14,7 @@ Gem::Specification.new do |s| s.required_rubygems_version = ">= 1.3.6" s.rubyforge_project = "paranoia" - s.add_dependency "activerecord", "~> 4.0" + s.add_dependency "activerecord", ">= 4.0", "< 5.1" s.add_development_dependency "bundler", ">= 1.0.0" s.add_development_dependency "rake" diff --git a/test/paranoia_test.rb b/test/paranoia_test.rb index 6bcac7fe..e7bd8e75 100644 --- a/test/paranoia_test.rb +++ b/test/paranoia_test.rb @@ -4,7 +4,7 @@ require 'paranoia' test_framework = defined?(MiniTest::Test) ? MiniTest::Test : MiniTest::Unit::TestCase -ActiveRecord::Base.raise_in_transactional_callbacks = true if ActiveRecord::VERSION::STRING >= '4.2' +ActiveRecord::Base.raise_in_transactional_callbacks = true if ActiveRecord::VERSION::STRING >= '4.2' && ActiveRecord::VERSION::MAJOR < 5 def connect! ActiveRecord::Base.establish_connection :adapter => 'sqlite3', database: ':memory:' @@ -56,8 +56,15 @@ class WithDifferentConnection < ActiveRecord::Base class ParanoiaTest < test_framework def setup - ActiveRecord::Base.connection.tables.each do |table| - ActiveRecord::Base.connection.execute "DELETE FROM #{table}" + connection = ActiveRecord::Base.connection + cleaner = ->(source) { + ActiveRecord::Base.connection.execute "DELETE FROM #{source}" + } + + if ActiveRecord::VERSION::MAJOR < 5 + connection.tables.each(&cleaner) + else + connection.data_sources.each(&cleaner) end end @@ -223,16 +230,10 @@ def test_active_column_model assert_equal 1, model.class.deleted.count end - def test_active_column_model_with_uniqueness_validation_only_checks_non_deleted_records + def test_active_column_model_with_uniqueness_validation_checks_all_records a = ActiveColumnModelWithUniquenessValidation.create!(name: "A") a.destroy b = ActiveColumnModelWithUniquenessValidation.new(name: "A") - assert b.valid? - end - - def test_active_column_model_with_uniqueness_validation_still_works_on_non_deleted_records - a = ActiveColumnModelWithUniquenessValidation.create!(name: "A") - b = ActiveColumnModelWithUniquenessValidation.new(name: "A") refute b.valid? end @@ -284,7 +285,11 @@ def test_destroy_behavior_for_has_one_with_build_and_validation_error def test_chaining_for_paranoid_models scope = FeaturefulModel.where(:name => "foo").only_deleted assert_equal "foo", scope.where_values_hash['name'] - assert_equal 2, scope.where_values.count + if ActiveRecord::VERSION::MAJOR < 5 + assert_equal 2, scope.where_values.count + else + assert_equal 2, scope.where_clause.ast.children.count + end end def test_only_destroyed_scope_for_paranoid_models @@ -745,16 +750,10 @@ def test_observers_not_notified_if_not_supported # essentially, we're just ensuring that this doesn't crash end - def test_validates_uniqueness_only_checks_non_deleted_records + def test_validates_uniqueness_checks_all_records a = Employer.create!(name: "A") a.destroy b = Employer.new(name: "A") - assert b.valid? - end - - def test_validates_uniqueness_still_works_on_non_deleted_records - a = Employer.create!(name: "A") - b = Employer.new(name: "A") refute b.valid? end @@ -762,7 +761,7 @@ def test_updated_at_modification_on_restore parent1 = ParentModel.create pt1 = ParanoidModelWithTimestamp.create(:parent_model => parent1) ParanoidModelWithTimestamp.record_timestamps = false - pt1.update_columns(created_at: 20.years.ago, updated_at: 10.years.ago, deleted_at: 10.years.ago) + pt1.update_columns(created_at: 20.years.ago, updated_at: 10.years.ago, deleted_at: 10.years.ago) ParanoidModelWithTimestamp.record_timestamps = true assert pt1.updated_at < 10.minutes.ago refute pt1.deleted_at.nil? @@ -948,7 +947,13 @@ class FailCallbackModel < ActiveRecord::Base belongs_to :parent_model acts_as_paranoid - before_destroy { |_| false } + before_destroy { |_| + if ActiveRecord::VERSION::MAJOR < 5 + false + else + throw :abort + end + } end class FeaturefulModel < ActiveRecord::Base