diff --git a/app/views/admin/enterprises/form/_images.html.haml b/app/views/admin/enterprises/form/_images.html.haml index 5ab27a27af..8781311f4f 100644 --- a/app/views/admin/enterprises/form/_images.html.haml +++ b/app/views/admin/enterprises/form/_images.html.haml @@ -1,6 +1,6 @@ -= render "admin/shared/attachment_field", object: @enterprise, attachment_url: @enterprise.logo_url(:thumb), attachment_name: :logo, f: f += render "admin/shared/attachment_field", attachment_url: @enterprise.logo_url(:thumb), attachment_name: :logo, f: f -= render "admin/shared/attachment_field", object: @enterprise, attachment_url: @enterprise.promo_image_url(:large), attachment_name: :promo_image, f: f do += render "admin/shared/attachment_field", attachment_url: @enterprise.promo_image_url(:large), attachment_name: :promo_image, f: f do %br/ = t('.promo_image_placeholder') %br diff --git a/app/views/admin/enterprises/form/_white_label.html.haml b/app/views/admin/enterprises/form/_white_label.html.haml index a65b3560aa..f4a427411f 100644 --- a/app/views/admin/enterprises/form/_white_label.html.haml +++ b/app/views/admin/enterprises/form/_white_label.html.haml @@ -7,7 +7,7 @@ = f.check_box :hide_ofn_navigation, { "data-controller": "checkbox-display", "data-checkbox-display-target-id-value": "white_label_logo" } %div{id: "white_label_logo"} - = render "admin/shared/attachment_field", object: @object, attachment_url: @object.white_label_logo_url, attachment_name: :white_label_logo, f: f + = render "admin/shared/attachment_field", attachment_url: @object.white_label_logo_url, attachment_name: :white_label_logo, f: f - if @object.white_label_logo.present? .row diff --git a/app/views/admin/shared/_attachment_field.html.haml b/app/views/admin/shared/_attachment_field.html.haml index d4772f23f2..f18925fa8f 100644 --- a/app/views/admin/shared/_attachment_field.html.haml +++ b/app/views/admin/shared/_attachment_field.html.haml @@ -7,14 +7,15 @@ - else = t(".#{attachment_name}_hint") .thirteen.columns - = image_tag attachment_url, class: "image-field-group__preview-image" if object.send(attachment_name).present? + - if f.object.send(attachment_name).present? && attachment_url.present? + = image_tag attachment_url, class: "image-field-group__preview-image" %br = f.file_field attachment_name, accept: "image/*" - - if object.send(attachment_name).present? + - if f.object.send(attachment_name).present? %button.button.small.red{ type: "button", "data-controller": "modal-link", "data-action": "click->modal-link#open", "data-modal-link-target-value": "remove_#{attachment_name}" } = t(".#{attachment_name}_remove") -- if object.send(attachment_name).present? +- if f.object.send(attachment_name).present? - # add to admin footer to avoid nesting forms - content_for :admin_footer do = render ModalComponent.new(id: "remove_#{attachment_name}", close_button: false, modal_class: "tiny") do @@ -22,4 +23,4 @@ = t(".#{attachment_name}_remove_confirm") %div{ class: "modal-actions justify-space-around" } %input{ class: "button icon-plus secondary", type: 'button', value: I18n.t('js.admin.modals.cancel'), "data-action": "click->modal#close" } - = button_to I18n.t('js.admin.modals.confirm'), admin_enterprise_path(object), method: :patch, params: { enterprise: { "remove_#{attachment_name}": 1 } }, form: { "data-turbo": true } + = button_to I18n.t('js.admin.modals.confirm'), admin_enterprise_path(f.object), method: :patch, params: { enterprise: { "remove_#{attachment_name}": 1 } }, form: { "data-turbo": true } diff --git a/spec/views/admin/shared/_attachment_field.html.haml_spec.rb b/spec/views/admin/shared/_attachment_field.html.haml_spec.rb new file mode 100644 index 0000000000..ca7020823b --- /dev/null +++ b/spec/views/admin/shared/_attachment_field.html.haml_spec.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +require "spec_helper" + +RSpec.describe "admin/shared/_attachment_field.html.haml" do + include FileHelper + + let(:enterprise) { create(:distributor_enterprise, white_label_logo: black_logo_file) } + let(:f) { ActionView::Helpers::FormBuilder.new(:enterprise, enterprise, view, {}) } + + it "includes a preview of the image if one was already uploaded" do + allow(view).to receive_messages( + attachment_name: :white_label_logo, + attachment_url: enterprise.white_label_logo_url, + f: + ) + + render + + expect(rendered).to include("