Move nested images partial to a component
This commit is contained in:
22
app/components/images/nested_component.html.erb
Normal file
22
app/components/images/nested_component.html.erb
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
<%= f.label image_fields, t("images.form.title") %>
|
||||||
|
<p class="help-text"><%= imageables_note %></p>
|
||||||
|
|
||||||
|
<div id="nested-image">
|
||||||
|
<%= f.fields_for image_fields do |image_builder| %>
|
||||||
|
<%= render Images::FieldsComponent.new(image_builder, imageable: imageable) %>
|
||||||
|
<% end %>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<%= link_to_add_association t("images.form.add_new_image"), f, image_fields,
|
||||||
|
force_non_association_create: true,
|
||||||
|
partial: "images/image_fields",
|
||||||
|
id: "new_image_link",
|
||||||
|
class: "button upload-image
|
||||||
|
#{"hide" if image_fields == :image && imageable.image.present?}",
|
||||||
|
render_options: {
|
||||||
|
locals: { imageable: imageable }
|
||||||
|
},
|
||||||
|
data: {
|
||||||
|
association_insertion_node: "#nested-image",
|
||||||
|
association_insertion_method: "append"
|
||||||
|
} %>
|
||||||
20
app/components/images/nested_component.rb
Normal file
20
app/components/images/nested_component.rb
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
class Images::NestedComponent < ApplicationComponent
|
||||||
|
attr_reader :f, :image_fields
|
||||||
|
delegate :imageable_humanized_accepted_content_types, :imageable_max_file_size, to: :helpers
|
||||||
|
|
||||||
|
def initialize(f, image_fields: :image)
|
||||||
|
@f = f
|
||||||
|
@image_fields = image_fields
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def imageable
|
||||||
|
f.object
|
||||||
|
end
|
||||||
|
|
||||||
|
def imageables_note
|
||||||
|
t "images.form.note", accepted_content_types: imageable_humanized_accepted_content_types,
|
||||||
|
max_file_size: imageable_max_file_size
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -14,9 +14,4 @@ module ImageablesHelper
|
|||||||
def imageable_humanized_accepted_content_types
|
def imageable_humanized_accepted_content_types
|
||||||
Setting.accepted_content_types_for("images").join(", ")
|
Setting.accepted_content_types_for("images").join(", ")
|
||||||
end
|
end
|
||||||
|
|
||||||
def imageables_note(_imageable)
|
|
||||||
t "images.form.note", accepted_content_types: imageable_humanized_accepted_content_types,
|
|
||||||
max_file_size: imageable_max_file_size
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
<%= render "shared/errors", resource: @answer %>
|
<%= render "shared/errors", resource: @answer %>
|
||||||
|
|
||||||
<div class="images">
|
<div class="images">
|
||||||
<%= render "images/nested_image", f: f, image_fields: :images %>
|
<%= render Images::NestedComponent.new(f, image_fields: :images) %>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<%= f.submit t("admin.questions.answers.images.save_image"), class: "button success" %>
|
<%= f.submit t("admin.questions.answers.images.save_image"), class: "button success" %>
|
||||||
|
|||||||
@@ -1,25 +1 @@
|
|||||||
<% image_fields ||= :image %>
|
<%= render Images::NestedComponent.new(f) %>
|
||||||
<% imageable = f.object %>
|
|
||||||
|
|
||||||
<%= f.label image_fields, t("images.form.title") %>
|
|
||||||
<p class="help-text"><%= imageables_note(imageable) %></p>
|
|
||||||
|
|
||||||
<div id="nested-image">
|
|
||||||
<%= f.fields_for image_fields do |image_builder| %>
|
|
||||||
<%= render Images::FieldsComponent.new(image_builder, imageable: imageable) %>
|
|
||||||
<% end %>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<%= link_to_add_association t("images.form.add_new_image"), f, image_fields,
|
|
||||||
force_non_association_create: true,
|
|
||||||
partial: "images/image_fields",
|
|
||||||
id: "new_image_link",
|
|
||||||
class: "button upload-image
|
|
||||||
#{"hide" if image_fields == :image && imageable.image.present?}",
|
|
||||||
render_options: {
|
|
||||||
locals: { imageable: imageable }
|
|
||||||
},
|
|
||||||
data: {
|
|
||||||
association_insertion_node: "#nested-image",
|
|
||||||
association_insertion_method: "append"
|
|
||||||
} %>
|
|
||||||
|
|||||||
Reference in New Issue
Block a user