From e54bcc5b7451bea76c9edc230fca0aa6bc297dff Mon Sep 17 00:00:00 2001 From: Laura Ghiorghisor Date: Wed, 12 Feb 2025 11:54:42 +0000 Subject: [PATCH] Add nil/blank checks --- app/models/document.rb | 7 ++++--- spec/models/document_spec.rb | 15 ++++++++++++++- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/app/models/document.rb b/app/models/document.rb index 1cbccf045..1aba40ca5 100644 --- a/app/models/document.rb +++ b/app/models/document.rb @@ -396,18 +396,19 @@ def self.apply_validations private def set_nested_facet_attributes(attrs) + byebug nested_facets = finder_schema.facets.select { |f| f.dig("specialist_publisher_properties", "nested_facet") == true } nested_facets.each do |nested_facet| 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