Skip to content

Commit

Permalink
search by keyword in details and title of Content Block
Browse files Browse the repository at this point in the history
This now matches keywords agains the user
generated free text on a block. Select options
will be added for Organisation, date etc.

If any other free text options were added (such as
internal instructions, description) we'd need to
add them to this keyword match here too.
  • Loading branch information
Harriethw committed Oct 25, 2024
1 parent 2c0bac2 commit ecd39ae
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def new_document_options_redirect
private

def params_filters
params.slice(:title)
params.slice(:keyword)
.permit!
.to_h
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module ContentBlockManager
module ContentBlock
class Document < ApplicationRecord
include Scopes::SearchableByTitle
include Scopes::SearchableByKeyword

extend FriendlyId
friendly_id :title, use: :slugged, slug_column: :content_id_alias, routes: :default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def initialize(filters = {})
def documents
documents = ContentBlock::Document
documents = documents.live
documents = documents.with_title(@filters[:title]) if @filters[:title].present?
documents = documents.with_keyword(@filters[:keyword]) if @filters[:keyword].present?
documents
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module ContentBlockManager
module ContentBlock::Document::Scopes::SearchableByKeyword
extend ActiveSupport::Concern

included do
scope :with_keyword,
lambda { |keywords|
split_keywords = keywords.split
pattern = split_keywords.map { |k|
escaped_word = Regexp.escape(k)
"(?=.*#{escaped_word})"
}.join
joins(:latest_edition)
.where("content_block_documents.title REGEXP :pattern OR content_block_editions.details REGEXP :pattern", pattern:)
}
end
end
end

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<%= form_with url: content_block_manager.content_block_manager_content_block_documents_path, method: :get do %>
<%= render "govuk_publishing_components/components/input", {
label: {
text: "Title",
text: "Keyword",
bold: true,
},
hint: 'For example, "driving standards"',
name: "title",
id: "title_filter",
value: [email protected]? && @filters[:title],
name: "keyword",
id: "keyword_filter",
value: [email protected]? && @filters[:keyword],
} %>
<%= render "govuk_publishing_components/components/button", {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,24 @@ Feature: Search for a content object
And a schema "email_address" exists with the following fields:
| email_address |
And an email address content block has been created
And an email address content block has been created with the title "example search title"
And an email address content block has been created with the following email address and title:
| title | "example search title" |
| email_address | "ministry@justice.com" |

Scenario: GDS Editor searches for a content object by keyword in title
When I visit the Content Block Manager home page
Then I should see the details for all documents
And "2" content blocks are returned
When I enter the title "example search"
When I enter the keyword "example search"
And I click to view results
Then I should see the content block with title "example search title" returned
And "1" content blocks are returned

Scenario: GDS Editor searches for a content object by keyword in details
When I visit the Content Block Manager home page
Then I should see the details for all documents
And "2" content blocks are returned
When I enter the keyword "ministry justice"
And I click to view results
Then I should see the content block with title "example search title" returned
And "1" content blocks are returned
Original file line number Diff line number Diff line change
Expand Up @@ -187,16 +187,17 @@
@content_blocks.push(@content_block)
end

Given("an email address content block has been created with the title {string}") do |title|
Given("an email address content block has been created with the following email address and title:") do |table|
fields = table.rows_hash
@content_blocks ||= []
@email_address = "[email protected]"
organisation = create(:organisation)
document = create(:content_block_document, :email_address, title:)
document = create(:content_block_document, :email_address, title: fields[:title])
@content_block = create(
:content_block_edition,
:email_address,
document:,
details: { email_address: @email_address },
details: { email_address: fields[:email_address] },
creator: @user,
organisation:,
)
Expand Down Expand Up @@ -476,8 +477,8 @@ def should_show_edit_form_for_email_address_content_block(document_title, email_
fill_in_date_and_time_field(past_date)
end

When("I enter the title {string}") do |title|
fill_in "Title", with: title
When("I enter the keyword {string}") do |keyword|
fill_in "Keyword", with: keyword
end

Then("the edition should have been scheduled successfully") do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ class ContentBlockManager::DocumentFilterTest < ActiveSupport::TestCase
it "returns live documents" do
document_scope_mock = mock
ContentBlockManager::ContentBlock::Document.expects(:live).returns(document_scope_mock)
document_scope_mock.expects(:with_title).never
document_scope_mock.expects(:with_keyword).never

ContentBlockManager::ContentBlock::Document::DocumentFilter.new({}).documents
end
end

describe "when a title filter is given" do
it "returns live documents with keyword in title" do
describe "when a keyword filter is given" do
it "returns live documents with keyword" do
document_scope_mock = mock
ContentBlockManager::ContentBlock::Document.expects(:live).returns(document_scope_mock)
document_scope_mock.expects(:with_title).returns([])
ContentBlockManager::ContentBlock::Document::DocumentFilter.new({ title: "ministry of example" }).documents
document_scope_mock.expects(:with_keyword).with("ministry of example").returns([])
ContentBlockManager::ContentBlock::Document::DocumentFilter.new({ keyword: "ministry of example" }).documents
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
require "test_helper"

class ContentBlockManager::SearchableByKeywordTest < ActiveSupport::TestCase
extend Minitest::Spec::DSL

describe ".with_keyword" do
test "should find documents with title containing keyword" do
document_with_first_keyword = create(:content_block_document, :email_address, title: "klingons and such")
_edition_with_first_keyword = create(:content_block_edition,
:email_address,
document: document_with_first_keyword,
details: { "email_address" => "[email protected]" })
document_without_first_keyword = create(:content_block_document, :email_address, title: "this document is about muppets")
_edition_without_first_keyword = create(:content_block_edition, :email_address, document: document_without_first_keyword)
assert_equal [document_with_first_keyword], ContentBlockManager::ContentBlock::Document.with_keyword("klingons")
end

test "should find documents with title containing keywords not in order" do
document_with_first_keyword = create(:content_block_document, :email_address, title: "klingons and such")
_edition_with_first_keyword = create(:content_block_edition,
:email_address,
document: document_with_first_keyword,
details: { "email_address" => "[email protected]" })
_document_without_first_keyword = create(:content_block_document, :email_address, title: "muppets and such")
assert_equal [document_with_first_keyword], ContentBlockManager::ContentBlock::Document.with_keyword("such klingons")
end

test "should find documents with latest edition's details containing keyword" do
document_with_first_keyword = create(:content_block_document, :email_address, title: "example title")
_edition_with_first_keyword = create(:content_block_edition,
document: document_with_first_keyword,
details: { "foo" => "Foo text", "bar" => "Bar text" })
document_without_first_keyword = create(:content_block_document, :email_address, title: "this document is about muppets")
_edition_without_first_keyword = create(:content_block_edition,
document: document_without_first_keyword,
details: { "something" => "something" })
assert_equal [document_with_first_keyword], ContentBlockManager::ContentBlock::Document.with_keyword("foo bar")
end

test "should find documents with details or title containing keyword" do
document_with_keyword_in_details = create(:content_block_document, :email_address, title: "example title")
_edition_with_keyword = create(:content_block_edition,
document: document_with_keyword_in_details,
details: { "foo" => "Foo text", "bar" => "Bar text" })
document_with_keyword_in_title = create(:content_block_document, :email_address, title: "this document is about bar foo")
_edition_without_keyword = create(:content_block_edition,
document: document_with_keyword_in_title,
details: { "something" => "something" })
assert_equal [document_with_keyword_in_details, document_with_keyword_in_title], ContentBlockManager::ContentBlock::Document.with_keyword("foo bar")
end
end
end

This file was deleted.

0 comments on commit ecd39ae

Please sign in to comment.