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:
@@ -17,7 +17,7 @@ class EmailsController < ApplicationController
|
||||
if @user.authenticate_password(params[:user][:password])
|
||||
@user.update(email_address_attributes: { address: params[:user][:email] })
|
||||
else
|
||||
@user.errors[:base] << "Password was incorrect"
|
||||
@user.errors.add(:base, "Password was incorrect")
|
||||
end
|
||||
|
||||
if @user.errors.none?
|
||||
|
||||
@@ -12,7 +12,7 @@ class PasswordsController < ApplicationController
|
||||
if @user.authenticate_password(params[:user][:old_password]) || @user.authenticate_login_key(params[:user][:signed_user_id])
|
||||
@user.update(password: params[:user][:password], password_confirmation: params[:user][:password_confirmation])
|
||||
else
|
||||
@user.errors[:base] << "Incorrect password"
|
||||
@user.errors.add(:base, "Incorrect password")
|
||||
end
|
||||
|
||||
flash[:notice] = @user.errors.none? ? "Password updated" : @user.errors.full_messages.join("; ")
|
||||
|
||||
@@ -80,7 +80,7 @@ class UsersController < ApplicationController
|
||||
flash[:notice] = "Sign up failed"
|
||||
elsif @user.email_address&.invalid?(:deliverable)
|
||||
flash[:notice] = "Sign up failed: email address is invalid or doesn't exist"
|
||||
@user.errors[:base] << @user.email_address.errors.full_messages.join("; ")
|
||||
@user.errors.add(:base, @user.email_address.errors.full_messages.join("; "))
|
||||
elsif !@user.save
|
||||
flash[:notice] = "Sign up failed: #{@user.errors.full_messages.join("; ")}"
|
||||
else
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -156,7 +156,7 @@ class Artist < ApplicationRecord
|
||||
return unless !is_deleted? && name_changed? && tag.present?
|
||||
|
||||
if tag.category_name != "Artist" && !tag.empty?
|
||||
errors[:base] << "'#{name}' is a #{tag.category_name.downcase} tag; artist entries can only be created for artist tags"
|
||||
errors.add(:base, "'#{name}' is a #{tag.category_name.downcase} tag; artist entries can only be created for artist tags")
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -113,11 +113,11 @@ class ArtistUrl < ApplicationRecord
|
||||
end
|
||||
|
||||
def validate_scheme(uri)
|
||||
errors[:url] << "'#{uri}' must begin with http:// or https:// " unless uri.scheme.in?(%w[http https])
|
||||
errors.add(:url, "'#{uri}' must begin with http:// or https:// ") unless uri.scheme.in?(%w[http https])
|
||||
end
|
||||
|
||||
def validate_hostname(uri)
|
||||
errors[:url] << "'#{uri}' has a hostname '#{uri.host}' that does not contain a dot" unless uri.host&.include?('.')
|
||||
errors.add(:url, "'#{uri}' has a hostname '#{uri.host}' that does not contain a dot") unless uri.host&.include?('.')
|
||||
end
|
||||
|
||||
def validate_url_format
|
||||
@@ -125,7 +125,7 @@ class ArtistUrl < ApplicationRecord
|
||||
validate_scheme(uri)
|
||||
validate_hostname(uri)
|
||||
rescue Addressable::URI::InvalidURIError => error
|
||||
errors[:url] << "'#{uri}' is malformed: #{error}"
|
||||
errors.add(:url, "'#{uri}' is malformed: #{error}")
|
||||
end
|
||||
|
||||
def self.searchable_includes
|
||||
|
||||
@@ -45,7 +45,7 @@ class Ban < ApplicationRecord
|
||||
end
|
||||
|
||||
def validate_user_is_bannable
|
||||
self.errors[:user] << "is already banned" if user.is_banned?
|
||||
errors.add(:user, "is already banned") if user.is_banned?
|
||||
end
|
||||
|
||||
def update_user_on_create
|
||||
|
||||
@@ -97,7 +97,7 @@ class BulkUpdateRequest < ApplicationRecord
|
||||
|
||||
def validate_script
|
||||
if processor.invalid?(:request)
|
||||
errors[:base] << processor.errors.full_messages.join("; ")
|
||||
errors.add(:base, processor.errors.full_messages.join("; "))
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -26,7 +26,7 @@ class CommentVote < ApplicationRecord
|
||||
|
||||
def validate_comment_can_be_down_voted
|
||||
if is_positive? && comment.creator == CurrentUser.user
|
||||
errors.add :base, "You cannot upvote your own comments"
|
||||
errors.add(:base, "You cannot upvote your own comments")
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -158,7 +158,7 @@ class Dmail < ApplicationRecord
|
||||
return if from.blank? || from.is_gold?
|
||||
|
||||
if from.dmails.where("created_at > ?", 1.hour.ago).group(:to).reorder(nil).count.size >= 10
|
||||
errors[:base] << "You can't send dmails to more than 10 users per hour"
|
||||
errors.add(:base, "You can't send dmails to more than 10 users per hour")
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ class EmailAddress < ApplicationRecord
|
||||
|
||||
def validate_deliverable
|
||||
if EmailValidator.undeliverable?(address)
|
||||
errors[:address] << "is invalid or does not exist"
|
||||
errors.add(:address, "is invalid or does not exist")
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -56,13 +56,13 @@ class FavoriteGroup < ApplicationRecord
|
||||
if !creator.is_platinum?
|
||||
error += " Upgrade your account to create more."
|
||||
end
|
||||
self.errors.add(:base, error)
|
||||
errors.add(:base, error)
|
||||
end
|
||||
end
|
||||
|
||||
def validate_number_of_posts
|
||||
if post_count > 10_000
|
||||
errors[:base] << "Favorite groups can have up to 10,000 posts each"
|
||||
errors.add(:base, "Favorite groups can have up to 10,000 posts each")
|
||||
end
|
||||
end
|
||||
|
||||
@@ -72,12 +72,12 @@ class FavoriteGroup < ApplicationRecord
|
||||
nonexisting_post_ids = added_post_ids - existing_post_ids
|
||||
|
||||
if nonexisting_post_ids.present?
|
||||
errors[:base] << "Cannot add invalid post(s) to favgroup: #{nonexisting_post_ids.to_sentence}"
|
||||
errors.add(:base, "Cannot add invalid post(s) to favgroup: #{nonexisting_post_ids.to_sentence}")
|
||||
end
|
||||
|
||||
duplicate_post_ids = post_ids.group_by(&:itself).transform_values(&:size).select { |id, count| count > 1 }.keys
|
||||
if duplicate_post_ids.present?
|
||||
errors[:base] << "Favgroup already contains post #{duplicate_post_ids.to_sentence}"
|
||||
errors.add(:base, "Favgroup already contains post #{duplicate_post_ids.to_sentence}")
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -48,19 +48,19 @@ class IpBan < ApplicationRecord
|
||||
|
||||
def validate_ip_addr
|
||||
if ip_addr.blank?
|
||||
errors[:ip_addr] << "is invalid"
|
||||
errors.add(:ip_addr, "is invalid")
|
||||
elsif ip_addr.private? || ip_addr.loopback? || ip_addr.link_local?
|
||||
errors[:ip_addr] << "must be a public address"
|
||||
errors.add(:ip_addr, "must be a public address")
|
||||
elsif full_ban? && ip_addr.ipv4? && ip_addr.prefix < 24
|
||||
errors[:ip_addr] << "may not have a subnet bigger than /24"
|
||||
errors.add(:ip_addr, "may not have a subnet bigger than /24")
|
||||
elsif partial_ban? && ip_addr.ipv4? && ip_addr.prefix < 8
|
||||
errors[:ip_addr] << "may not have a subnet bigger than /8"
|
||||
errors.add(:ip_addr, "may not have a subnet bigger than /8")
|
||||
elsif full_ban? && ip_addr.ipv6? && ip_addr.prefix < 64
|
||||
errors[:ip_addr] << "may not have a subnet bigger than /64"
|
||||
errors.add(:ip_addr, "may not have a subnet bigger than /64")
|
||||
elsif partial_ban? && ip_addr.ipv6? && ip_addr.prefix < 20
|
||||
errors[:ip_addr] << "may not have a subnet bigger than /20"
|
||||
errors.add(:ip_addr, "may not have a subnet bigger than /20")
|
||||
elsif new_record? && IpBan.active.ip_matches(subnetted_ip).exists?
|
||||
errors[:ip_addr] << "is already banned"
|
||||
errors.add(:ip_addr, "is already banned")
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -26,13 +26,13 @@ class Note < ApplicationRecord
|
||||
extend SearchMethods
|
||||
|
||||
def validate_post_is_not_locked
|
||||
errors[:post] << "is note locked" if post.is_note_locked?
|
||||
errors.add(:post, "is note locked") if post.is_note_locked?
|
||||
end
|
||||
|
||||
def note_within_image
|
||||
return false unless post.present?
|
||||
if x < 0 || y < 0 || (x > post.image_width) || (y > post.image_height) || width < 0 || height < 0 || (x + width > post.image_width) || (y + height > post.image_height)
|
||||
self.errors.add(:note, "must be inside the image")
|
||||
errors.add(:note, "must be inside the image")
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -147,7 +147,7 @@ class Pool < ApplicationRecord
|
||||
|
||||
def updater_can_edit_deleted
|
||||
if is_deleted? && !Pundit.policy!([CurrentUser.user, nil], self).update?
|
||||
errors[:base] << "You cannot update pools that are deleted"
|
||||
errors.add(:base, "You cannot update pools that are deleted")
|
||||
end
|
||||
end
|
||||
|
||||
@@ -254,23 +254,23 @@ class Pool < ApplicationRecord
|
||||
def validate_name
|
||||
case name
|
||||
when /\A(any|none|series|collection)\z/i
|
||||
errors[:name] << "cannot be any of the following names: any, none, series, collection"
|
||||
errors.add(:name, "cannot be any of the following names: any, none, series, collection")
|
||||
when /,/
|
||||
errors[:name] << "cannot contain commas"
|
||||
errors.add(:name, "cannot contain commas")
|
||||
when /\*/
|
||||
errors[:name] << "cannot contain asterisks"
|
||||
errors.add(:name, "cannot contain asterisks")
|
||||
when /\A_/
|
||||
errors[:name] << "cannot begin with an underscore"
|
||||
errors.add(:name, "cannot begin with an underscore")
|
||||
when /_\z/
|
||||
errors[:name] << "cannot end with an underscore"
|
||||
errors.add(:name, "cannot end with an underscore")
|
||||
when /__/
|
||||
errors[:name] << "cannot contain consecutive underscores"
|
||||
errors.add(:name, "cannot contain consecutive underscores")
|
||||
when /[^[:graph:]]/
|
||||
errors[:name] << "cannot contain non-printable characters"
|
||||
errors.add(:name, "cannot contain non-printable characters")
|
||||
when ""
|
||||
errors[:name] << "cannot be blank"
|
||||
errors.add(:name, "cannot be blank")
|
||||
when /\A[0-9]+\z/
|
||||
errors[:name] << "cannot contain only digits"
|
||||
errors.add(:name, "cannot contain only digits")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -490,7 +490,7 @@ class Post < ApplicationRecord
|
||||
|
||||
invalid_tags.each do |tag|
|
||||
tag.errors.messages.each do |attribute, messages|
|
||||
warnings[:base] << "Couldn't add tag: #{messages.join(';')}"
|
||||
warnings.add(:base, "Couldn't add tag: #{messages.join(';')}")
|
||||
end
|
||||
end
|
||||
|
||||
@@ -976,7 +976,7 @@ class Post < ApplicationRecord
|
||||
module DeletionMethods
|
||||
def expunge!
|
||||
if is_status_locked?
|
||||
self.errors.add(:is_status_locked, "; cannot delete post")
|
||||
errors.add(:is_status_locked, "; cannot delete post")
|
||||
return false
|
||||
end
|
||||
|
||||
@@ -1082,11 +1082,11 @@ class Post < ApplicationRecord
|
||||
def copy_notes_to(other_post, copy_tags: NOTE_COPY_TAGS)
|
||||
transaction do
|
||||
if id == other_post.id
|
||||
errors.add :base, "Source and destination posts are the same"
|
||||
errors.add(:base, "Source and destination posts are the same")
|
||||
return false
|
||||
end
|
||||
unless has_notes?
|
||||
errors.add :post, "has no notes"
|
||||
errors.add(:post, "has no notes")
|
||||
return false
|
||||
end
|
||||
|
||||
@@ -1357,8 +1357,7 @@ class Post < ApplicationRecord
|
||||
module ValidationMethods
|
||||
def post_is_not_its_own_parent
|
||||
if !new_record? && id == parent_id
|
||||
errors[:base] << "Post cannot have itself as a parent"
|
||||
false
|
||||
errors.add(:base, "Post cannot have itself as a parent")
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1372,7 +1371,7 @@ class Post < ApplicationRecord
|
||||
end
|
||||
|
||||
def uploader_is_not_limited
|
||||
errors[:uploader] << uploader.upload_limit.limit_reason if uploader.upload_limit.limited?
|
||||
errors.add(:uploader, uploader.upload_limit.limit_reason) if uploader.upload_limit.limited?
|
||||
end
|
||||
|
||||
def added_tags_are_valid
|
||||
@@ -1382,12 +1381,12 @@ class Post < ApplicationRecord
|
||||
if new_general_tags.present?
|
||||
n = new_general_tags.size
|
||||
tag_wiki_links = new_general_tags.map { |tag| "[[#{tag.name}]]" }
|
||||
self.warnings[:base] << "Created #{n} new #{(n == 1) ? "tag" : "tags"}: #{tag_wiki_links.join(", ")}"
|
||||
warnings.add(:base, "Created #{n} new #{(n == 1) ? "tag" : "tags"}: #{tag_wiki_links.join(", ")}")
|
||||
end
|
||||
|
||||
new_artist_tags.each do |tag|
|
||||
if tag.artist.blank?
|
||||
self.warnings[:base] << "Artist [[#{tag.name}]] requires an artist entry. \"Create new artist entry\":[/artists/new?artist%5Bname%5D=#{CGI.escape(tag.name)}]"
|
||||
warnings.add(:base, "Artist [[#{tag.name}]] requires an artist entry. \"Create new artist entry\":[/artists/new?artist%5Bname%5D=#{CGI.escape(tag.name)}]")
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1398,7 +1397,7 @@ class Post < ApplicationRecord
|
||||
|
||||
if unremoved_tags.present?
|
||||
unremoved_tags_list = unremoved_tags.map { |t| "[[#{t}]]" }.to_sentence
|
||||
self.warnings[:base] << "#{unremoved_tags_list} could not be removed. Check for implications and try again"
|
||||
warnings.add(:base, "#{unremoved_tags_list} could not be removed. Check for implications and try again")
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1409,21 +1408,21 @@ class Post < ApplicationRecord
|
||||
return if tags.any?(&:artist?)
|
||||
return if Sources::Strategies.find(source).is_a?(Sources::Strategies::Null)
|
||||
|
||||
self.warnings[:base] << "Artist tag is required. \"Create new artist tag\":[/artists/new?artist%5Bsource%5D=#{CGI.escape(source)}]. Ask on the forum if you need naming help"
|
||||
warnings.add(:base, "Artist tag is required. \"Create new artist tag\":[/artists/new?artist%5Bsource%5D=#{CGI.escape(source)}]. Ask on the forum if you need naming help")
|
||||
end
|
||||
|
||||
def has_copyright_tag
|
||||
return if !new_record?
|
||||
return if has_tag?("copyright_request") || tags.any?(&:copyright?)
|
||||
|
||||
self.warnings[:base] << "Copyright tag is required. Consider adding [[copyright request]] or [[original]]"
|
||||
warnings.add(:base, "Copyright tag is required. Consider adding [[copyright request]] or [[original]]")
|
||||
end
|
||||
|
||||
def has_enough_tags
|
||||
return if !new_record?
|
||||
|
||||
if tags.count(&:general?) < 10
|
||||
self.warnings[:base] << "Uploads must have at least 10 general tags. Read [[howto:tag]] for guidelines on tagging your uploads"
|
||||
warnings.add(:base, "Uploads must have at least 10 general tags. Read [[howto:tag]] for guidelines on tagging your uploads")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -28,11 +28,11 @@ class PostAppeal < ApplicationRecord
|
||||
extend SearchMethods
|
||||
|
||||
def validate_creator_is_not_limited
|
||||
errors[:creator] << "have reached your appeal limit" if creator.is_appeal_limited?
|
||||
errors.add(:creator, "have reached your appeal limit") if creator.is_appeal_limited?
|
||||
end
|
||||
|
||||
def validate_post_is_appealable
|
||||
errors[:post] << "cannot be appealed" if post.is_status_locked? || !post.is_appealable?
|
||||
errors.add(:post, "cannot be appealed") if post.is_status_locked? || !post.is_appealable?
|
||||
end
|
||||
|
||||
def self.searchable_includes
|
||||
|
||||
@@ -51,7 +51,7 @@ class PostDisapproval < ApplicationRecord
|
||||
|
||||
def validate_disapproval
|
||||
if post.is_active?
|
||||
errors[:post] << "is already active and cannot be disapproved"
|
||||
errors.add(:post, "is already active and cannot be disapproved")
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -95,17 +95,17 @@ class PostFlag < ApplicationRecord
|
||||
end
|
||||
|
||||
def validate_creator_is_not_limited
|
||||
errors[:creator] << "have reached your flag limit" if creator.is_flag_limited? && !is_deletion
|
||||
errors.add(:creator, "have reached your flag limit") if creator.is_flag_limited? && !is_deletion
|
||||
end
|
||||
|
||||
def validate_post
|
||||
errors[:post] << "is pending and cannot be flagged" if post.is_pending? && !is_deletion
|
||||
errors[:post] << "is deleted and cannot be flagged" if post.is_deleted? && !is_deletion
|
||||
errors[:post] << "is locked and cannot be flagged" if post.is_status_locked?
|
||||
errors.add(:post, "is pending and cannot be flagged") if post.is_pending? && !is_deletion
|
||||
errors.add(:post, "is deleted and cannot be flagged") if post.is_deleted? && !is_deletion
|
||||
errors.add(:post, "is locked and cannot be flagged") if post.is_status_locked?
|
||||
|
||||
flag = post.flags.in_cooldown.last
|
||||
if !is_deletion && flag.present?
|
||||
errors[:post] << "cannot be flagged more than once every #{Danbooru.config.moderation_period.inspect} (last flagged: #{flag.created_at.to_s(:long)})"
|
||||
errors.add(:post, "cannot be flagged more than once every #{Danbooru.config.moderation_period.inspect} (last flagged: #{flag.created_at.to_s(:long)})")
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -182,7 +182,7 @@ class SavedSearch < ApplicationRecord
|
||||
|
||||
def validate_count
|
||||
if user.saved_searches.count >= user.max_saved_searches
|
||||
self.errors[:user] << "can only have up to #{user.max_saved_searches} " + "saved search".pluralize(user.max_saved_searches)
|
||||
errors.add(:user, "can only have up to #{user.max_saved_searches} " + "saved search".pluralize(user.max_saved_searches))
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -23,7 +23,7 @@ class TagAlias < TagRelationship
|
||||
|
||||
tag_alias = TagAlias.active.find_by(antecedent_name: consequent_name)
|
||||
if tag_alias.present? && tag_alias.consequent_name != antecedent_name
|
||||
errors[:base] << "#{tag_alias.antecedent_name} is already aliased to #{tag_alias.consequent_name}"
|
||||
errors.add(:base, "#{tag_alias.antecedent_name} is already aliased to #{tag_alias.consequent_name}")
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -64,7 +64,7 @@ class TagImplication < TagRelationship
|
||||
# We don't want a -> b -> a chains
|
||||
implied_tags = TagImplication.tags_implied_by(consequent_name).map(&:name)
|
||||
if implied_tags.include?(antecedent_name)
|
||||
errors[:base] << "Tag implication can not create a circular relation with another tag implication"
|
||||
errors.add(:base, "Tag implication can not create a circular relation with another tag implication")
|
||||
end
|
||||
end
|
||||
|
||||
@@ -77,7 +77,7 @@ class TagImplication < TagRelationship
|
||||
implied_tags = implications.tags_implied_by(antecedent_name).map(&:name)
|
||||
|
||||
if implied_tags.include?(consequent_name)
|
||||
errors[:base] << "#{antecedent_name} already implies #{consequent_name} through another implication"
|
||||
errors.add(:base, "#{antecedent_name} already implies #{consequent_name} through another implication")
|
||||
end
|
||||
end
|
||||
|
||||
@@ -86,7 +86,7 @@ class TagImplication < TagRelationship
|
||||
|
||||
# We don't want to implicate a -> b if a is already aliased to c
|
||||
if TagAlias.active.exists?(["antecedent_name = ?", antecedent_name])
|
||||
errors[:base] << "Antecedent tag must not be aliased to another tag"
|
||||
errors.add(:base, "Antecedent tag must not be aliased to another tag")
|
||||
end
|
||||
end
|
||||
|
||||
@@ -95,13 +95,13 @@ class TagImplication < TagRelationship
|
||||
|
||||
# We don't want to implicate a -> b if b is already aliased to c
|
||||
if TagAlias.active.exists?(["antecedent_name = ?", consequent_name])
|
||||
errors[:base] << "Consequent tag must not be aliased to another tag"
|
||||
errors.add(:base, "Consequent tag must not be aliased to another tag")
|
||||
end
|
||||
end
|
||||
|
||||
def tag_categories_are_compatible
|
||||
if antecedent_tag.category != consequent_tag.category
|
||||
errors[:base] << "Can't imply a #{antecedent_tag.category_name.downcase} tag to a #{consequent_tag.category_name.downcase} tag"
|
||||
errors.add(:base, "Can't imply a #{antecedent_tag.category_name.downcase} tag to a #{consequent_tag.category_name.downcase} tag")
|
||||
end
|
||||
end
|
||||
|
||||
@@ -114,24 +114,24 @@ class TagImplication < TagRelationship
|
||||
return if antecedent_tag.empty? || consequent_tag.empty?
|
||||
|
||||
if antecedent_tag.post_count < MINIMUM_TAG_COUNT
|
||||
errors[:base] << "'#{antecedent_name}' must have at least #{MINIMUM_TAG_COUNT} posts"
|
||||
errors.add(:base, "'#{antecedent_name}' must have at least #{MINIMUM_TAG_COUNT} posts")
|
||||
elsif antecedent_tag.post_count < (MINIMUM_TAG_PERCENTAGE * consequent_tag.post_count)
|
||||
errors[:base] << "'#{antecedent_name}' must have at least #{(MINIMUM_TAG_PERCENTAGE * consequent_tag.post_count).to_i} posts"
|
||||
errors.add(:base, "'#{antecedent_name}' must have at least #{(MINIMUM_TAG_PERCENTAGE * consequent_tag.post_count).to_i} posts")
|
||||
end
|
||||
|
||||
max_count = MAXIMUM_TAG_PERCENTAGE * PostQueryBuilder.new("~#{antecedent_name} ~#{consequent_name}").fast_count(timeout: 0).to_i
|
||||
if antecedent_tag.post_count > max_count && max_count > 0
|
||||
errors[:base] << "'#{antecedent_name}' can't make up than #{(MAXIMUM_TAG_PERCENTAGE * 100).to_i}% of '#{consequent_name}'"
|
||||
errors.add(:base, "'#{antecedent_name}' can't make up than #{(MAXIMUM_TAG_PERCENTAGE * 100).to_i}% of '#{consequent_name}'")
|
||||
end
|
||||
end
|
||||
|
||||
def has_wiki_page
|
||||
if !antecedent_tag.empty? && antecedent_wiki.blank?
|
||||
errors[:base] << "'#{antecedent_name}' must have a wiki page"
|
||||
errors.add(:base, "'#{antecedent_name}' must have a wiki page")
|
||||
end
|
||||
|
||||
if !consequent_tag.empty? && consequent_wiki.blank?
|
||||
errors[:base] << "'#{consequent_name}' must have a wiki page"
|
||||
errors.add(:base, "'#{consequent_name}' must have a wiki page")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -114,7 +114,7 @@ class TagRelationship < ApplicationRecord
|
||||
|
||||
def antecedent_and_consequent_are_different
|
||||
if antecedent_name == consequent_name
|
||||
errors[:base] << "Cannot alias or implicate a tag to itself"
|
||||
errors.add(:base, "Cannot alias or implicate a tag to itself")
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -12,13 +12,13 @@ class Upload < ApplicationRecord
|
||||
|
||||
def validate_file_ext(record)
|
||||
if record.file_ext == "bin"
|
||||
record.errors[:file_ext] << "is invalid (only JPEG, PNG, GIF, SWF, MP4, and WebM files are allowed"
|
||||
record.errors.add(:file_ext, "is invalid (only JPEG, PNG, GIF, SWF, MP4, and WebM files are allowed")
|
||||
end
|
||||
end
|
||||
|
||||
def validate_integrity(record)
|
||||
if record.media_file.is_corrupt?
|
||||
record.errors[:file] << "is corrupted"
|
||||
record.errors.add(:file, "is corrupted")
|
||||
end
|
||||
end
|
||||
|
||||
@@ -37,24 +37,24 @@ class Upload < ApplicationRecord
|
||||
return
|
||||
end
|
||||
|
||||
record.errors[:md5] << "duplicate: #{md5_post.id}"
|
||||
record.errors.add(:md5, "duplicate: #{md5_post.id}")
|
||||
end
|
||||
|
||||
def validate_resolution(record)
|
||||
resolution = record.image_width.to_i * record.image_height.to_i
|
||||
|
||||
if resolution > Danbooru.config.max_image_resolution
|
||||
record.errors[:base] << "image resolution is too large (resolution: #{(resolution / 1_000_000.0).round(1)} megapixels (#{record.image_width}x#{record.image_height}); max: #{Danbooru.config.max_image_resolution / 1_000_000} megapixels)"
|
||||
record.errors.add(:base, "image resolution is too large (resolution: #{(resolution / 1_000_000.0).round(1)} megapixels (#{record.image_width}x#{record.image_height}); max: #{Danbooru.config.max_image_resolution / 1_000_000} megapixels)")
|
||||
elsif record.image_width > Danbooru.config.max_image_width
|
||||
record.errors[:image_width] << "is too large (width: #{record.image_width}; max width: #{Danbooru.config.max_image_width})"
|
||||
record.errors.add(:image_width, "is too large (width: #{record.image_width}; max width: #{Danbooru.config.max_image_width})")
|
||||
elsif record.image_height > Danbooru.config.max_image_height
|
||||
record.errors[:image_height] << "is too large (height: #{record.image_height}; max height: #{Danbooru.config.max_image_height})"
|
||||
record.errors.add(:image_height, "is too large (height: #{record.image_height}; max height: #{Danbooru.config.max_image_height})")
|
||||
end
|
||||
end
|
||||
|
||||
def validate_video_duration(record)
|
||||
if !record.uploader.is_admin? && record.media_file.is_video? && record.media_file.duration > 120
|
||||
record.errors[:base] << "video must not be longer than 2 minutes"
|
||||
record.errors.add(:base, "video must not be longer than 2 minutes")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -30,7 +30,7 @@ class UserNameChangeRequest < ApplicationRecord
|
||||
|
||||
def not_limited
|
||||
if UserNameChangeRequest.unscoped.where(user: user).where("created_at >= ?", 1.week.ago).exists?
|
||||
errors[:base] << "You can only submit one name change request per week"
|
||||
errors.add(:base, "You can only submit one name change request per week")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -116,13 +116,13 @@ class WikiPage < ApplicationRecord
|
||||
|
||||
tag_was = Tag.find_by_name(Tag.normalize_name(title_was))
|
||||
if tag_was.present? && !tag_was.empty?
|
||||
warnings[:base] << %!Warning: {{#{title_was}}} still has #{tag_was.post_count} #{"post".pluralize(tag_was.post_count)}. Be sure to move the posts!
|
||||
warnings.add(:base, %!Warning: {{#{title_was}}} still has #{tag_was.post_count} #{"post".pluralize(tag_was.post_count)}. Be sure to move the posts!)
|
||||
end
|
||||
|
||||
broken_wikis = WikiPage.linked_to(title_was)
|
||||
if broken_wikis.count > 0
|
||||
broken_wiki_search = Rails.application.routes.url_helpers.wiki_pages_path(search: { linked_to: title_was })
|
||||
warnings[:base] << %!Warning: [[#{title_was}]] is still linked from "#{broken_wikis.count} #{"other wiki page".pluralize(broken_wikis.count)}":[#{broken_wiki_search}]. Update #{(broken_wikis.count > 1) ? "these wikis" : "this wiki"} to link to [[#{title}]] instead!
|
||||
warnings.add(:base, %!Warning: [[#{title_was}]] is still linked from "#{broken_wikis.count} #{"other wiki page".pluralize(broken_wikis.count)}":[#{broken_wiki_search}]. Update #{(broken_wikis.count > 1) ? "these wikis" : "this wiki"} to link to [[#{title}]] instead!)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user