forked from ActsAsParanoid/acts_as_paranoid
/
validations.rb
91 lines (75 loc) · 3.27 KB
/
validations.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
require 'active_support/core_ext/array/wrap'
module ActsAsParanoid
module Validations
def self.included(base)
base.extend ClassMethods
end
class UniquenessWithoutDeletedValidator
def self.[](version)
name = "V#{version.to_s.tr('.', '_')}"
unless constants.include? name.to_sym
raise "Unknown validator version #{name.inspect}; expected one of #{constants.sort.join(', ')}"
end
const_get name
end
class V5 < ActiveRecord::Validations::UniquenessValidator
def validate_each(record, attribute, value)
finder_class = find_finder_class_for(record)
table = finder_class.arel_table
relation = build_relation(finder_class, attribute, value)
[Array(finder_class.primary_key), Array(record.send(:id))].transpose.each do |pk_key, pk_value|
relation = relation.where(table[pk_key.to_sym].not_eq(pk_value))
end if record.persisted?
Array.wrap(options[:scope]).each do |scope_item|
relation = relation.where(table[scope_item].eq(record.public_send(scope_item)))
end
if relation.where(finder_class.paranoid_default_scope).exists?(relation)
record.errors.add(attribute, :taken, options.except(:case_sensitive, :scope).merge(:value => value))
end
end
protected
def build_relation(klass, attribute, value)
if ActiveRecord::VERSION::MINOR == 0 && ActiveRecord::VERSION::MAJOR == 5
return super(klass, klass.arel_table, attribute, value)
else
super
end
end
end
class V4 < ActiveRecord::Validations::UniquenessValidator
def validate_each(record, attribute, value)
finder_class = find_finder_class_for(record)
table = finder_class.arel_table
# TODO: Use record.class.column_types[attribute.to_s].coder ?
coder = record.class.column_types[attribute.to_s]
if value && coder
value = if coder.respond_to? :type_cast_for_database
coder.type_cast_for_database value
else
coder.type_cast_for_write value
end
end
relation = build_relation(finder_class, table, attribute, value)
[Array(finder_class.primary_key), Array(record.send(:id))].transpose.each do |pk_key, pk_value|
relation = relation.and(table[pk_key.to_sym].not_eq(pk_value))
end if record.persisted?
Array.wrap(options[:scope]).each do |scope_item|
scope_value = record.send(scope_item)
relation = relation.and(table[scope_item].eq(scope_value))
end
# Re-add ActsAsParanoid default scope conditions manually.
if finder_class.unscoped.where(finder_class.paranoid_default_scope).where(relation).exists?
record.errors.add(attribute, :taken, options.except(:case_sensitive, :scope).merge(:value => value))
end
end
end
class V6 < V5
end
end
module ClassMethods
def validates_uniqueness_of_without_deleted(*attr_names)
validates_with UniquenessWithoutDeletedValidator[ActiveRecord::VERSION::MAJOR], _merge_attributes(attr_names)
end
end
end
end