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