models: fix deprecated errors[:base] << "message" calls.

Replace the idiom `errors[:base] << "message"` with
`errors.add(:base, "message")`. The former is deprecated in Rails 6.1.
This commit is contained in:
evazion
2020-12-13 04:00:32 -06:00
parent 62b69eb133
commit 8d87b1a0c0
29 changed files with 108 additions and 109 deletions

View File

@@ -55,20 +55,20 @@ class BulkUpdateRequestProcessor
tag_alias = TagAlias.new(creator: User.system, antecedent_name: args[0], consequent_name: args[1])
tag_alias.save(context: validation_context)
if tag_alias.errors.present?
errors[:base] << "Can't create alias #{tag_alias.antecedent_name} -> #{tag_alias.consequent_name} (#{tag_alias.errors.full_messages.join("; ")})"
errors.add(:base, "Can't create alias #{tag_alias.antecedent_name} -> #{tag_alias.consequent_name} (#{tag_alias.errors.full_messages.join("; ")})")
end
when :create_implication
tag_implication = TagImplication.new(creator: User.system, antecedent_name: args[0], consequent_name: args[1], status: "active")
tag_implication.save(context: validation_context)
if tag_implication.errors.present?
errors[:base] << "Can't create implication #{tag_implication.antecedent_name} -> #{tag_implication.consequent_name} (#{tag_implication.errors.full_messages.join("; ")})"
errors.add(:base, "Can't create implication #{tag_implication.antecedent_name} -> #{tag_implication.consequent_name} (#{tag_implication.errors.full_messages.join("; ")})")
end
when :remove_alias
tag_alias = TagAlias.active.find_by(antecedent_name: args[0], consequent_name: args[1])
if tag_alias.nil?
errors[:base] << "Can't remove alias #{args[0]} -> #{args[1]} (alias doesn't exist)"
errors.add(:base, "Can't remove alias #{args[0]} -> #{args[1]} (alias doesn't exist)")
else
tag_alias.update(status: "deleted")
end
@@ -76,7 +76,7 @@ class BulkUpdateRequestProcessor
when :remove_implication
tag_implication = TagImplication.active.find_by(antecedent_name: args[0], consequent_name: args[1])
if tag_implication.nil?
errors[:base] << "Can't remove implication #{args[0]} -> #{args[1]} (implication doesn't exist)"
errors.add(:base, "Can't remove implication #{args[0]} -> #{args[1]} (implication doesn't exist)")
else
tag_implication.update(status: "deleted")
end
@@ -84,22 +84,22 @@ class BulkUpdateRequestProcessor
when :change_category
tag = Tag.find_by_name(args[0])
if tag.nil?
errors[:base] << "Can't change category #{args[0]} -> #{args[1]} (the '#{args[0]}' tag doesn't exist)"
errors.add(:base, "Can't change category #{args[0]} -> #{args[1]} (the '#{args[0]}' tag doesn't exist)")
end
when :rename
tag = Tag.find_by_name(args[0])
if tag.nil?
errors[:base] << "Can't rename #{args[0]} -> #{args[1]} (the '#{args[0]}' tag doesn't exist)"
errors.add(:base, "Can't rename #{args[0]} -> #{args[1]} (the '#{args[0]}' tag doesn't exist)")
elsif tag.post_count > MAXIMUM_RENAME_COUNT
errors[:base] << "Can't rename #{args[0]} -> #{args[1]} ('#{args[0]}' has more than #{MAXIMUM_RENAME_COUNT} posts, use an alias instead)"
errors.add(:base, "Can't rename #{args[0]} -> #{args[1]} ('#{args[0]}' has more than #{MAXIMUM_RENAME_COUNT} posts, use an alias instead)")
end
when :mass_update, :nuke
# okay
when :invalid_line
errors[:base] << "Invalid line: #{args[0]}"
errors.add(:base, "Invalid line: #{args[0]}")
else
# should never happen
@@ -113,7 +113,7 @@ class BulkUpdateRequestProcessor
def validate_script_length
if commands.size > MAXIMUM_SCRIPT_LENGTH
errors[:base] << "Bulk update request is too long (maximum size: #{MAXIMUM_SCRIPT_LENGTH} lines). Split your request into smaller chunks and try again."
errors.add(:base, "Bulk update request is too long (maximum size: #{MAXIMUM_SCRIPT_LENGTH} lines). Split your request into smaller chunks and try again.")
end
end

View File

@@ -2,37 +2,37 @@ class TagNameValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
case Tag.normalize_name(value)
when /\A_*\z/
record.errors[attribute] << "'#{value}' cannot be blank"
record.errors.add(attribute, "'#{value}' cannot be blank")
when /\*/
record.errors[attribute] << "'#{value}' cannot contain asterisks ('*')"
record.errors.add(attribute, "'#{value}' cannot contain asterisks ('*')")
when /,/
record.errors[attribute] << "'#{value}' cannot contain commas (',')"
record.errors.add(attribute, "'#{value}' cannot contain commas (',')")
when /\A~/
record.errors[attribute] << "'#{value}' cannot begin with a tilde ('~')"
record.errors.add(attribute, "'#{value}' cannot begin with a tilde ('~')")
when /\A-/
record.errors[attribute] << "'#{value}' cannot begin with a dash ('-')"
record.errors.add(attribute, "'#{value}' cannot begin with a dash ('-')")
when /\A_/
record.errors[attribute] << "'#{value}' cannot begin with an underscore"
record.errors.add(attribute, "'#{value}' cannot begin with an underscore")
when /_\z/
record.errors[attribute] << "'#{value}' cannot end with an underscore"
record.errors.add(attribute, "'#{value}' cannot end with an underscore")
when /__/
record.errors[attribute] << "'#{value}' cannot contain consecutive underscores"
record.errors.add(attribute, "'#{value}' cannot contain consecutive underscores")
when /[^[:graph:]]/
record.errors[attribute] << "'#{value}' cannot contain non-printable characters"
record.errors.add(attribute, "'#{value}' cannot contain non-printable characters")
when /[^[:ascii:]]/
record.errors[attribute] << "'#{value}' must consist of only ASCII characters"
record.errors.add(attribute, "'#{value}' must consist of only ASCII characters")
when /\A(#{PostQueryBuilder::METATAGS.join("|")}):(.+)\z/i
record.errors[attribute] << "'#{value}' cannot begin with '#{$1}:'"
record.errors.add(attribute, "'#{value}' cannot begin with '#{$1}:'")
when /\A(#{Tag.categories.regexp}):(.+)\z/i
record.errors[attribute] << "'#{value}' cannot begin with '#{$1}:'"
record.errors.add(attribute, "'#{value}' cannot begin with '#{$1}:'")
when "new", "search"
record.errors[attribute] << "'#{value}' is a reserved name and cannot be used"
record.errors.add(attribute, "'#{value}' is a reserved name and cannot be used")
when /\A(.+)_\(cosplay\)\z/i
tag_name = TagAlias.to_aliased([$1]).first
tag = Tag.find_by_name(tag_name)
if tag.present? && !tag.empty? && !tag.character?
record.errors[attribute] << "#{tag_name} must be a character tag"
record.errors.add(attribute, "#{tag_name} must be a character tag")
end
end
end

View File

@@ -61,11 +61,11 @@ class UserDeletion
def validate_deletion
if !user.authenticate_password(password)
errors[:base] << "Password is incorrect"
errors.add(:base, "Password is incorrect")
end
if user.level >= User::Levels::ADMIN
errors[:base] << "Admins cannot delete their account"
errors.add(:base, "Admins cannot delete their account")
end
end
end

View File

@@ -2,10 +2,10 @@ class UserNameValidator < ActiveModel::EachValidator
def validate_each(rec, attr, value)
name = value
rec.errors[attr] << "already exists" if User.find_by_name(name).present?
rec.errors[attr] << "must be 2 to 100 characters long" if !name.length.between?(2, 100)
rec.errors[attr] << "cannot have whitespace or colons" if name =~ /[[:space:]]|:/
rec.errors[attr] << "cannot begin or end with an underscore" if name =~ /\A_|_\z/
rec.errors[attr] << "is not allowed" if name =~ Regexp.union(Danbooru.config.user_name_blacklist)
rec.errors.add(attr, "already exists") if User.find_by_name(name).present?
rec.errors.add(attr, "must be 2 to 100 characters long") if !name.length.between?(2, 100)
rec.errors.add(attr, "cannot have whitespace or colons") if name =~ /[[:space:]]|:/
rec.errors.add(attr, "cannot begin or end with an underscore") if name =~ /\A_|_\z/
rec.errors.add(attr, "is not allowed") if name =~ Regexp.union(Danbooru.config.user_name_blacklist)
end
end