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).
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
class Documents::NestedComponent < ApplicationComponent
|
class Documents::NestedComponent < ApplicationComponent
|
||||||
attr_reader :f
|
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)
|
def initialize(f)
|
||||||
@f = f
|
@f = f
|
||||||
@@ -19,7 +19,7 @@ class Documents::NestedComponent < ApplicationComponent
|
|||||||
def note
|
def note
|
||||||
t "documents.form.note", max_documents_allowed: max_documents_allowed,
|
t "documents.form.note", max_documents_allowed: max_documents_allowed,
|
||||||
accepted_content_types: documentable_humanized_accepted_content_types(documentable.class),
|
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
|
end
|
||||||
|
|
||||||
def max_documents_allowed?
|
def max_documents_allowed?
|
||||||
|
|||||||
@@ -1,8 +1,4 @@
|
|||||||
module DocumentablesHelper
|
module DocumentablesHelper
|
||||||
def max_file_size(documentable_class)
|
|
||||||
documentable_class.max_file_size / Numeric::MEGABYTE
|
|
||||||
end
|
|
||||||
|
|
||||||
def accepted_content_types(documentable_class)
|
def accepted_content_types(documentable_class)
|
||||||
documentable_class.accepted_content_types
|
documentable_class.accepted_content_types
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ module Documentable
|
|||||||
end
|
end
|
||||||
|
|
||||||
def max_file_size
|
def max_file_size
|
||||||
Setting["uploads.documents.max_size"].to_i.megabytes
|
Setting["uploads.documents.max_size"].to_i
|
||||||
end
|
end
|
||||||
|
|
||||||
def accepted_content_types
|
def accepted_content_types
|
||||||
|
|||||||
@@ -78,10 +78,10 @@ class Document < ApplicationRecord
|
|||||||
|
|
||||||
def validate_attachment_size
|
def validate_attachment_size
|
||||||
if documentable_class.present? &&
|
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",
|
errors.add(:attachment, I18n.t("documents.errors.messages.in_between",
|
||||||
min: "0 Bytes",
|
min: "0 Bytes",
|
||||||
max: "#{max_file_size(documentable_class)} MB"))
|
max: "#{documentable_class.max_file_size} MB"))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ shared_examples "document validations" do |documentable_factory|
|
|||||||
include DocumentablesHelper
|
include DocumentablesHelper
|
||||||
|
|
||||||
let!(:document) { build(:document, documentable_factory.to_sym) }
|
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) }
|
let!(:acceptedcontenttypes) { accepted_content_types(document.documentable.class) }
|
||||||
|
|
||||||
it "is valid" do
|
it "is valid" do
|
||||||
|
|||||||
Reference in New Issue
Block a user