From c6e1f458cc93979e4b43d54cf99ceb4827047de8 Mon Sep 17 00:00:00 2001 From: Gaetan Riou Date: Fri, 7 Aug 2020 14:46:31 +1000 Subject: [PATCH] add product filter parameters on the various product properties pages, so that the bulk import product page filters can be preserved --- .../admin/product_properties_controller.rb | 14 +++++ .../_product_property_fields.html.haml | 2 +- .../admin/product_properties/index.html.haml | 7 ++- spec/features/admin/products_spec.rb | 59 +++++++++++++++++-- 4 files changed, 73 insertions(+), 9 deletions(-) diff --git a/app/controllers/spree/admin/product_properties_controller.rb b/app/controllers/spree/admin/product_properties_controller.rb index b236b7be7e..c376b15b51 100644 --- a/app/controllers/spree/admin/product_properties_controller.rb +++ b/app/controllers/spree/admin/product_properties_controller.rb @@ -5,6 +5,20 @@ module Spree before_action :find_properties before_action :setup_property, only: [:index] + def index + @url_filters = ::ProductFilters.new.extract(request.query_parameters) + end + + def destroy + @url_filters = ::ProductFilters.new.extract(request.query_parameters) + + if @object.destroy + flash[:success] = flash_message_for(@object, :successfully_removed) + end + # if destroy fails it won't show any errors to the user + redirect_to admin_product_product_properties_url(params[:product_id], @url_filters) + end + private def find_properties diff --git a/app/views/spree/admin/product_properties/_product_property_fields.html.haml b/app/views/spree/admin/product_properties/_product_property_fields.html.haml index d4362c4366..758225a756 100644 --- a/app/views/spree/admin/product_properties/_product_property_fields.html.haml +++ b/app/views/spree/admin/product_properties/_product_property_fields.html.haml @@ -11,4 +11,4 @@ = f.text_field :value, class: 'autocomplete' %td.actions - if f.object.persisted? - = link_to_delete f.object, no_text: true + = link_to_delete f.object, { url: admin_product_product_property_url(@product, f.object, @url_filters), no_text: true } diff --git a/app/views/spree/admin/product_properties/index.html.haml b/app/views/spree/admin/product_properties/index.html.haml index 87886cb37b..a595ceadc7 100644 --- a/app/views/spree/admin/product_properties/index.html.haml +++ b/app/views/spree/admin/product_properties/index.html.haml @@ -9,7 +9,7 @@ %li = link_to_add_fields t('.add_product_properties'), 'tbody#product_properties', class: 'icon-plus button' -= form_for @product, url: admin_product_url(@product), method: :put do |f| += form_for @product, url: admin_product_url(@product, @url_filters), method: :put do |f| %fieldset.no-border-top .add_product_properties = image_tag 'select2-spinner.gif', plugin: 'spree', style: 'display:none;', id: 'busy_indicator' @@ -46,7 +46,10 @@ %td.actions - = render partial: 'spree/admin/shared/edit_resource_links' + .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_product_properties_url(@product, @url_filters), id: 'cancel_link', class: 'button icon-remove' = hidden_field_tag 'clear_product_properties', 'true' diff --git a/spec/features/admin/products_spec.rb b/spec/features/admin/products_spec.rb index ca54f162e0..b6a3696fd3 100644 --- a/spec/features/admin/products_spec.rb +++ b/spec/features/admin/products_spec.rb @@ -162,7 +162,7 @@ feature ' # 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, producerFilter: 2) + expect("#{uri.path}?#{uri.query}").to eq spree.edit_admin_product_path(product, filter) # 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.to_query}")) @@ -217,13 +217,25 @@ feature ' expect(product.meta_keywords).to eq('Product Search Keywords') end + scenario "loading product properties page including url filters", js: true do + product = create(:simple_product, supplier: @supplier2) + visit spree.admin_product_product_properties_path(product, filter) + + 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_product_properties_path(product, filter) + + expected_cancel_link = Regexp.new(Regexp.escape(spree.admin_product_product_properties_path(product, filter))) + expect(page).to have_link(I18n.t(:cancel), href: expected_cancel_link) + end + scenario "deleting product properties", js: true do # Given a product with a property - p = create(:simple_product, supplier: @supplier2) - p.set_property('fooprop', 'fooval') + product = create(:simple_product, supplier: @supplier2) + product.set_property('fooprop', 'fooval') # When I navigate to the product properties page - visit spree.admin_product_product_properties_path(p) + visit spree.admin_product_product_properties_path(product) expect(page).to have_select2 'product_product_properties_attributes_0_property_name', selected: 'fooprop' expect(page).to have_field 'product_product_properties_attributes_0_value', with: 'fooval' @@ -236,7 +248,42 @@ feature ' # Then the property should have been deleted expect(page).not_to have_field 'product_product_properties_attributes_0_property_name', with: 'fooprop' expect(page).not_to have_field 'product_product_properties_attributes_0_value', with: 'fooval' - expect(p.reload.property('fooprop')).to be_nil + expect(product.reload.property('fooprop')).to be_nil + end + + scenario "deleting product properties including url filters", js: true do + # Given a product with a property + product = create(:simple_product, supplier: @supplier2) + product.set_property('fooprop', 'fooval') + + # When I navigate to the product properties page + visit spree.admin_product_product_properties_path(product, filter) + + # And I delete the property + accept_alert do + page.all('a.delete-resource').first.click + end + + uri = URI.parse(current_url) + expect("#{uri.path}?#{uri.query}").to eq spree.admin_product_product_properties_path(product, filter) + end + + scenario "adding product properties including url filters", js: true do + # Given a product + product = create(:simple_product, supplier: @supplier2) + product.set_property('fooprop', 'fooval') + + # When I navigate to the product properties page + visit spree.admin_product_product_properties_path(product, filter) + + # And I add a property + select 'fooprop', from: 'product_product_properties_attributes_0_property_name' + fill_in 'product_product_properties_attributes_0_value', with: 'fooval2' + + click_button 'Update' + + uri = URI.parse(current_url) + expect("#{uri.path}?#{uri.query}").to eq spree.edit_admin_product_path(product, filter) end scenario "loading new product image page", js: true do @@ -249,7 +296,7 @@ feature ' expect(page).to have_selector "#image_attachment" end - scenario "loading new procut image page including url filters", js: true do + scenario "loading new product image page including url filters", js: true do product = create(:simple_product, supplier: @supplier2) visit spree.admin_product_images_path(product, filter)