Skip to content

Commit

Permalink
Fix searching discarded products in admin by SKU
Browse files Browse the repository at this point in the history
Now that we've got a new scope we can use that will correctly return
products with discarded variants, we've updated the admin interface to
use it.

This meant adding a new Backbone view to help control the two inputs we
now have on the page, one for each scope. It will hide and disable the
appropriate input based on the value of the "with deleted" checkbox.
  • Loading branch information
adammathys committed Sep 13, 2021
1 parent 4ae093a commit 1952e82
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 5 deletions.
1 change: 1 addition & 0 deletions backend/app/assets/javascripts/spree/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
//= require spree/backend/orders
//= require spree/backend/payments/edit
//= require spree/backend/payments/new
//= require spree/backend/products/index
//= require spree/backend/product_picker
//= require spree/backend/progress
//= require spree/backend/promotions
Expand Down
1 change: 1 addition & 0 deletions backend/app/assets/javascripts/spree/backend/namespaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ _.extend(window.Spree, {
Cart: {},
Zones: {},
Payment: {},
Product: {},
Promotions: {},
Stock: {},
Tables: {}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Spree.ready(function() {
if ($('[data-hook="admin_products_index_search"]').length) {
new Spree.Views.Product.Search({
el: $('[data-hook="admin_products_index_search"]')
})
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
//= require 'spree/backend/views/payment/new'
//= require 'spree/backend/views/payment/payment_row'
//= require 'spree/backend/views/payment/edit_credit_card'
//= require 'spree/backend/views/product/search'
//= require 'spree/backend/views/promotions/option_values_rule'
//= require 'spree/backend/views/tables/editable_table'
//= require 'spree/backend/views/tables/editable_table_row'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
Spree.Views.Product.Search = Backbone.View.extend({
initialize: function() {
this.render();
},

events: {
"change .js-with-discarded-input": "onChange"
},

onChange: function(e) {
const withDiscarded = $(e.target).is(":checked");

var keptInput = this.$el.find(".js-kept-variant-sku-input input");
var allInput = this.$el.find(".js-all-variant-sku-input input");

if (withDiscarded) {
allInput.val(keptInput.val());
keptInput.val("");
} else {
keptInput.val(allInput.val());
allInput.val("");
}

allInput.prop("disabled", !withDiscarded)
keptInput.prop("disabled", withDiscarded)

this.render();
},

render: function() {
var withDiscarded = this.$el.find(".js-with-discarded-input").is(":checked");

var keptContainer = this.$el.find(".js-kept-variant-sku-input");
var allContainer = this.$el.find(".js-all-variant-sku-input");

if (withDiscarded) {
keptContainer.hide();
allContainer.show();
} else {
keptContainer.show();
allContainer.hide();
}
},
});
12 changes: 9 additions & 3 deletions backend/app/views/spree/admin/products/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,21 @@

<div class="col-4">
<div class="field">
<%= f.label :with_variant_sku_cont, Spree::Variant.human_attribute_name(:sku) %>
<%= f.text_field :with_variant_sku_cont, size: 15 %>
<div class="js-kept-variant-sku-input">
<%= f.label :with_kept_variant_sku_cont, Spree::Variant.human_attribute_name(:sku) %>
<%= f.text_field :with_kept_variant_sku_cont, size: 15 %>
</div>
<div class="js-all-variant-sku-input">
<%= f.label :with_all_variant_sku_cont, Spree::Variant.human_attribute_name(:sku) %>
<%= f.text_field :with_all_variant_sku_cont, size: 15 %>
</div>
</div>
</div>

<div class="col-2">
<div class="field checkbox">
<label>
<%= f.check_box :with_discarded, { checked: params[:q][:with_discarded] == 'true' }, 'true', 'false' %>
<%= f.check_box :with_discarded, { checked: params[:q][:with_discarded] == 'true', class: 'js-with-discarded-input' }, 'true', 'false' %>
<%= t('spree.show_deleted') %>
</label>
</div>
Expand Down
30 changes: 28 additions & 2 deletions backend/spec/features/admin/products/products_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ def build_option_type_with_values(name, values)
end
end

context "searching products" do
it "should be able to search deleted products", js: true do
context "searching products", js: true do
it "should be able to search deleted products" do
create(:product, name: 'apache baseball cap', deleted_at: "2011-01-06 18:21:13")
create(:product, name: 'zomg shirt')

Expand Down Expand Up @@ -127,6 +127,32 @@ def build_option_type_with_values(name, values)
expect(page).not_to have_content("zomg shirt")
end

# Regression test for https://github.com/solidusio/solidus/issues/3912
it "should be able to search deleted products by their properties" do
create(:product, name: "First Product", sku: "A101").discard
create(:product, name: "Second Product", sku: "A102")
create(:product, name: "Third Product", sku: "B100")

click_nav "Products"
expect(page).not_to have_content("First Product")
expect(page).to have_content("Second Product")
expect(page).to have_content("Third Product")

fill_in "SKU", with: "A1"
check "Show Deleted"
click_button "Search"
expect(find('input[name="q[with_discarded]"]')).to be_checked
expect(page).to have_content("First Product")
expect(page).to have_content("Second Product")
expect(page).not_to have_content("Third Product")

uncheck "Show Deleted"
click_button "Search"
expect(page).not_to have_content("First Product")
expect(page).to have_content("Second Product")
expect(page).not_to have_content("Third Product")
end

# Regression test for https://github.com/solidusio/solidus/issues/2016
it "should be able to search and sort by price" do
product = create(:product, name: 'apache baseball cap', sku: "A001")
Expand Down

0 comments on commit 1952e82

Please sign in to comment.