mirror of
https://github.com/discourse/discourse.git
synced 2024-12-05 10:26:17 +08:00
45ccadeeeb
Rails 6.1.3.1 deprecates a few API and has some internal changes that break our tests suite, so this commit fixes all the deprecations and errors and now Discourse should be fully compatible with Rails 6.1.3.1. We also have a new release of the rails_failover gem that's compatible with Rails 6.1.3.1.
39 lines
1.1 KiB
Ruby
39 lines
1.1 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
class UniqueAmongValidator < ActiveRecord::Validations::UniquenessValidator
|
|
def validate_each(record, attribute, value)
|
|
old_errors = []
|
|
record.errors.each do |error|
|
|
old_errors << error if error.attribute == attribute
|
|
end
|
|
|
|
# look for any duplicates at all
|
|
super
|
|
|
|
new_errors = []
|
|
record.errors.each do |error|
|
|
new_errors << error if error.attribute == attribute
|
|
end
|
|
|
|
# do nothing further unless there were some duplicates.
|
|
if new_errors.size - old_errors.size != 0
|
|
# now look only in the collection we care about.
|
|
dupes = options[:collection].call(record).where("lower(#{attribute}) = ?", value.downcase)
|
|
dupes = dupes.where("id != ?", record.id) if record.persisted?
|
|
|
|
# pop off the error, if it was a false positive
|
|
if !dupes.exists?
|
|
record.errors.delete(attribute)
|
|
old_errors.each do |error|
|
|
record.errors.add(
|
|
error.attribute,
|
|
error.type,
|
|
**error.options
|
|
)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
end
|