From 6e5c168d3bcc768cc13bcc9571888bab61eeff12 Mon Sep 17 00:00:00 2001 From: Gaetan Riou Date: Fri, 3 Jul 2020 17:06:26 +1000 Subject: [PATCH] add filter parameters to link leading back to bulk import product page and preserve filter parameters when updating product --- .../spree/admin/products_controller.rb | 29 +++++++++++++++++-- app/views/spree/admin/products/edit.html.haml | 9 ++++-- spec/features/admin/products_spec.rb | 18 ++++++++++++ 3 files changed, 51 insertions(+), 5 deletions(-) diff --git a/app/controllers/spree/admin/products_controller.rb b/app/controllers/spree/admin/products_controller.rb index 76f0066d86..e131e10e25 100644 --- a/app/controllers/spree/admin/products_controller.rb +++ b/app/controllers/spree/admin/products_controller.rb @@ -31,6 +31,19 @@ module Spree } } } + respond_override update: { html: { + success: lambda { + redirect_to edit_admin_product_url(@product, @url_filter) + }, + failure: lambda { + redirect_to edit_admin_product_url(@product, @url_filter) + } + } } + + PRODUCT_FILTER = [ + 'query', 'producerFilter', 'categoryFilter', 'sorting', 'importDateFilter' + ].freeze + def new @object.shipping_category = DefaultShippingCategory.find_or_create super @@ -56,9 +69,17 @@ module Spree @show_latest_import = params[:latest_import] || false end - def update - original_supplier_id = @product.supplier_id + def edit + filters = product_filters(params) + @url_filters = filters.empty? ? "" : "?#{filters.to_query}" + super + end + + def update + @url_filter = product_filters(request.query_parameters) + + original_supplier_id = @product.supplier_id delete_stock_params_and_set_after do super if original_supplier_id != @product.supplier_id @@ -258,6 +279,10 @@ module Spree def set_product_master_variant_price_to_zero @product.price = 0 if @product.price.nil? end + + def product_filters(params) + params.select { |k, _v| PRODUCT_FILTER.include?(k) } + end end end end diff --git a/app/views/spree/admin/products/edit.html.haml b/app/views/spree/admin/products/edit.html.haml index 95ef4c061c..8967233715 100644 --- a/app/views/spree/admin/products/edit.html.haml +++ b/app/views/spree/admin/products/edit.html.haml @@ -1,5 +1,5 @@ - content_for :page_actions do - %li= button_link_to t('admin.products.back_to_products_list'), admin_products_path, :icon => 'icon-arrow-left' + %li= button_link_to t('admin.products.back_to_products_list'), "#{admin_products_path}##{@url_filters}", :icon => 'icon-arrow-left' %li#new_product_link = button_link_to t(:new_product), new_object_url, { :icon => 'icon-plus', :id => 'admin_new_product' } @@ -8,7 +8,10 @@ = render :partial => 'spree/admin/shared/product_tabs', :locals => { :current => 'Product Details' } = render :partial => 'spree/shared/error_messages', :locals => { :target => @product } -= form_for [:admin, @product], :method => :put, :html => { :multipart => true } do |f| += form_for [:admin, @product], :url => "#{admin_product_path(@product)}#{@url_filters}", :method => :put, :html => { :multipart => true } do |f| %fieldset.no-border-top{'ng-app' => 'admin.products'} = render :partial => 'form', :locals => { :f => f } - = render :partial => 'spree/admin/shared/edit_resource_links' + .form-buttons.filter-actions.actions + = button t(:update), 'icon-refresh' + %span.or= t(:or) + = button_link_to t(:cancel), "#{collection_url}##{@url_filters}", icon: 'icon-remove' diff --git a/spec/features/admin/products_spec.rb b/spec/features/admin/products_spec.rb index dd9bfafd2c..78b0c2f9e2 100644 --- a/spec/features/admin/products_spec.rb +++ b/spec/features/admin/products_spec.rb @@ -151,6 +151,24 @@ feature ' expect(product.tax_category).to eq(tax_category) end + 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}" + + click_button 'Update' + expect(flash_message).to eq('Product "a product" has been successfully updated!') + + # Check the url still includes the filters + uri = URI.parse(current_url) + expect("#{uri.path}?#{uri.query}").to eq "#{spree.edit_admin_product_path(product)}?#{filter_query}" + + # Link back to the bulk product update page should include the filters + expect(page).to have_link(I18n.t('admin.products.back_to_products_list'), href: %r{admin\/products#\?#{filter_query}}) + expect(page).to have_link(I18n.t(:cancel), href: %r{admin\/products#\?#{filter_query}}) + end + scenario "editing product group buy options" do product = product = create(:simple_product, supplier: @supplier2)