From 1b50a2e5f93bf09d91bb99ed5a583f7ff1b9799d Mon Sep 17 00:00:00 2001 From: Justin Coyne Date: Fri, 10 Mar 2023 11:35:37 -0600 Subject: [PATCH] Poision the exhibit cache value when a document is updated --- app/models/spotlight/solr_document_sidecar.rb | 3 ++- spec/models/spotlight/resource_spec.rb | 4 ++-- spec/models/spotlight/solr_document_sidecar_spec.rb | 13 ++++++++++++- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/app/models/spotlight/solr_document_sidecar.rb b/app/models/spotlight/solr_document_sidecar.rb index 9fadcdbcb..3185a01f4 100644 --- a/app/models/spotlight/solr_document_sidecar.rb +++ b/app/models/spotlight/solr_document_sidecar.rb @@ -8,7 +8,8 @@ class SolrDocumentSidecar < ActiveRecord::Base acts_as_taggable - belongs_to :exhibit, optional: false + # The "touch: true" ensures the exhibit's cache is invalidated when the document is updated + belongs_to :exhibit, optional: false, touch: true belongs_to :resource, optional: true belongs_to :document, optional: false, polymorphic: true diff --git a/spec/models/spotlight/resource_spec.rb b/spec/models/spotlight/resource_spec.rb index b29ec9196..bd9bced19 100644 --- a/spec/models/spotlight/resource_spec.rb +++ b/spec/models/spotlight/resource_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -describe Spotlight::Resource, type: :model do +RSpec.describe Spotlight::Resource, type: :model do subject(:resource) { described_class.create(id: 123, exhibit: exhibit) } let(:exhibit) { FactoryBot.create(:exhibit) } @@ -113,7 +113,7 @@ indexed_document - expect(exhibit).to have_received(:touch) + expect(exhibit).to have_received(:touch).twice end context 'with touch: false' do diff --git a/spec/models/spotlight/solr_document_sidecar_spec.rb b/spec/models/spotlight/solr_document_sidecar_spec.rb index 5be5f9df0..043e0c33d 100644 --- a/spec/models/spotlight/solr_document_sidecar_spec.rb +++ b/spec/models/spotlight/solr_document_sidecar_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -describe Spotlight::SolrDocumentSidecar, type: :model do +RSpec.describe Spotlight::SolrDocumentSidecar, type: :model do let(:exhibit) { FactoryBot.create(:exhibit) } before do @@ -55,4 +55,15 @@ its(:to_solr) { is_expected.to include 'a_hash_field' => ['b'] } end end + + describe '#update' do + before do + subject.save + end + + it 'poisions the exhibit cache' do + expect { subject.update(data: { 'a_tesim' => 1, 'b_tesim' => 2, 'c_tesim' => 3 }) } + .to(change { subject.exhibit.updated_at }) + end + end end