Fix ActiveModel::Errors#[]= deprecation warning

DEPRECATION WARNING: ActiveModel::Errors#[]= is deprecated and will be
removed in Rails 5.1. Use model.errors.add(:attachment, "content type
image/png does not match any of accepted content types pdf") instead.
(called from validate_attachment_content_type at
/home/travis/build/consul/consul/app/models/document.rb:92)
This commit is contained in:
rgarcia
2018-04-08 14:45:29 +02:00
committed by Julian Herrero
parent f668317cc1
commit b2b418284d
3 changed files with 14 additions and 14 deletions

View File

@@ -51,7 +51,7 @@ class DirectUpload
@relation.valid?
if @relation.errors.key? :attachment
errors[:attachment] = @relation.errors[:attachment]
errors.add(:attachment, @relation.errors.full_messages_for(:attachment))
end
end

View File

@@ -80,24 +80,24 @@ class Document < ActiveRecord::Base
def validate_attachment_size
if documentable_class.present? &&
attachment_file_size > documentable_class.max_file_size
errors[:attachment] = I18n.t("documents.errors.messages.in_between",
errors.add(:attachment, I18n.t("documents.errors.messages.in_between",
min: "0 Bytes",
max: "#{max_file_size(documentable_class)} MB")
max: "#{max_file_size(documentable_class)} MB"))
end
end
def validate_attachment_content_type
if documentable_class &&
!accepted_content_types(documentable_class).include?(attachment_content_type)
errors[:attachment] = I18n.t("documents.errors.messages.wrong_content_type",
content_type: attachment_content_type,
accepted_content_types: documentable_humanized_accepted_content_types(documentable_class))
errors.add(:attachment, I18n.t("documents.errors.messages.wrong_content_type",
content_type: attachment_content_type,
accepted_content_types: documentable_humanized_accepted_content_types(documentable_class)))
end
end
def attachment_presence
if attachment.blank? && cached_attachment.blank?
errors[:attachment] = I18n.t("errors.messages.blank")
errors.add(:attachment, I18n.t("errors.messages.blank"))
end
end

View File

@@ -79,23 +79,23 @@ class Image < ActiveRecord::Base
def validate_attachment_size
if imageable_class &&
attachment_file_size > 1.megabytes
errors[:attachment] = I18n.t("images.errors.messages.in_between",
min: "0 Bytes",
max: "#{imageable_max_file_size} MB")
errors.add(:attachment, I18n.t("images.errors.messages.in_between",
min: "0 Bytes",
max: "#{imageable_max_file_size} MB"))
end
end
def validate_attachment_content_type
if imageable_class && !attachment_of_valid_content_type?
errors[:attachment] = I18n.t("images.errors.messages.wrong_content_type",
content_type: attachment_content_type,
accepted_content_types: imageable_humanized_accepted_content_types)
errors.add(:attachment, I18n.t("images.errors.messages.wrong_content_type",
content_type: attachment_content_type,
accepted_content_types: imageable_humanized_accepted_content_types))
end
end
def attachment_presence
if attachment.blank? && cached_attachment.blank?
errors[:attachment] = I18n.t("errors.messages.blank")
errors.add(:attachment, I18n.t("errors.messages.blank"))
end
end