Skip to content

Commit

Permalink
Fixed ProductsHelper#cache_key_for_products (#10034)
Browse files Browse the repository at this point in the history
* Fixed ProductsHelper#cache_key_for_products

Use Product IDs rather than product ccount as it's way more reliable

* fix ruboccop
  • Loading branch information
damianlegawiec committed Mar 11, 2020
1 parent 3db9977 commit 20c2d42
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
8 changes: 6 additions & 2 deletions core/app/helpers/spree/products_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,13 @@ def line_item_description_text(description_text)
end

def cache_key_for_products(products = @products, additional_cache_key = nil)
count = products.count
ids = if products.is_a?(Array)
products.map(&:id)
else
products.ids
end.join('-')
max_updated_at = (products.maximum(:updated_at) || Date.today).to_s(:number)
products_cache_keys = "spree/products/all-#{params[:page]}-#{params[:sort_by]}-#{max_updated_at}-#{count}-#{@taxon&.id}"
products_cache_keys = "spree/products/#{ids}-#{params[:page]}-#{params[:sort_by]}-#{max_updated_at}-#{@taxon&.id}"
(common_product_cache_keys + [products_cache_keys] + [additional_cache_key]).compact.join('/')
end

Expand Down
32 changes: 15 additions & 17 deletions core/spec/helpers/products_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,13 +195,15 @@ module Spree
end

context '#cache_key_for_products' do
subject { helper.cache_key_for_products }
subject { helper.cache_key_for_products(products) }

let(:zone) { Spree::Zone.new }
let(:price_options) { { tax_zone: zone } }
let!(:products) { create_list(:product, 5) }
let(:product_ids) { products.map(&:id).join('-') }
let(:taxon) { create(:taxon) }

before do
@products = double('products collection')
allow(helper).to receive(:params).and_return(page: 10)
allow(helper).to receive(:current_price_options) { price_options }
end
Expand All @@ -210,49 +212,45 @@ module Spree
let(:updated_at) { Date.new(2011, 12, 13) }

before do
allow(@products).to receive(:count).and_return(5)
allow(@products).to receive(:maximum).with(:updated_at) { updated_at }
allow(products).to receive(:maximum).with(:updated_at) { updated_at }
end

it { is_expected.to eq('en/USD/spree/zones/new/spree/products/all-10--20111213-5-') }
it { is_expected.to eq("en/USD/spree/zones/new/spree/products/#{product_ids}-10--20111213-") }
end

context 'when there is no considered maximum updated date' do
let(:today) { Date.new(2013, 12, 11) }

before do
allow(@products).to receive(:count).and_return(1_234_567)
allow(@products).to receive(:maximum).with(:updated_at).and_return(nil)
allow(products).to receive(:maximum).with(:updated_at).and_return(nil)
allow(Date).to receive(:today) { today }
end

it { is_expected.to eq('en/USD/spree/zones/new/spree/products/all-10--20131211-1234567-') }
it { is_expected.to eq("en/USD/spree/zones/new/spree/products/#{product_ids}-10--20131211-") }
end

context 'with Taxon ID present' do
let(:updated_at) { Date.new(2011, 12, 13) }

before do
@taxon = create(:taxon)
allow(@products).to receive(:count).and_return(5)
allow(@products).to receive(:maximum).with(:updated_at) { updated_at }
@taxon = taxon
allow(products).to receive(:maximum).with(:updated_at) { updated_at }
end

it { is_expected.to eq("en/USD/spree/zones/new/spree/products/all-10--20111213-5-#{@taxon.id}") }
it { is_expected.to eq("en/USD/spree/zones/new/spree/products/#{product_ids}-10--20111213-#{taxon.id}") }
end

context 'with `additional_cache_key` passed' do
subject { helper.cache_key_for_products(@products, 'json-ld') }
subject { helper.cache_key_for_products(products, 'json-ld') }

let(:updated_at) { Date.new(2011, 12, 13) }

before do
@taxon = create(:taxon)
allow(@products).to receive(:count).and_return(5)
allow(@products).to receive(:maximum).with(:updated_at) { updated_at }
@taxon = taxon
allow(products).to receive(:maximum).with(:updated_at) { updated_at }
end

it { is_expected.to eq("en/USD/spree/zones/new/spree/products/all-10--20111213-5-#{@taxon.id}/json-ld") }
it { is_expected.to eq("en/USD/spree/zones/new/spree/products/#{product_ids}-10--20111213-#{taxon.id}/json-ld") }
end
end

Expand Down

0 comments on commit 20c2d42

Please sign in to comment.