Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions app/helpers/search_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ def search_posts(user, params)
posts = posts.paginate(page: params[:page], per_page: 25)

posts = if search_string.present?
posts.search(search_data[:search]).user_sort({ term: params[:sort], default: :search_score },
relevance: :search_score,
score: :score, age: :created_at,
activity: :updated_at)
posts.search(search_string).user_sort({ term: params[:sort], default: :search_score },
relevance: :search_score,
score: :score, age: :created_at,
activity: :updated_at)
else
posts.user_sort({ term: params[:sort], default: :score },
score: :score, age: :created_at, activity: :updated_at)
Expand Down
5 changes: 5 additions & 0 deletions app/models/application_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ def self.match_search(term, **cols)
select(Arel.sql("`#{table_name}`.*, #{sanitized} AS search_score")).where(sanitized)
end

def self.exclusion_search(term, **cols)
matched = match_search(term[1..], **cols)
select(Arel.sql("`#{table_name}`.*, 1 AS search_score")).excluding(matched)
end

def self.sanitize_name(name)
name.to_s.delete('`').insert(0, '`').insert(-1, '`')
end
Expand Down
6 changes: 5 additions & 1 deletion app/models/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@
# @param term [String] the search term
# @return [ActiveRecord::Relation<Post>]
def self.search(term)
match_search term, posts: [:body_markdown, :title]
if term.match(/^(?:\s*?-\S+?)+?$/)
Comment thread
github-advanced-security[bot] marked this conversation as resolved.
Fixed
exclusion_search term, posts: [:body_markdown, :title]
else
match_search term, posts: [:body_markdown, :title]
end
end

def self.by_slug(slug, user)
Expand Down