Skip to content

Commit

Permalink
BUU Display error messages when file not valid
Browse files Browse the repository at this point in the history
- implements a turbo response in controller
- display error messages on modal -> able for user to re upload
- removes a pending in spec that now tests error message
  • Loading branch information
cyrillefr committed Jun 10, 2024
1 parent 23c175e commit 0161284
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
13 changes: 7 additions & 6 deletions app/controllers/spree/admin/images_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,20 @@ def update
@url_filters = ::ProductFilters.new.extract(request.query_parameters)
set_viewable

if @object.update(permitted_resource_params)
if @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
else
respond_with(@object)
end
rescue ActiveStorage::IntegrityError
@object.errors.add :attachment, :integrity_error
respond_with(@object)
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

def destroy
Expand Down
8 changes: 7 additions & 1 deletion app/views/spree/admin/images/edit.turbo_stream.haml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
%h2= t(".title")

-# Display image in the same way it appears in the shopfront popup
%p= image_tag @image.persisted? ? @image.url(:large) : Spree::Image.default_image_url(:large), width: 433, height: 433
- if defined?(@errors) && [email protected]?
- @errors.each do |error|
%p
= error
- else
%p= image_tag @image.persisted? ? @image.url(:large) : Spree::Image.default_image_url(:large), width: 433, height: 433


-# Submit as turbo stream to avoid full page reload.
-# TODO: show loading indicator.
Expand Down
2 changes: 0 additions & 2 deletions spec/requests/admin/images_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@
product.reload
}.not_to change{ product.image&.attachment&.filename.to_s }

pending "error status code"
expect(response).to be_unprocessable
expect(response.body).to include "Attachment has an invalid content type"
end
end
Expand Down

0 comments on commit 0161284

Please sign in to comment.