Requested changes

- refined the code and applied it to create method too
- modified one request spec to work with turbo_stream
- added  2 examples in system specs
This commit is contained in:
cyrillefr
2024-06-11 22:29:12 +02:00
parent 01612843b4
commit 942990612b
4 changed files with 37 additions and 19 deletions

View File

@@ -35,33 +35,28 @@ module Spree
@object.attributes = permitted_resource_params
if @object.save
flash[:success] = flash_message_for(@object, :successfully_created)
redirect_to location_after_save
else
respond_with(@object)
@object.save!
flash[:success] = flash_message_for(@object, :successfully_created)
redirect_to location_after_save
rescue ActiveRecord::RecordInvalid => e
@errors = e.record.errors.map(&:full_message)
respond_to do |format|
format.turbo_stream { render :edit }
end
rescue ActiveStorage::IntegrityError
@object.errors.add :attachment, :integrity_error
respond_with(@object)
end
def update
@url_filters = ::ProductFilters.new.extract(request.query_parameters)
set_viewable
if @object.update!(permitted_resource_params)
flash[:success] = flash_message_for(@object, :successfully_updated)
@object.update!(permitted_resource_params)
flash[:success] = flash_message_for(@object, :successfully_updated)
respond_with do |format|
format.html { redirect_to location_after_save }
format.turbo_stream
end
end
redirect_to location_after_save
rescue ActiveRecord::RecordInvalid => e
@errors = e.record.errors.map(&:full_message)
respond_with do |format|
format.html { respond_with(@object) }
format.turbo_stream { render :edit }
end
end

BIN
public/invalid_image.jpg Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 MiB

View File

@@ -55,14 +55,16 @@ RSpec.describe "/admin/products/:product_id/images", type: :request do
end
describe "POST /admin/products/:product_id/images" do
subject { post(spree.admin_product_images_path(product), params:) }
subject { post(spree.admin_product_images_path(product), params:, as: :turbo_stream) }
it_behaves_like "updating images"
end
describe "PATCH /admin/products/:product_id/images/:id" do
let!(:product) { create(:product_with_image) }
subject { patch(spree.admin_product_image_path(product, product.image), params:) }
subject {
patch(spree.admin_product_image_path(product, product.image), params:, as: :turbo_stream)
}
it_behaves_like "updating images"
end

View File

@@ -1041,13 +1041,15 @@ RSpec.describe 'As an enterprise user, I can manage my products', feature: :admi
describe "edit image" do
shared_examples "updating image" do
it "saves product image" do
before do
visit admin_products_url
within row_containing_name("Apples") do
click_on "Edit"
end
end
it "saves product image" do
within ".reveal-modal" do
expect(page).to have_content "Edit product photo"
expect_page_to_have_image(current_img_url)
@@ -1064,6 +1066,25 @@ RSpec.describe 'As an enterprise user, I can manage my products', feature: :admi
expect_page_to_have_image('500.jpg')
end
end
it 'shows a modal telling not a valid image when uploading wrong type of file' do
within ".reveal-modal" do
attach_file 'image[attachment]',
Rails.public_path.join('Terms-of-service.pdf'),
visible: false
expect(page).to have_content /Attachment is not a valid image/
expect(page).to have_content /Attachment has an invalid content type/
end
end
it 'shows a modal telling not a valid image when uploading a non valid image file' do
within ".reveal-modal" do
attach_file 'image[attachment]',
Rails.public_path.join('invalid_image.jpg'),
visible: false
expect(page).to have_content /Attachment is not a valid image/
end
end
end
context "with existing image" do