diff --git a/app/components/documents/nested_component.html.erb b/app/components/documents/nested_component.html.erb new file mode 100644 index 000000000..acff7d0e3 --- /dev/null +++ b/app/components/documents/nested_component.html.erb @@ -0,0 +1,24 @@ +
+ <%= f.label :documents, t("documents.form.title") %> +

<%= documentables_note %>

+ +
+ <%= f.fields_for :documents do |documents_builder| %> + <%= render Documents::FieldsComponent.new(documents_builder) %> + <% end %> +
+ + <%= link_to_add_association t("documents.form.add_new_document"), f, :documents, + partial: "documents/document_fields", + id: "new_document_link", + class: "button upload-document + #{"hide" if max_documents_allowed?}", + data: { + association_insertion_node: "#nested-documents", + association_insertion_method: "append" + } %> + +
"> + <%= sanitize(t("documents.max_documents_allowed_reached")) %> +
+
diff --git a/app/components/documents/nested_component.rb b/app/components/documents/nested_component.rb new file mode 100644 index 000000000..14ddd857e --- /dev/null +++ b/app/components/documents/nested_component.rb @@ -0,0 +1,28 @@ +class Documents::NestedComponent < ApplicationComponent + attr_reader :f + delegate :documentable_humanized_accepted_content_types, :max_file_size, to: :helpers + + def initialize(f) + @f = f + end + + private + + def documentable + f.object + end + + def max_documents_allowed + documentable.class.max_documents_allowed + end + + def documentables_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) + end + + def max_documents_allowed? + documentable.documents.count >= documentable.class.max_documents_allowed + end +end diff --git a/app/helpers/documentables_helper.rb b/app/helpers/documentables_helper.rb index eae1e1e1f..5dcffacb3 100644 --- a/app/helpers/documentables_helper.rb +++ b/app/helpers/documentables_helper.rb @@ -1,8 +1,4 @@ module DocumentablesHelper - def max_documents_allowed(documentable) - documentable.class.max_documents_allowed - end - def max_file_size(documentable_class) documentable_class.max_file_size / Numeric::MEGABYTE end @@ -14,14 +10,4 @@ module DocumentablesHelper def documentable_humanized_accepted_content_types(documentable_class) Setting.accepted_content_types_for("documents").join(", ") end - - def documentables_note(documentable) - t "documents.form.note", max_documents_allowed: max_documents_allowed(documentable), - accepted_content_types: documentable_humanized_accepted_content_types(documentable.class), - max_file_size: max_file_size(documentable.class) - end - - def max_documents_allowed?(documentable) - documentable.documents.count >= documentable.class.max_documents_allowed - end end diff --git a/app/views/documents/_nested_documents.html.erb b/app/views/documents/_nested_documents.html.erb index 4fd7411cf..229840e83 100644 --- a/app/views/documents/_nested_documents.html.erb +++ b/app/views/documents/_nested_documents.html.erb @@ -1,26 +1 @@ -<% documentable = f.object %> - -
- <%= f.label :documents, t("documents.form.title") %> -

<%= documentables_note(documentable) %>

- -
- <%= f.fields_for :documents do |documents_builder| %> - <%= render Documents::FieldsComponent.new(documents_builder) %> - <% end %> -
- - <%= link_to_add_association t("documents.form.add_new_document"), f, :documents, - partial: "documents/document_fields", - id: "new_document_link", - class: "button upload-document - #{"hide" if max_documents_allowed?(documentable)}", - data: { - association_insertion_node: "#nested-documents", - association_insertion_method: "append" - } %> - -
"> - <%= sanitize(t("documents.max_documents_allowed_reached")) %> -
-
+<%= render Documents::NestedComponent.new(f) %>