Skip to content
Merged
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
14 changes: 14 additions & 0 deletions lib/paranoia.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,18 @@ def paranoia_column
end
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)
relation.and(klass.quoted_table_name + ".#{klass.paranoia_column} IS NULL")
end
alias_method_chain :build_relation, :paranoia
end
end
end
1 change: 1 addition & 0 deletions paranoia.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Gem::Specification.new do |s|

s.add_development_dependency 'bundler', '>= 1.0.0'
s.add_development_dependency 'rake'
s.add_development_dependency 'test-unit'

s.files = `git ls-files`.split("\n")
s.executables = `git ls-files`.split("\n").map { |f| f =~ /^bin\/(.*)/ ? $1 : nil }.compact
Expand Down
10 changes: 9 additions & 1 deletion test/paranoia_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
ActiveRecord::Base.connection.execute 'CREATE TABLE callback_models (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME)'
ActiveRecord::Base.connection.execute 'CREATE TABLE fail_callback_models (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME)'
ActiveRecord::Base.connection.execute 'CREATE TABLE related_models (id INTEGER NOT NULL PRIMARY KEY, parent_model_id INTEGER NOT NULL, deleted_at DATETIME)'
ActiveRecord::Base.connection.execute 'CREATE TABLE employers (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME)'
ActiveRecord::Base.connection.execute 'CREATE TABLE employers (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME, name VARCHAR(10))'
ActiveRecord::Base.connection.execute 'CREATE TABLE employees (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME)'
ActiveRecord::Base.connection.execute 'CREATE TABLE jobs (id INTEGER NOT NULL PRIMARY KEY, employer_id INTEGER NOT NULL, employee_id INTEGER NOT NULL, deleted_at DATETIME)'
ActiveRecord::Base.connection.execute 'CREATE TABLE custom_column_models (id INTEGER NOT NULL PRIMARY KEY, destroyed_at DATETIME)'
Expand Down Expand Up @@ -434,6 +434,13 @@ def test_observers_not_notified_if_not_supported
a.restore!
# essentially, we're just ensuring that this doesn't crash
end

def test_validates_uniqueness_only_checks_non_deleted_records
a = Employer.create!(name: "A")
a.destroy
b = Employer.new(name: "A")
assert b.valid?
end

private
def get_featureful_model
Expand Down Expand Up @@ -498,6 +505,7 @@ class RelatedModel < ActiveRecord::Base

class Employer < ActiveRecord::Base
acts_as_paranoid
validates_uniqueness_of :name
has_many :jobs
has_many :employees, :through => :jobs
end
Expand Down