From 482438819ceb12499fe75f8b11fe4c505b32816f Mon Sep 17 00:00:00 2001 From: Jared Norman Date: Tue, 25 Jun 2019 11:49:55 -0700 Subject: Make cache key a string --- CHANGELOG.md | 2 ++ lib/super_good/solidus_taxjar.rb | 2 +- spec/super_good/solidus_taxjar_spec.rb | 15 +++++++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ac142a7..32fb9d5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## master +- Made sure cache key is a string, instead of a giant nested hash/array structure that probably won't get interpreted by Rails well. Still not happy with the caching behaviour, but it's configurable. + ## v0.14.0 - Added `SuperGood::SolidusTaxJar.taxable_order_check` option which can be set to a proc that receives the order and will prevent actual tax calculation from occurring if it returns false. If your app has introduced a method like `Spree::Order#complimentary?`, you could avoid trying to compute taxes on complimentary orders by doing the following in an initializer: diff --git a/lib/super_good/solidus_taxjar.rb b/lib/super_good/solidus_taxjar.rb index af2c7e8..1016397 100644 --- a/lib/super_good/solidus_taxjar.rb +++ b/lib/super_good/solidus_taxjar.rb @@ -24,7 +24,7 @@ module SuperGood end self.cache_duration = 3.hours - self.cache_key = ->(order) { APIParams.order_params(order) } + self.cache_key = ->(order) { APIParams.order_params(order).to_json } self.discount_calculator = ::SuperGood::SolidusTaxJar::DiscountCalculator self.exception_handler = ->(e) { Rails.logger.error "An error occurred while fetching TaxJar tax rates - #{e}: #{e.message}" diff --git a/spec/super_good/solidus_taxjar_spec.rb b/spec/super_good/solidus_taxjar_spec.rb index efc38b0..2b04e7b 100644 --- a/spec/super_good/solidus_taxjar_spec.rb +++ b/spec/super_good/solidus_taxjar_spec.rb @@ -6,6 +6,21 @@ RSpec.describe SuperGood::SolidusTaxJar do end describe "configuration" do + describe ".cache_key" do + subject { described_class.cache_key.(order) } + + let(:order) { Spree::Order.new } + + it "returns the API params converted to JSON" do + allow(SuperGood::SolidusTaxJar::APIParams) + .to receive(:order_params) + .with(order) + .and_return({ some: "hash", with: "stuff", in: "it" }) + + expect(subject).to eq '{"some":"hash","with":"stuff","in":"it"}' + end + end + describe ".discount_calculator" do subject { described_class.discount_calculator } it { is_expected.to eq SuperGood::SolidusTaxJar::DiscountCalculator } -- cgit v1.2.3