Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix searching deleted products by SKU #4164

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion core/app/models/spree/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def find_or_build_master
self.whitelisted_ransackable_attributes = %w[name slug]

def self.ransackable_scopes(_auth_object = nil)
%i(with_discarded with_variant_sku_cont)
%i(with_discarded with_variant_sku_cont with_all_variant_sku_cont with_kept_variant_sku_cont)
end

# @return [Boolean] true if there are any variants
Expand Down
24 changes: 21 additions & 3 deletions core/app/models/spree/product/scopes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,31 @@ def self.available(available_on = nil)
group("spree_products.id").joins(:taxons).where(Spree::Taxon.arel_table[:name].eq(name))
end

def self.with_variant_sku_cont(sku)
sku_match = "%#{sku}%"
def self.with_all_variant_sku_cont(sku)
variant_table = Spree::Variant.arel_table
subquery = Spree::Variant.with_discarded.where(
variant_table[:sku].matches("%#{sku}%").and(
variant_table[:product_id].eq(arel_table[:id])
)
)
where(subquery.arel.exists)
end

def self.with_kept_variant_sku_cont(sku)
variant_table = Spree::Variant.arel_table
subquery = Spree::Variant.where(variant_table[:sku].matches(sku_match).and(variant_table[:product_id].eq(arel_table[:id])))
subquery = Spree::Variant.where(
variant_table[:sku].matches("%#{sku}%").and(
variant_table[:product_id].eq(arel_table[:id])
)
)
where(subquery.arel.exists)
end

def self.with_variant_sku_cont(sku)
Spree::Deprecation.warn("use .with_kept_variant_sku_cont instead")
with_kept_variant_sku_cont(sku)
end

class << self
private

Expand Down
54 changes: 54 additions & 0 deletions core/spec/models/spree/product/scopes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,58 @@
end
end
end

describe ".with_all_variant_sku_cont" do
let!(:product) { create(:product, sku: sku) }
let(:sku) { "SEARCH-SKU-1" }

subject { Spree::Product.with_all_variant_sku_cont("SEARCH") }

it "returns the product" do
expect(subject).to contain_exactly(product)
end

context "when the variant has been discarded" do
before { product.master.discard }

it "returns the product" do
expect(subject).to contain_exactly(product)
end
end

context "when the SKU doesn't match" do
let(:sku) { "NON-MATCHING-SKU" }

it "does not include the product" do
expect(subject).to be_empty
end
end
end

describe ".with_kept_variant_sku_cont" do
let!(:product) { create(:product, sku: sku) }
let(:sku) { "SEARCH-SKU-1" }

subject { Spree::Product.with_kept_variant_sku_cont("SEARCH") }

it "returns the product" do
expect(subject).to contain_exactly(product)
end

context "when the variant has been discarded" do
before { product.master.discard }

it "does not include the product" do
expect(subject).to be_empty
end
end

context "when the SKU doesn't match" do
let(:sku) { "NON-MATCHING-SKU" }

it "does not include the product" do
expect(subject).to be_empty
end
end
end
end