Skip to content

Commit

Permalink
Merge pull request #2604 from jhawthorn/filter_taxon_preview_by_curre…
Browse files Browse the repository at this point in the history
…nt_pricing_options

Filter unpriced products in taxon_preview
  • Loading branch information
jhawthorn authored Mar 7, 2018
2 parents dbb0ee3 + 93a5441 commit cf29a55
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 13 deletions.
5 changes: 3 additions & 2 deletions core/app/helpers/spree/taxons_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ module TaxonsHelper
# that we can use configurations as well as make it easier for end users to override this determination. One idea is
# to show the most popular products for a particular taxon (that is an exercise left to the developer.)
def taxon_preview(taxon, max = 4)
products = taxon.active_products.select("DISTINCT (spree_products.id), spree_products.*, spree_products_taxons.position").limit(max)
price_scope = Spree::Price.where(current_pricing_options.search_arguments)
products = taxon.active_products.joins(:prices).merge(price_scope).select("DISTINCT (spree_products.id), spree_products.*, spree_products_taxons.position").limit(max)
if products.size < max
products_arel = Spree::Product.arel_table
taxon.descendants.each do |descendent_taxon|
to_get = max - products.length
products += descendent_taxon.active_products.select("DISTINCT (spree_products.id), spree_products.*, spree_products_taxons.position").where(products_arel[:id].not_in(products.map(&:id))).limit(to_get)
products += descendent_taxon.active_products.joins(:prices).merge(price_scope).select("DISTINCT (spree_products.id), spree_products.*, spree_products_taxons.position").where(products_arel[:id].not_in(products.map(&:id))).limit(to_get)
break if products.size >= max
end
end
Expand Down
44 changes: 33 additions & 11 deletions core/spec/helpers/taxons_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,39 @@
require 'rails_helper'

RSpec.describe Spree::TaxonsHelper, type: :helper do
# Regression test for https://github.com/spree/spree/issues/4382
it "#taxon_preview" do
taxon = create(:taxon)
child_taxon = create(:taxon, parent: taxon)
product_1 = create(:product)
product_2 = create(:product)
product_3 = create(:product)
taxon.products << product_1
taxon.products << product_2
child_taxon.products << product_3
let(:currency) { 'USD' }
let(:pricing_options) do
Spree::Config.pricing_options_class.new(currency: currency)
end
before do
allow(helper).to receive(:current_pricing_options) { pricing_options }
end

describe "#taxon_preview" do
let!(:taxon) { create(:taxon) }
let!(:child_taxon) { create(:taxon, parent: taxon) }
let!(:product_1) { create(:product) }
let!(:product_2) { create(:product) }
let!(:product_3) { create(:product) }

before do
taxon.products << product_1
taxon.products << product_2
child_taxon.products << product_3
taxon.reload
end

# Regression test for https://github.com/spree/spree/issues/4382
it "returns products" do
expect(helper.taxon_preview(taxon)).to eql([product_1, product_2, product_3])
end

context "with different currency" do
let(:currency) { 'CAD' }

expect(taxon_preview(taxon.reload)).to eql([product_1, product_2, product_3])
it "returns no products" do
expect(helper.taxon_preview(taxon)).to be_empty
end
end
end
end
21 changes: 21 additions & 0 deletions frontend/spec/features/taxons_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,25 @@
expect(tmp.sort!).to eq(["Ruby on Rails Bag", "Ruby on Rails Tote"])
end
end

# Regression test for https://github.com/solidusio/solidus/issues/2602
context "root taxon page" do
it "shows taxon previews" do
visit spree.nested_taxons_path(taxonomy.root)

expect(page).to have_css('ul.product-listing li', count: 2)
expect(page).to have_content("Superman T-Shirt", count: 2)
end

context "with prices in other currency" do
before { Spree::Price.update_all(currency: "CAD") }

it "shows no products" do
visit spree.nested_taxons_path(taxonomy.root)

expect(page).to have_css('ul.product-listing li', count: 0)
expect(page).to have_no_content("Superman T-Shirt")
end
end
end
end

0 comments on commit cf29a55

Please sign in to comment.