From aa75d03c5a952ab3219ce9d7486ddf496dc02abd Mon Sep 17 00:00:00 2001 From: Harriet H-W Date: Fri, 25 Oct 2024 12:26:19 +0100 Subject: [PATCH] add lead_organisation filter to Content Block Document Filter --- .../content_block/document/document_filter.rb | 1 + .../document/document_filter_test.rb | 19 +++++++++++++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/lib/engines/content_block_manager/app/models/content_block_manager/content_block/document/document_filter.rb b/lib/engines/content_block_manager/app/models/content_block_manager/content_block/document/document_filter.rb index dcdef7d3af0..ba30dd275e9 100644 --- a/lib/engines/content_block_manager/app/models/content_block_manager/content_block/document/document_filter.rb +++ b/lib/engines/content_block_manager/app/models/content_block_manager/content_block/document/document_filter.rb @@ -9,6 +9,7 @@ def documents documents = documents.live documents = documents.with_keyword(@filters[:keyword]) if @filters[:keyword].present? documents = documents.where(block_type: @filters[:block_type]) if @filters[:block_type].present? + documents = documents.with_lead_organisation(@filters[:lead_organisation]) if @filters[:lead_organisation].present? documents end end diff --git a/lib/engines/content_block_manager/test/unit/app/models/content_block_edition/document/document_filter_test.rb b/lib/engines/content_block_manager/test/unit/app/models/content_block_edition/document/document_filter_test.rb index 01261d8602f..b0f02eace43 100644 --- a/lib/engines/content_block_manager/test/unit/app/models/content_block_edition/document/document_filter_test.rb +++ b/lib/engines/content_block_manager/test/unit/app/models/content_block_edition/document/document_filter_test.rb @@ -10,6 +10,7 @@ class ContentBlockManager::DocumentFilterTest < ActiveSupport::TestCase ContentBlockManager::ContentBlock::Document.expects(:live).returns(document_scope_mock) document_scope_mock.expects(:with_keyword).never document_scope_mock.expects(:where).never + document_scope_mock.expects(:with_lead_organisation).never ContentBlockManager::ContentBlock::Document::DocumentFilter.new({}).documents end @@ -33,14 +34,24 @@ class ContentBlockManager::DocumentFilterTest < ActiveSupport::TestCase end end - describe "when both block types and keyword is given" do - it "returns live documents of the type and keyword given" do + describe "when a lead organisation id is given" do + it "returns live documents with lead org given" do + document_scope_mock = mock + ContentBlockManager::ContentBlock::Document.expects(:live).returns(document_scope_mock) + document_scope_mock.expects(:with_lead_organisation).with("123").returns([]) + ContentBlockManager::ContentBlock::Document::DocumentFilter.new({ lead_organisation: "123" }).documents + end + end + + describe "when block types, keyword and organisation is given" do + it "returns live documents with the filters given" do document_scope_mock = mock ContentBlockManager::ContentBlock::Document.expects(:live).returns(document_scope_mock) document_scope_mock.expects(:with_keyword).with("ministry of example").returns(document_scope_mock) - document_scope_mock.expects(:where).with(block_type: %w[email_address]).returns([]) + document_scope_mock.expects(:where).with(block_type: %w[email_address]).returns(document_scope_mock) + document_scope_mock.expects(:with_lead_organisation).with("123").returns([]) ContentBlockManager::ContentBlock::Document::DocumentFilter.new( - { block_type: %w[email_address], keyword: "ministry of example" }, + { block_type: %w[email_address], keyword: "ministry of example", lead_organisation: "123" }, ).documents end end