From d14f6691dc4e66ad1845aa2b9085086c37becb70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Fri, 23 Jul 2021 19:32:04 +0200 Subject: [PATCH] Return document max file size in megabytes The same way it's done for images. We were converting the number of megabytes to bytes and then converting it to megabytes again. Instead, we can leave it as it is and only convert it to bytes when necessary (only one place). --- app/components/documents/nested_component.rb | 4 ++-- app/helpers/documentables_helper.rb | 4 ---- app/models/concerns/documentable.rb | 2 +- app/models/document.rb | 4 ++-- spec/shared/models/document_validations.rb | 2 +- 5 files changed, 6 insertions(+), 10 deletions(-) diff --git a/app/components/documents/nested_component.rb b/app/components/documents/nested_component.rb index a37587e81..50acc9b5c 100644 --- a/app/components/documents/nested_component.rb +++ b/app/components/documents/nested_component.rb @@ -1,6 +1,6 @@ class Documents::NestedComponent < ApplicationComponent attr_reader :f - delegate :documentable_humanized_accepted_content_types, :max_file_size, to: :helpers + delegate :documentable_humanized_accepted_content_types, to: :helpers def initialize(f) @f = f @@ -19,7 +19,7 @@ class Documents::NestedComponent < ApplicationComponent def note t "documents.form.note", max_documents_allowed: max_documents_allowed, accepted_content_types: documentable_humanized_accepted_content_types(documentable.class), - max_file_size: max_file_size(documentable.class) + max_file_size: documentable.class.max_file_size end def max_documents_allowed? diff --git a/app/helpers/documentables_helper.rb b/app/helpers/documentables_helper.rb index 5dcffacb3..2f75221b6 100644 --- a/app/helpers/documentables_helper.rb +++ b/app/helpers/documentables_helper.rb @@ -1,8 +1,4 @@ module DocumentablesHelper - def max_file_size(documentable_class) - documentable_class.max_file_size / Numeric::MEGABYTE - end - def accepted_content_types(documentable_class) documentable_class.accepted_content_types end diff --git a/app/models/concerns/documentable.rb b/app/models/concerns/documentable.rb index dcff3461d..dd4aa531a 100644 --- a/app/models/concerns/documentable.rb +++ b/app/models/concerns/documentable.rb @@ -12,7 +12,7 @@ module Documentable end def max_file_size - Setting["uploads.documents.max_size"].to_i.megabytes + Setting["uploads.documents.max_size"].to_i end def accepted_content_types diff --git a/app/models/document.rb b/app/models/document.rb index da9bbfd08..886367437 100644 --- a/app/models/document.rb +++ b/app/models/document.rb @@ -78,10 +78,10 @@ class Document < ApplicationRecord def validate_attachment_size if documentable_class.present? && - attachment_file_size > documentable_class.max_file_size + attachment_file_size > documentable_class.max_file_size.megabytes errors.add(:attachment, I18n.t("documents.errors.messages.in_between", min: "0 Bytes", - max: "#{max_file_size(documentable_class)} MB")) + max: "#{documentable_class.max_file_size} MB")) end end diff --git a/spec/shared/models/document_validations.rb b/spec/shared/models/document_validations.rb index 57daf7cd1..0a3356111 100644 --- a/spec/shared/models/document_validations.rb +++ b/spec/shared/models/document_validations.rb @@ -2,7 +2,7 @@ shared_examples "document validations" do |documentable_factory| include DocumentablesHelper let!(:document) { build(:document, documentable_factory.to_sym) } - let!(:maxfilesize) { max_file_size(document.documentable.class) } + let!(:maxfilesize) { document.documentable.class.max_file_size } let!(:acceptedcontenttypes) { accepted_content_types(document.documentable.class) } it "is valid" do