From 4fbbba2c988c57d9d13e5789c7b224648b00c846 Mon Sep 17 00:00:00 2001 From: Harriet H-W Date: Fri, 25 Oct 2024 12:26:44 +0100 Subject: [PATCH] add SearchByLeadOrganisation to Content Block Documents --- .../content_block/document.rb | 1 + .../scopes/searchable_by_lead_organisation.rb | 12 +++++++++++ .../searchable_by_lead_organisation_test.rb | 20 +++++++++++++++++++ 3 files changed, 33 insertions(+) create mode 100644 lib/engines/content_block_manager/app/models/content_block_manager/content_block/document/scopes/searchable_by_lead_organisation.rb create mode 100644 lib/engines/content_block_manager/test/unit/app/models/content_block_edition/document/scopes/searchable_by_lead_organisation_test.rb diff --git a/lib/engines/content_block_manager/app/models/content_block_manager/content_block/document.rb b/lib/engines/content_block_manager/app/models/content_block_manager/content_block/document.rb index afe2424b5d4..dd877073bb0 100644 --- a/lib/engines/content_block_manager/app/models/content_block_manager/content_block/document.rb +++ b/lib/engines/content_block_manager/app/models/content_block_manager/content_block/document.rb @@ -2,6 +2,7 @@ module ContentBlockManager module ContentBlock class Document < ApplicationRecord include Scopes::SearchableByKeyword + include Scopes::SearchableByLeadOrganisation extend FriendlyId friendly_id :title, use: :slugged, slug_column: :content_id_alias, routes: :default diff --git a/lib/engines/content_block_manager/app/models/content_block_manager/content_block/document/scopes/searchable_by_lead_organisation.rb b/lib/engines/content_block_manager/app/models/content_block_manager/content_block/document/scopes/searchable_by_lead_organisation.rb new file mode 100644 index 00000000000..6bc1cb1897d --- /dev/null +++ b/lib/engines/content_block_manager/app/models/content_block_manager/content_block/document/scopes/searchable_by_lead_organisation.rb @@ -0,0 +1,12 @@ +module ContentBlockManager + module ContentBlock::Document::Scopes::SearchableByLeadOrganisation + extend ActiveSupport::Concern + + included do + scope :with_lead_organisation, + lambda { |id| + joins(latest_edition: :edition_organisation).where("content_block_edition_organisations.organisation_id = :id", id:) + } + end + end +end diff --git a/lib/engines/content_block_manager/test/unit/app/models/content_block_edition/document/scopes/searchable_by_lead_organisation_test.rb b/lib/engines/content_block_manager/test/unit/app/models/content_block_edition/document/scopes/searchable_by_lead_organisation_test.rb new file mode 100644 index 00000000000..3950b086423 --- /dev/null +++ b/lib/engines/content_block_manager/test/unit/app/models/content_block_edition/document/scopes/searchable_by_lead_organisation_test.rb @@ -0,0 +1,20 @@ +require "test_helper" + +class ContentBlockManager::SearchableByLeadOrganisationTest < ActiveSupport::TestCase + extend Minitest::Spec::DSL + + describe ".with_lead_organisation" do + test "finds documents with lead organisation on latest edition" do + matching_organisation = create(:organisation, id: "1234") + document_with_org = create(:content_block_document, :email_address) + _edition_with_org = create(:content_block_edition, + :email_address, + document: document_with_org, + organisation: matching_organisation) + document_without_org = create(:content_block_document, :email_address) + _edition_without_org = create(:content_block_edition, :email_address, document: document_without_org) + _document_without_latest_edition = create(:content_block_document, :email_address) + assert_equal [document_with_org], ContentBlockManager::ContentBlock::Document.with_lead_organisation("1234") + end + end +end