Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Rails 5.0 compatibility #290

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might have to update this to 5.0.0.beta1.

14 changes: 0 additions & 14 deletions lib/paranoia.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/paranoia/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Paranoia
VERSION = "2.1.4"
VERSION = "3.0.0.beta1"
end
2 changes: 1 addition & 1 deletion paranoia.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
45 changes: 25 additions & 20 deletions test/paranoia_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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:'
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -745,24 +750,18 @@ 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

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?
Expand Down Expand Up @@ -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
Expand Down