Skip to content

Commit 8aa4bd1

Browse files
Merge pull request #11433 from alphagov/search_Agnostic_URL_or_Slug
[WHIT-3405] Allow document searching by path and URL
2 parents f66b8bf + b78b77c commit 8aa4bd1

2 files changed

Lines changed: 33 additions & 6 deletions

File tree

app/models/concerns/edition/scopes/searchable_by_title.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ module Edition::Scopes::SearchableByTitle
44
included do
55
scope :with_title_containing, lambda { |keywords|
66
like_clause = "%#{sanitize_sql_like(keywords)}%"
7+
slug_keyword_candidate = keywords.to_s.sub(%r{[?#].*\z}, "").split("/").last
78

8-
if keywords.match?(/\A[a-z0-9]+(-[a-z0-9]+)+\z/)
9-
where(slug: keywords)
9+
if slug_keyword_candidate.match?(/\A[a-z0-9]+(-[a-z0-9]+)+\z/)
10+
where(slug: slug_keyword_candidate)
1011
else
1112
in_default_locale
1213
.includes(:document)

test/unit/app/models/edition_test.rb

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -506,10 +506,36 @@ class EditionTest < ActiveSupport::TestCase
506506
assert_equal [edition_with_first_keyword], Edition.with_title_containing("klingons")
507507
end
508508

509-
test "should find editions with slug containing keyword" do
510-
edition_with_first_keyword = create(:edition, title: "klingons rule")
511-
_edition_without_first_keyword = create(:edition, title: "this document is about muppets")
512-
assert_equal [edition_with_first_keyword], Edition.with_title_containing("klingons-rule")
509+
test "should find editions by slug, when given the exact slug" do
510+
edition_with_slug = create(:edition, title: "New title", slug_override: "klingons-rule")
511+
_edition_without_match = create(:edition, title: "this document is about muppets")
512+
513+
assert_equal "klingons-rule", edition_with_slug.slug
514+
assert_equal [edition_with_slug], Edition.with_title_containing("klingons-rule")
515+
end
516+
517+
test "should find editions by slug, when given a full path ending in a slug" do
518+
edition_with_slug = create(:edition, title: "New title", slug_override: "klingons-rule")
519+
_edition_without_match = create(:edition, title: "this document is about muppets")
520+
521+
assert_equal "klingons-rule", edition_with_slug.slug
522+
assert_equal [edition_with_slug], Edition.with_title_containing("/government/news/klingons-rule")
523+
end
524+
525+
test "should find editions by slug, when given a full URL ending in a slug" do
526+
edition_with_slug = create(:edition, title: "New title", slug_override: "klingons-rule")
527+
_edition_without_match = create(:edition, title: "this document is about muppets")
528+
529+
assert_equal "klingons-rule", edition_with_slug.slug
530+
assert_equal [edition_with_slug], Edition.with_title_containing("https://www.gov.uk/government/news/klingons-rule")
531+
end
532+
533+
test "should find editions by slug, when given a full URL, including query string and fragment" do
534+
edition_with_slug = create(:edition, title: "New title", slug_override: "klingons-rule")
535+
_edition_without_match = create(:edition, title: "this document is about muppets")
536+
537+
assert_equal "klingons-rule", edition_with_slug.slug
538+
assert_equal [edition_with_slug], Edition.with_title_containing("https://www.gov.uk/government/news/klingons-rule?foo=bar#details")
513539
end
514540

515541
test "should find editions with title containing regular expression characters" do

0 commit comments

Comments
 (0)