From 902189ecb988c0e1214a03386dcf693bfdbb51c4 Mon Sep 17 00:00:00 2001 From: Sawy Date: Wed, 28 Aug 2024 14:05:01 +0300 Subject: [PATCH] CV2-5129: fix CC --- app/models/team.rb | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/app/models/team.rb b/app/models/team.rb index 0768a8099..a3d8572e5 100644 --- a/app/models/team.rb +++ b/app/models/team.rb @@ -495,11 +495,7 @@ 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 - if filters[:text].to_s.size > 2 - tsquery = Team.sanitize_sql_array(["websearch_to_tsquery(?)", filters[:text]]) # 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 + query = self.filter_by_keywords(query, filters, 'Explainer') if filters[:text].to_s.size > 2 # Exclude the ones already applied to a target item target = ProjectMedia.find_by_id(filters[:target_id].to_i) @@ -539,11 +535,7 @@ def filtered_fact_checks(filters = {}) query = query.where('fact_checks.report_status' => filters[:report_status].to_a.map(&:to_s)) unless filters[:report_status].blank? # Filter by text - if filters[:text].to_s.size > 2 - tsquery = Team.sanitize_sql_array(["websearch_to_tsquery(?)", filters[:text]]) # FIXME: May not work for all languages - tsvector = "to_tsvector('simple', coalesce(title, '') || ' ' || coalesce(summary, '') || coalesce(url, ''))" - query = query.where(Arel.sql("#{tsvector} @@ #{tsquery}")) - end + query = self.filter_by_keywords(query, filters) if filters[:text].to_s.size > 2 # Exclude the ones already applied to a target item target = ProjectMedia.find_by_id(filters[:target_id].to_i) @@ -552,6 +544,16 @@ def filtered_fact_checks(filters = {}) query end + def filter_by_keywords(query, filters, type = 'FactCheck') + tsquery = Team.sanitize_sql_array(["websearch_to_tsquery(?)", filters[:text]]) # FIXME: May not work for all languages + if type == 'FactCheck' + tsvector = "to_tsvector('simple', coalesce(title, '') || ' ' || coalesce(summary, '') || coalesce(url, ''))" + else + tsvector = "to_tsvector('simple', coalesce(title, '') || ' ' || coalesce(description, '') || coalesce(url, ''))" + end + query.where(Arel.sql("#{tsvector} @@ #{tsquery}")) + end + # private # # Please add private methods to app/models/concerns/team_private.rb