diff --git a/app/models/document.rb b/app/models/document.rb index 1cbccf045..bf99db0ef 100644 --- a/app/models/document.rb +++ b/app/models/document.rb @@ -401,13 +401,13 @@ def set_nested_facet_attributes(attrs) nested_facet_values = param_value(attrs, nested_facet["key"]) main_facet_value = [] sub_facet_value = [] - nested_facet_values.each do |main_and_sub_tuple| + nested_facet_values&.each do |main_and_sub_tuple| parsed_tuple = JSON.parse(main_and_sub_tuple) main_facet_value << parsed_tuple["main_facet_value"] sub_facet_value << parsed_tuple["sub_facet_value"] end - public_send(:"#{clean_key(nested_facet['key'].to_s)}=", main_facet_value.uniq) - public_send(:"#{clean_key(nested_facet['sub_facet_key']&.to_s)}=", sub_facet_value.uniq) + public_send(:"#{clean_key(nested_facet['key'].to_s)}=", main_facet_value.uniq) if main_facet_value.any? + public_send(:"#{clean_key(nested_facet['sub_facet_key']&.to_s)}=", sub_facet_value.uniq) if sub_facet_value.any? end end diff --git a/spec/models/document_spec.rb b/spec/models/document_spec.rb index 7aba7d7f9..881d1ca0e 100644 --- a/spec/models/document_spec.rb +++ b/spec/models/document_spec.rb @@ -526,6 +526,10 @@ def initialize(params = {}) describe "validations" do subject { Document.from_publishing_api(payload) } + before do + allow(FinderSchema).to receive(:load_from_schema) + end + context "when the document is a draft" do let(:payload) { FactoryBot.create(:document) } @@ -626,6 +630,8 @@ def initialize(params = {}) describe "when called on a class that mismatches the document_type" do it "raises a helpful error" do + allow(FinderSchema).to receive(:load_from_schema).and_return(finder_schema) + expect { CmaCase.find(document.content_id, document.locale) }.to raise_error(/wrong type/) @@ -636,6 +642,10 @@ def initialize(params = {}) describe "attachment methods" do let(:attachment) { Attachment.new } + before do + allow(FinderSchema).to receive(:load_from_schema) + end + describe "#attachments=" do it "creates an AttachmentCollection with the given attachments" do subject.attachments = [attachment] @@ -691,7 +701,10 @@ def initialize(params = {}) end describe "#set_temporary_update_type!" do - before { subject.publication_state = "published" } + before do + allow(FinderSchema).to receive(:load_from_schema) + subject.publication_state = "published" + end context "when the document has an update_type" do before do