From ad56105d316cfb854851a347d56ab503164cd4c1 Mon Sep 17 00:00:00 2001 From: Adrien Chauve Date: Mon, 11 Oct 2021 12:10:11 +0200 Subject: [PATCH 1/4] Display error messages on product image creation (not only edition) --- app/views/spree/admin/images/new.html.haml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/views/spree/admin/images/new.html.haml b/app/views/spree/admin/images/new.html.haml index 651bddf3e1..78ebc0ea19 100644 --- a/app/views/spree/admin/images/new.html.haml +++ b/app/views/spree/admin/images/new.html.haml @@ -1,3 +1,5 @@ += render partial: 'spree/shared/error_messages', locals: { target: @image } + = form_for [:admin, @product, @image], url: admin_product_images_path(@product, @image, @url_filters), html: { multipart: true } do |f| %fieldset %legend{ align: "center" }= t('spree.new_image') From 3d6654a4b52e2828b9149a9722c54c8cc2406043 Mon Sep 17 00:00:00 2001 From: Adrien Chauve Date: Mon, 11 Oct 2021 12:17:56 +0200 Subject: [PATCH 2/4] Cleanup errors on wrong image format -> Remove all duplicates and use the same error message than in product creation. --- app/models/spree/image.rb | 9 +++++++-- config/locales/en.yml | 1 + 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/app/models/spree/image.rb b/app/models/spree/image.rb index 7a3964a7a7..db664b0006 100644 --- a/app/models/spree/image.rb +++ b/app/models/spree/image.rb @@ -56,8 +56,13 @@ module Spree def no_attachment_errors return if attachment.errors.empty? - errors.add :attachment, - "Paperclip returned errors for file '#{attachment_file_name}' - check ImageMagick installation or image source file." + if errors.all? {|e| e.type == "Paperclip::Errors::NotIdentifiedByImageMagickError"} + attachment.errors.clear + errors.add :base, I18n.t('spree.admin.products.image_upload_error') + else + errors.add :attachment, + I18n.t('spree.admin.products.paperclip_image_error', attachment_file_name: attachment_file_name) + end false end diff --git a/config/locales/en.yml b/config/locales/en.yml index 76398dceef..9c806d9c7f 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -3726,6 +3726,7 @@ See the %{link} to find out more about %{sitename}'s features and to start using no_payment_via_admin_backend: Paypal payments cannot be captured in the Backoffice products: image_upload_error: "The product image was not recognised. Please upload an image in PNG or JPG format." + paperclip_image_error: "Paperclip returned errors for file '%{attachment_file_name}' - check ImageMagick installation or image source file." new: title: "New Product" new_product: "New Product" From c818ec5508a865fb3d36ae6023166c4709d5114f Mon Sep 17 00:00:00 2001 From: Adrien Chauve Date: Mon, 11 Oct 2021 12:19:07 +0200 Subject: [PATCH 3/4] Fix image creation button title and icon --- app/views/spree/admin/images/new.html.haml | 2 +- spec/features/admin/products_spec.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/views/spree/admin/images/new.html.haml b/app/views/spree/admin/images/new.html.haml index 78ebc0ea19..1b120d3f22 100644 --- a/app/views/spree/admin/images/new.html.haml +++ b/app/views/spree/admin/images/new.html.haml @@ -5,7 +5,7 @@ %legend{ align: "center" }= t('spree.new_image') = render partial: 'form', locals: { f: f } .form-buttons.filter-actions.actions - = button t('spree.actions.update'), 'icon-refresh' + = button t('actions.create'), 'icon-ok' %span.or= t('spree.or') = link_to_with_icon 'icon-remove', t('spree.actions.cancel'), admin_product_images_url(@product, @url_filters), id: 'cancel_link', class: 'button' diff --git a/spec/features/admin/products_spec.rb b/spec/features/admin/products_spec.rb index aacc0d3b0f..44e6031ef8 100644 --- a/spec/features/admin/products_spec.rb +++ b/spec/features/admin/products_spec.rb @@ -484,7 +484,7 @@ describe ' page.find('a#new_image_link').click attach_file('image_attachment', file_path) - click_button "Update" + click_button "Create" uri = URI.parse(current_url) expect("#{uri.path}?#{uri.query}").to eq spree.admin_product_images_path(product, filter) From e488f599e6012b79fe714726c7e99f54502040aa Mon Sep 17 00:00:00 2001 From: Adrien Chauve Date: Fri, 15 Oct 2021 10:44:47 +0200 Subject: [PATCH 4/4] Add new feature test to check error message on unsupported image format --- spec/features/admin/products_spec.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/spec/features/admin/products_spec.rb b/spec/features/admin/products_spec.rb index 44e6031ef8..83ec8afe8c 100644 --- a/spec/features/admin/products_spec.rb +++ b/spec/features/admin/products_spec.rb @@ -540,6 +540,23 @@ describe ' expect("#{uri.path}?#{uri.query}").to eq spree.admin_product_images_path(product, filter) end + it "checks error when creating product image with unsupported format", js: true do + unsupported_image_file_path = Rails.root + "README.md" + product = create(:simple_product, supplier: @supplier2) + + image = File.open(File.expand_path('../../../app/assets/images/logo-white.png', __dir__)) + Spree::Image.create(viewable_id: product.master.id, viewable_type: 'Spree::Variant', + alt: "position 1", attachment: image, position: 1) + + visit spree.admin_product_images_path(product) + page.find('a#new_image_link').click + attach_file('image_attachment', unsupported_image_file_path) + click_button "Create" + + expect(page).to have_text "The product image was not recognised." + expect(page).to have_text "Please upload an image in PNG or JPG format." + end + it "deleting product images", js: true do product = create(:simple_product, supplier: @supplier2) image = File.open(File.expand_path('../../../app/assets/images/logo-white.png', __dir__))