From 1c75d6bebcc8be0e081538f0364801f9f36cc731 Mon Sep 17 00:00:00 2001 From: Sawy Date: Wed, 28 Aug 2024 13:00:05 +0300 Subject: [PATCH] CV2-5129: apply same search for Explainer --- app/models/team.rb | 6 +++++- test/models/team_2_test.rb | 12 +++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/app/models/team.rb b/app/models/team.rb index 13695de9d..bb96e63aa 100644 --- a/app/models/team.rb +++ b/app/models/team.rb @@ -495,7 +495,11 @@ def filtered_explainers(filters = {}) query = query.where(updated_at: Range.new(*format_times_search_range_filter(JSON.parse(filters[:updated_at]), nil))) unless filters[:updated_at].blank? # Filter by text - query = query.where('(title ILIKE ? OR url ILIKE ? OR description ILIKE ?)', *["%#{filters[:text]}%"]*3) if filters[:text].to_s.size > 2 + if filters[:text].to_s.size > 2 + tsquery = Team.sanitize_sql_array(["to_tsquery(?)", filters[:text].split(/\s+/).map(&:strip).join(' & ')]) # FIXME: May not work for all languages + tsvector = "to_tsvector('simple', coalesce(title, '') || ' ' || coalesce(url, '') || coalesce(description, ''))" + query = query.where(Arel.sql("#{tsvector} @@ #{tsquery}")) + end # Exclude the ones already applied to a target item target = ProjectMedia.find_by_id(filters[:target_id].to_i) diff --git a/test/models/team_2_test.rb b/test/models/team_2_test.rb index f9bceb24b..49e1ae56d 100644 --- a/test/models/team_2_test.rb +++ b/test/models/team_2_test.rb @@ -1528,9 +1528,10 @@ def setup assert_equal ['none', 'link_preview'], t.available_newsletter_header_types end - test "should search for fact-checks by keywords" do + test "should search for fact-checks and explainers by keywords" do Sidekiq::Testing.fake! t = create_team + # Fact-checks create_fact_check title: 'Some Other Test', claim_description: create_claim_description(project_media: create_project_media(team: t)) create_fact_check title: 'Bar Bravo Foo Test', claim_description: create_claim_description(project_media: create_project_media(team: t)) create_fact_check title: 'Foo Alpha Bar Test', claim_description: create_claim_description(project_media: create_project_media(team: t)) @@ -1539,5 +1540,14 @@ def setup assert_equal 2, t.filtered_fact_checks(text: 'Foo Bar').count assert_equal 1, t.filtered_fact_checks(text: 'Foo Bar Bravo').count assert_equal 1, t.filtered_fact_checks(text: 'Foo Bar Alpha').count + # Explainer + create_explainer title: 'Some Other Test', team: t + create_explainer title: 'Bar Bravo Foo Test', team: t + create_explainer title: 'Foo Alpha Bar Test', team: t + assert_equal 3, t.filtered_explainers.count + assert_equal 3, t.filtered_explainers(text: 'Test').count + assert_equal 2, t.filtered_explainers(text: 'Foo Bar').count + assert_equal 1, t.filtered_explainers(text: 'Foo Bar Bravo').count + assert_equal 1, t.filtered_explainers(text: 'Foo Bar Alpha').count end end