Add image create form

Re-using the edit image form, because they're basically the same.
This commit is contained in:
David Cook
2024-01-16 16:10:19 +11:00
parent 07bef860b2
commit f72154e40c
4 changed files with 28 additions and 19 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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')

View File

@@ -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