diff --git a/app/reflexes/products_reflex.rb b/app/reflexes/products_reflex.rb index ec6c28f9fd..e6dd59e41d 100644 --- a/app/reflexes/products_reflex.rb +++ b/app/reflexes/products_reflex.rb @@ -77,10 +77,13 @@ class ProductsReflex < ApplicationReflex def edit_image id = current_id_from_element(element) product = product_finder(id).find_product + image = product.image + + image = Spree::Image.new(viewable: product) if product.image.blank? morph "#modal-component", render(partial: "admin/products_v3/edit_image", - locals: { product:, image: product.image, return_url: url }) + locals: { product:, image:, return_url: url }) end private diff --git a/app/views/admin/products_v3/_edit_image.html.haml b/app/views/admin/products_v3/_edit_image.html.haml index 7af268d64d..288e3d35c3 100644 --- a/app/views/admin/products_v3/_edit_image.html.haml +++ b/app/views/admin/products_v3/_edit_image.html.haml @@ -1,10 +1,10 @@ = render ModalComponent.new id: "#modal_edit_product_image_#{image.id}", instant: true, close_button: false do %h2= t(".title") - %p= image_tag image.variant(:product) + %p= image_tag image.persisted? ? image.variant(:product) : Spree::Image.default_image_url(:product) -# Submit to controller, because StimulusReflex doesn't support file uploads - = form_for [:admin, product, image], url: admin_product_image_path(product, image), + = form_for [:admin, product, image], html: { multipart: true }, data: { controller: "form" } do |f| %input{ type: :hidden, name: :return_url, value: return_url} = f.hidden_field :viewable_id, value: product.id diff --git a/app/views/admin/products_v3/_table.html.haml b/app/views/admin/products_v3/_table.html.haml index c508972722..faf517bd3e 100644 --- a/app/views/admin/products_v3/_table.html.haml +++ b/app/views/admin/products_v3/_table.html.haml @@ -49,13 +49,9 @@ %tbody.relaxed{ 'data-record-id': product_form.object.id } %tr %td.with-image - - if product.image.present? - %a.image-field{ href: edit_admin_product_image_path(product, product.image), data: { controller: "modal", reflex: "click->products#edit_image", "current-id": product.id} } - = image_tag product.image&.url(:mini), width: 40, height: 40 - .button.secondary.mini= t('admin.products_page.image.edit') - - else - .image-field - = image_tag Spree::Image.default_image_url(:mini), width: 40, height: 40 + %a.image-field{ href: admin_product_images_path(product), data: { controller: "modal", reflex: "click->products#edit_image", "current-id": product.id} } + = image_tag product.image&.url(:mini) || Spree::Image.default_image_url(:mini), width: 40, height: 40 + .button.secondary.mini= t('admin.products_page.image.edit') %td.field.align-left.header = product_form.hidden_field :id = product_form.text_field :name, 'aria-label': t('admin.products_page.columns.name') diff --git a/spec/system/admin/products_v3/products_spec.rb b/spec/system/admin/products_v3/products_spec.rb index 3a04e6c49e..642265abea 100644 --- a/spec/system/admin/products_v3/products_spec.rb +++ b/spec/system/admin/products_v3/products_spec.rb @@ -325,28 +325,24 @@ describe 'As an admin, I can manage products', feature: :admin_style_v3 do end describe "edit image" do - context "product has an image" do - let!(:product) { create(:product_with_image, name: "Apples") } - - before do + shared_examples "updating image" do + it "saves product image" do visit admin_products_url - end - it "updates product image" do within row_containing_name("Apples") do click_on "Edit" end within ".reveal-modal" do expect(page).to have_content "Edit product photo" - expect_page_to_have_image(product.image.url(:product)) + expect_page_to_have_image(current_img_url) # Upload a new image file attach_file 'image[attachment]', Rails.public_path.join('500.jpg'), visible: false + # It uploads automatically end - expect(page).to have_content "Loading" - expect(page).to have_content "Image has been successfully updated" + expect(page).to have_content /Image has been successfully (updated|created)/ expect(product.image.reload.url(:product)).to match /500.jpg$/ within row_containing_name("Apples") do @@ -354,6 +350,20 @@ describe 'As an admin, I can manage products', feature: :admin_style_v3 do end end end + + context "with existing image" do + let!(:product) { create(:product_with_image, name: "Apples") } + let(:current_img_url) { product.image.url(:product) } + + include_examples "updating image" + end + + context "with default image" do + let!(:product) { create(:product, name: "Apples") } + let(:current_img_url) { Spree::Image.default_image_url(:product) } + + include_examples "updating image" + end end describe "actions" do