Move nested documents partial to a component
This commit is contained in:
24
app/components/documents/nested_component.html.erb
Normal file
24
app/components/documents/nested_component.html.erb
Normal file
@@ -0,0 +1,24 @@
|
||||
<div class="documents-list">
|
||||
<%= f.label :documents, t("documents.form.title") %>
|
||||
<p class="help-text"><%= documentables_note %></p>
|
||||
|
||||
<div id="nested-documents" data-max-documents-allowed="<%= documentable.class.max_documents_allowed %>">
|
||||
<%= f.fields_for :documents do |documents_builder| %>
|
||||
<%= render Documents::FieldsComponent.new(documents_builder) %>
|
||||
<% end %>
|
||||
</div>
|
||||
|
||||
<%= 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"
|
||||
} %>
|
||||
|
||||
<div id="max-documents-notice" class="max-documents-notice callout primary text-center <%= "hide" unless max_documents_allowed? %>">
|
||||
<%= sanitize(t("documents.max_documents_allowed_reached")) %>
|
||||
</div>
|
||||
</div>
|
||||
28
app/components/documents/nested_component.rb
Normal file
28
app/components/documents/nested_component.rb
Normal file
@@ -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
|
||||
@@ -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
|
||||
|
||||
@@ -1,26 +1 @@
|
||||
<% documentable = f.object %>
|
||||
|
||||
<div class="documents-list">
|
||||
<%= f.label :documents, t("documents.form.title") %>
|
||||
<p class="help-text"><%= documentables_note(documentable) %></p>
|
||||
|
||||
<div id="nested-documents" data-max-documents-allowed="<%= documentable.class.max_documents_allowed %>">
|
||||
<%= f.fields_for :documents do |documents_builder| %>
|
||||
<%= render Documents::FieldsComponent.new(documents_builder) %>
|
||||
<% end %>
|
||||
</div>
|
||||
|
||||
<%= 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"
|
||||
} %>
|
||||
|
||||
<div id="max-documents-notice" class="max-documents-notice callout primary text-center <%= "hide" unless max_documents_allowed?(documentable) %>">
|
||||
<%= sanitize(t("documents.max_documents_allowed_reached")) %>
|
||||
</div>
|
||||
</div>
|
||||
<%= render Documents::NestedComponent.new(f) %>
|
||||
|
||||
Reference in New Issue
Block a user