Skip to content

Commit

Permalink
CV2-5129: fix CC
Browse files Browse the repository at this point in the history
  • Loading branch information
melsawy committed Aug 28, 2024
1 parent 0aef3e9 commit 902189e
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions app/models/team.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 902189e

Please sign in to comment.