From a6444e76a567b573a05edeacce61dc0ac456a6bf Mon Sep 17 00:00:00 2001 From: Gaetan Riou Date: Fri, 31 Jul 2020 16:13:38 +1000 Subject: [PATCH] add product filter parameters on the various product image pages, so that the bulk import product page filters can be preserved --- .../spree/admin/images_controller.rb | 52 ++++++++- app/views/spree/admin/images/edit.html.haml | 6 +- app/views/spree/admin/images/index.html.haml | 8 +- app/views/spree/admin/images/new.html.haml | 4 +- spec/features/admin/products_spec.rb | 109 ++++++++++++++++-- 5 files changed, 157 insertions(+), 22 deletions(-) diff --git a/app/controllers/spree/admin/images_controller.rb b/app/controllers/spree/admin/images_controller.rb index 8f47fe3576..c73e4e6826 100644 --- a/app/controllers/spree/admin/images_controller.rb +++ b/app/controllers/spree/admin/images_controller.rb @@ -8,9 +8,55 @@ module Spree before_action :load_data - create.before :set_viewable - update.before :set_viewable - destroy.before :destroy_before + def index + @url_filters = ::ProductFilters.new.extract(request.query_parameters) + end + + def new + @url_filters = ::ProductFilters.new.extract(request.query_parameters) + + render layout: !request.xhr? + end + + def create + @url_filters = ::ProductFilters.new.extract(params) + set_viewable + + @object.attributes = permitted_resource_params + if @object.save + flash[:success] = flash_message_for(@object, :successfully_created) + redirect_to admin_product_images_url(params[:product_id], @url_filters) + else + respond_with(@object) + end + end + + def edit + @url_filters = ::ProductFilters.new.extract(request.query_parameters) + end + + def update + @url_filters = ::ProductFilters.new.extract(params) + set_viewable + + if @object.update(permitted_resource_params) + flash[:success] = flash_message_for(@object, :successfully_updated) + redirect_to admin_product_images_url(params[:product_id], @url_filters) + else + respond_with(@object) + end + end + + def destroy + @url_filters = ::ProductFilters.new.extract(request.query_parameters) + destroy_before + + if @object.destroy + flash[:success] = flash_message_for(@object, :successfully_removed) + end + + redirect_to admin_product_images_url(params[:product_id], @url_filters) + end private diff --git a/app/views/spree/admin/images/edit.html.haml b/app/views/spree/admin/images/edit.html.haml index 9b691479b4..4a7910a223 100644 --- a/app/views/spree/admin/images/edit.html.haml +++ b/app/views/spree/admin/images/edit.html.haml @@ -3,9 +3,9 @@ = render partial: 'spree/shared/error_messages', locals: { target: @image } - content_for :page_actions do - %li= button_link_to t('spree.back_to_images_list'), admin_product_images_url(@product), icon: 'icon-arrow-left' + %li= button_link_to t('spree.back_to_images_list'), admin_product_images_url(@product, @url_filters), icon: 'icon-arrow-left' -= form_for [:admin, @product, @image], html: { multipart: true } do |f| += form_for [:admin, @product, @image], url: admin_product_image_path(@product, @image, @url_filters), html: { multipart: true } do |f| %fieldset %legend{align: "center"}= @image.attachment_file_name .field.alpha.three.columns.align-center @@ -18,4 +18,4 @@ .form-buttons.filter-actions.actions = button t('spree.actions.update'), 'icon-refresh' %span.or= t('spree.or') - = link_to t('spree.actions.cancel'), admin_product_images_url(@product), id: 'cancel_link', class: 'button icon-remove' + = link_to t('spree.actions.cancel'), admin_product_images_url(@product, @url_filters), id: 'cancel_link', class: 'button icon-remove' diff --git a/app/views/spree/admin/images/index.html.haml b/app/views/spree/admin/images/index.html.haml index 37cc53c70b..7f777c573a 100644 --- a/app/views/spree/admin/images/index.html.haml +++ b/app/views/spree/admin/images/index.html.haml @@ -2,7 +2,7 @@ = render partial: 'spree/admin/shared/product_tabs', locals: { current: 'Images'} - content_for :page_actions do - %li= link_to_with_icon('icon-plus', t('spree.new_image'), new_admin_product_image_url(@product), id: 'new_image_link', class: 'button') + %li= link_to_with_icon('icon-plus', t('spree.new_image'), new_admin_product_image_url(@product, @url_filters), id: 'new_image_link', class: 'button') #images @@ -11,7 +11,7 @@ = t('spree.no_images_found') \. - else - %table.index.sortable{ "data-sortable-link" => "#{update_positions_admin_product_images_url(@product)}" } + %table.index.sortable{ "data-sortable-link" => "#{update_positions_admin_product_images_url(@product, @url_filters)}" } %colgroup %col{ style: "width: 5%" }/ %col{ style: "width: 10%" }/ @@ -36,5 +36,5 @@ %td= options_text_for(image) %td= image.alt %td.actions - = link_to_with_icon 'icon-edit', t('spree.edit'), edit_admin_product_image_url(@product, image), no_text: true, data: { action: 'edit'} - = link_to_delete image, { url: admin_product_image_url(@product, image), no_text: true } + = link_to_with_icon 'icon-edit', t('spree.edit'), edit_admin_product_image_url(@product, image, @url_filters), no_text: true, data: { action: 'edit'} + = link_to_delete image, { url: admin_product_image_url(@product, image, @url_filters), no_text: true } diff --git a/app/views/spree/admin/images/new.html.haml b/app/views/spree/admin/images/new.html.haml index 4026cef6fb..651bddf3e1 100644 --- a/app/views/spree/admin/images/new.html.haml +++ b/app/views/spree/admin/images/new.html.haml @@ -1,10 +1,10 @@ -= form_for [:admin, @product, @image], html: { multipart: true } do |f| += 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') = render partial: 'form', locals: { f: f } .form-buttons.filter-actions.actions = button t('spree.actions.update'), 'icon-refresh' %span.or= t('spree.or') - = link_to_with_icon 'icon-remove', t('spree.actions.cancel'), admin_product_images_url(@product), id: 'cancel_link', class: 'button' + = link_to_with_icon 'icon-remove', t('spree.actions.cancel'), admin_product_images_url(@product, @url_filters), id: 'cancel_link', class: 'button' = javascript_include_tag 'admin/spree/images/new.js' diff --git a/spec/features/admin/products_spec.rb b/spec/features/admin/products_spec.rb index fd6be7a41d..ca54f162e0 100644 --- a/spec/features/admin/products_spec.rb +++ b/spec/features/admin/products_spec.rb @@ -94,6 +94,7 @@ feature ' context "as an enterprise user" do let!(:tax_category) { create(:tax_category) } + let(:filter) { { producerFilter: 2 } } before do @new_user = create(:user) @@ -154,8 +155,7 @@ feature ' scenario "editing a product comming from the bulk product update page with filter" do product = create(:simple_product, name: 'a product', supplier: @supplier2) - filter_query = "producerFilter=2" - visit "#{spree.edit_admin_product_path(product)}?#{filter_query}" + visit spree.edit_admin_product_path(product, filter) click_button 'Update' expect(flash_message).to eq('Product "a product" has been successfully updated!') @@ -165,26 +165,26 @@ feature ' expect("#{uri.path}?#{uri.query}").to eq spree.edit_admin_product_path(product, producerFilter: 2) # Link back to the bulk product update page should include the filters - expected_admin_product_url = Regexp.new(Regexp.escape("#{spree.admin_products_path}#?#{filter_query}")) + expected_admin_product_url = Regexp.new(Regexp.escape("#{spree.admin_products_path}#?#{filter.to_query}")) expect(page).to have_link(I18n.t('admin.products.back_to_products_list'), href: expected_admin_product_url) expect(page).to have_link(I18n.t(:cancel), href: expected_admin_product_url) - expected_product_url = Regexp.new(Regexp.escape(spree.edit_admin_product_path(product.permalink, producerFilter: 2))) + expected_product_url = Regexp.new(Regexp.escape(spree.edit_admin_product_path(product.permalink, filter))) expect(page).to have_link(I18n.t('admin.products.tabs.product_details'), href: expected_product_url) - expected_product_image_url = Regexp.new(Regexp.escape(spree.admin_product_images_path(product.permalink, producerFilter: 2))) + expected_product_image_url = Regexp.new(Regexp.escape(spree.admin_product_images_path(product.permalink, filter))) expect(page).to have_link(I18n.t('admin.products.tabs.images'), href: expected_product_image_url) - expected_product_variant_url = Regexp.new(Regexp.escape(spree.admin_product_variants_path(product.permalink, producerFilter: 2))) + expected_product_variant_url = Regexp.new(Regexp.escape(spree.admin_product_variants_path(product.permalink, filter))) expect(page).to have_link(I18n.t('admin.products.tabs.variants'), href: expected_product_variant_url) - expected_product_properties_url = Regexp.new(Regexp.escape(spree.admin_product_product_properties_path(product.permalink, producerFilter: 2))) + expected_product_properties_url = Regexp.new(Regexp.escape(spree.admin_product_product_properties_path(product.permalink, filter))) expect(page).to have_link(I18n.t('admin.products.tabs.product_properties'), href: expected_product_properties_url) - expected_product_group_buy_option_url = Regexp.new(Regexp.escape(spree.group_buy_options_admin_product_path(product.permalink, producerFilter: 2))) + expected_product_group_buy_option_url = Regexp.new(Regexp.escape(spree.group_buy_options_admin_product_path(product.permalink, filter))) expect(page).to have_link(I18n.t('admin.products.tabs.group_buy_options'), href: expected_product_group_buy_option_url) - expected_product_seo_url = Regexp.new(Regexp.escape(spree.seo_admin_product_path(product.permalink, producerFilter: 2))) + expected_product_seo_url = Regexp.new(Regexp.escape(spree.seo_admin_product_path(product.permalink, filter))) expect(page).to have_link(I18n.t(:search), href: expected_product_seo_url) end @@ -239,7 +239,7 @@ feature ' expect(p.reload.property('fooprop')).to be_nil end - scenario "loading new image page", js: true do + scenario "loading new product image page", js: true do product = create(:simple_product, supplier: @supplier2) visit spree.admin_product_images_path(product) @@ -249,6 +249,80 @@ feature ' expect(page).to have_selector "#image_attachment" end + scenario "loading new procut image page including url filters", js: true do + product = create(:simple_product, supplier: @supplier2) + + visit spree.admin_product_images_path(product, filter) + + page.find('a#new_image_link').click + + uri = URI.parse(current_url) + # we stay on the same url as the new image content is loaded via an ajax call + expect("#{uri.path}?#{uri.query}").to eq spree.admin_product_images_path(product, filter) + + expected_cancel_link = Regexp.new(Regexp.escape(spree.admin_product_images_path(product, filter))) + expect(page).to have_link(I18n.t(:cancel), href: expected_cancel_link) + end + + scenario "upload a new product image including url filters", js: true do + file_path = Rails.root + "spec/support/fixtures/thinking-cat.jpg" + product = create(:simple_product, supplier: @supplier2) + + visit spree.admin_product_images_path(product, filter) + + page.find('a#new_image_link').click + + attach_file('image_attachment', file_path) + click_button "Update" + + uri = URI.parse(current_url) + expect("#{uri.path}?#{uri.query}").to eq spree.admin_product_images_path(product, filter) + end + + scenario "loading image page including url filter", js: true do + product = create(:simple_product, supplier: @supplier2) + + visit spree.admin_product_images_path(product, filter) + + expected_new_image_link = Regexp.new(Regexp.escape(spree.new_admin_product_image_path(product, filter))) + expect(page).to have_link(I18n.t('spree.new_image'), href: expected_new_image_link) + end + + scenario "loading edit product image page including url filter", js: true do + product = create(:simple_product, supplier: @supplier2) + image = File.open(File.expand_path('../../../app/assets/images/logo-white.png', __dir__)) + image_object = 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, filter) + + page.find("a.icon-edit").click + + uri = URI.parse(current_url) + expect("#{uri.path}?#{uri.query}").to eq spree.edit_admin_product_image_path(product, image_object, filter) + + expected_cancel_link = Regexp.new(Regexp.escape(spree.admin_product_images_path(product, filter))) + expect(page).to have_link(I18n.t(:cancel), href: expected_cancel_link) + expect(page).to have_link("Back To Images List", href: expected_cancel_link) + end + + scenario "updating a product image including url filter", js: true do + product = create(:simple_product, supplier: @supplier2) + image = File.open(File.expand_path('../../../app/assets/images/logo-white.png', __dir__)) + image_object = Spree::Image.create(viewable_id: product.master.id, viewable_type: 'Spree::Variant', alt: "position 1", attachment: image, position: 1) + + file_path = Rails.root + "spec/support/fixtures/thinking-cat.jpg" + + visit spree.admin_product_images_path(product, filter) + + page.find("a.icon-edit").click + + attach_file('image_attachment', file_path) + click_button "Update" + + uri = URI.parse(current_url) + expect("#{uri.path}?#{uri.query}").to eq spree.admin_product_images_path(product, filter) + end + scenario "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__)) @@ -265,5 +339,20 @@ feature ' expect(page).to_not have_selector "table.index td img" expect(product.reload.images.count).to eq 0 end + + scenario "deleting product image including url filter", js: true do + 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, filter) + + accept_alert do + page.find('a.delete-resource').click + end + + uri = URI.parse(current_url) + expect("#{uri.path}?#{uri.query}").to eq spree.admin_product_images_path(product, filter) + end end end