Refresh cache when inventory setting product_selection_from_inventory_only is changed

This commit is contained in:
Rohan Mitchell
2016-03-09 13:29:33 +11:00
parent 27d7b3026b
commit 6f29a8b642
5 changed files with 97 additions and 0 deletions

View File

@@ -0,0 +1,31 @@
require 'open_food_network/products_cache'
module Spree
Preference.class_eval do
after_save :refresh_products_cache
# When the setting preferred_product_selection_from_inventory_only has changed, we want to
# refresh all active exchanges for this enterprise.
def refresh_products_cache
if product_selection_from_inventory_only_changed?
OpenFoodNetwork::ProductsCache.distributor_changed(enterprise)
end
end
private
def product_selection_from_inventory_only_changed?
key =~ product_selection_from_inventory_only_regex
end
def enterprise
enterprise_id = key.match(product_selection_from_inventory_only_regex)[1]
enterprise = Enterprise.find enterprise_id
end
def product_selection_from_inventory_only_regex
/^enterprise\/product_selection_from_inventory_only\/(\d+)$/
end
end
end

View File

@@ -88,6 +88,13 @@ module OpenFoodNetwork
end
def self.distributor_changed(enterprise)
Exchange.cachable.where(receiver_id: enterprise).each do |exchange|
refresh_cache exchange.receiver, exchange.order_cycle
end
end
private
def self.exchanges_featuring_variants(variants, distributor: nil)

View File

@@ -251,6 +251,32 @@ feature %q{
end
describe "inventory settings", js: true do
let!(:enterprise) { create(:distributor_enterprise) }
let!(:product) { create(:simple_product) }
let!(:order_cycle) { create(:simple_order_cycle, distributors: [enterprise], variants: [product.variants.first]) }
before do
Delayed::Job.destroy_all
quick_login_as_admin
end
it "refreshes the cache when I change what products appear on my shopfront" do
# Given a product that's not in my inventory, but is in an active order cycle
# When I change which products appear on the shopfront
visit edit_admin_enterprise_path(enterprise)
within(".side_menu") { click_link 'Inventory Settings' }
choose 'enterprise_preferred_product_selection_from_inventory_only_1'
# Then a job should have been enqueued to refresh the cache
expect do
click_button 'Update'
end.to enqueue_job RefreshProductsCacheJob, distributor_id: enterprise.id, order_cycle_id: order_cycle.id
end
end
context "as an Enterprise user", js: true do
let(:supplier1) { create(:supplier_enterprise, name: 'First Supplier') }
let(:supplier2) { create(:supplier_enterprise, name: 'Another Supplier') }

View File

@@ -376,6 +376,16 @@ module OpenFoodNetwork
end
end
describe "when a distributor enterprise is changed" do
let(:d) { create(:distributor_enterprise) }
let(:oc) { create(:open_order_cycle, distributors: [d]) }
it "updates each distribution the enterprise is active in" do
expect(ProductsCache).to receive(:refresh_cache).with(d, oc)
ProductsCache.distributor_changed d
end
end
describe "refreshing the cache" do
let(:distributor) { double(:distributor) }
let(:order_cycle) { double(:order_cycle) }

View File

@@ -0,0 +1,23 @@
require 'spec_helper'
module Spree
describe Preference do
describe "refreshing the products cache" do
it "reports when product_selection_from_inventory_only has changed" do
p = Preference.new(key: 'enterprise/product_selection_from_inventory_only/123')
expect(p.send(:product_selection_from_inventory_only_changed?)).to be_true
end
it "reports when product_selection_from_inventory_only has not changed" do
p = Preference.new(key: 'enterprise/shopfront_message/123')
expect(p.send(:product_selection_from_inventory_only_changed?)).to be_false
end
it "looks up the referenced enterprise" do
e = create(:distributor_enterprise)
p = Preference.new(key: "enterprise/product_selection_from_inventory_only/#{e.id}")
expect(p.send(:enterprise)).to eql e
end
end
end
end