Skip to content

Commit

Permalink
Requested changes
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
cyrillefr committed Jun 11, 2024
1 parent 0161284 commit 9429906
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 19 deletions.
27 changes: 11 additions & 16 deletions app/controllers/spree/admin/images_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,33 +35,28 @@ def create

@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
Expand Down
Binary file added public/invalid_image.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 4 additions & 2 deletions spec/requests/admin/images_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,16 @@
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
Expand Down
23 changes: 22 additions & 1 deletion spec/system/admin/products_v3/products_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1041,13 +1041,15 @@

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)
Expand All @@ -1064,6 +1066,25 @@
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
Expand Down

0 comments on commit 9429906

Please sign in to comment.