diff --git a/app/models/concerns/attachable.rb b/app/models/concerns/attachable.rb index f1a069bcb..ac6c1e33a 100644 --- a/app/models/concerns/attachable.rb +++ b/app/models/concerns/attachable.rb @@ -7,8 +7,26 @@ module Attachable attr_accessor :cached_attachment validate :attachment_presence - validate :validate_attachment_content_type, if: -> { attachment.attached? } - validate :validate_attachment_size, if: -> { attachment.attached? } + + validates :attachment, + file_content_type: { + allow: ->(record) { record.accepted_content_types }, + if: -> { association_class && attachment.attached? }, + message: ->(record, *) do + I18n.t("#{record.model_name.plural}.errors.messages.wrong_content_type", + content_type: record.attachment_content_type, + accepted_content_types: record.class.humanized_accepted_content_types) + end + }, + file_size: { + less_than_or_equal_to: ->(record) { record.max_file_size.megabytes }, + if: -> { association_class && attachment.attached? }, + message: ->(record, *) do + I18n.t("#{record.model_name.plural}.errors.messages.in_between", + min: "0 Bytes", + max: "#{record.max_file_size} MB") + end + } before_validation :set_attachment_from_cached_attachment, if: -> { cached_attachment.present? } end @@ -49,23 +67,6 @@ module Attachable private - def validate_attachment_size - if association_class && attachment_file_size > max_file_size.megabytes - errors.add(:attachment, I18n.t("#{model_name.plural}.errors.messages.in_between", - min: "0 Bytes", - max: "#{max_file_size} MB")) - end - end - - def validate_attachment_content_type - if association_class && !accepted_content_types.include?(attachment_content_type) - message = I18n.t("#{model_name.plural}.errors.messages.wrong_content_type", - content_type: attachment_content_type, - accepted_content_types: self.class.humanized_accepted_content_types) - errors.add(:attachment, message) - end - end - def attachment_presence unless attachment.attached? errors.add(:attachment, I18n.t("errors.messages.blank")) diff --git a/spec/shared/models/document_validations.rb b/spec/shared/models/document_validations.rb index 194c8fbba..3594b1a57 100644 --- a/spec/shared/models/document_validations.rb +++ b/spec/shared/models/document_validations.rb @@ -29,7 +29,7 @@ shared_examples "document validations" do |documentable_factory| end it "is not valid for attachments larger than documentable max_file_size definition" do - allow(document).to receive(:attachment_file_size).and_return(maxfilesize.megabytes + 1.byte) + allow(document.attachment).to receive(:byte_size).and_return(maxfilesize.megabytes + 1.byte) max_size_error_message = "must be in between 0 Bytes and #{maxfilesize} MB" expect(document).not_to be_valid diff --git a/spec/shared/models/image_validations.rb b/spec/shared/models/image_validations.rb index 092372550..ed26d0499 100644 --- a/spec/shared/models/image_validations.rb +++ b/spec/shared/models/image_validations.rb @@ -38,7 +38,7 @@ shared_examples "image validations" do |imageable_factory| it "is not valid for attachments larger than imageable max_file_size definition" do larger_size = Setting["uploads.images.max_size"].to_i.megabytes + 1.byte - allow(image).to receive(:attachment_file_size).and_return(larger_size) + allow(image.attachment).to receive(:byte_size).and_return(larger_size) expect(image).not_to be_valid expect(image.errors[:attachment]).to include "must be in between 0 Bytes and 1 MB"