forked from goncalossilva/acts_as_paranoid
/
uniqueness_without_deleted.rb
39 lines (31 loc) · 1.35 KB
/
uniqueness_without_deleted.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
require 'active_support/core_ext/array/wrap'
module ParanoidValidations
class UniquenessWithoutDeletedValidator < ActiveRecord::Validations::UniquenessValidator
def validate_each(record, attribute, value)
finder_class = find_finder_class_for(record)
if value && record.class.serialized_attributes.key?(attribute.to_s)
value = YAML.dump value
end
table = Arel::Table.new(record.class.table_name)
sql, params = build_relation(finder_class, table, attribute, value)
# This is the only changed line from the base class version - it does finder_class.unscoped
relation = finder_class.where(sql, *params)
Array.wrap(options[:scope]).each do |scope_item|
scope_value = record.send(scope_item)
relation = relation.where(scope_item => scope_value)
end
if record.persisted?
# TODO : This should be in Arel
relation = relation.where("#{record.class.quoted_table_name}.#{record.class.primary_key} <> ?", record.send(:id))
end
if relation.exists?
record.errors.add(attribute, :taken, options.except(:case_sensitive, :scope).merge(:value => value))
end
end
end
module ClassMethods
def validates_uniqueness_of_without_deleted(*attr_names)
validates_with UniquenessWithoutDeletedValidator, _merge_attributes(attr_names)
end
end
end