Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
41 changes: 23 additions & 18 deletions app/controllers/messages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,13 @@ class MessagesController < ApplicationController

# GET /messages
def index
query = params[:q]
unless query
if (query = params[:q])
search query
else
@messages = []
return
end

page = params[:page].to_i
list_ids = get_list_ids(params)
if list_ids.empty?
raise "Need to select at least one list"
end

# %> and <-> are defined by pg_trgm.
# https://www.postgresql.org/docs/17/pgtrgm.html
message_where = if Rails.env.production?
Message.where('body %> ? AND list_id IN (?)', query, list_ids)
.order(Arel.sql('body <-> ?', query))
else
Message.where('body LIKE ? AND list_id IN (?)', "%#{query}%", list_ids)
end
@messages = message_where.offset(page * PER_PAGE).limit(PER_PAGE)
render :search
end

# GET /messages/1
Expand All @@ -37,6 +23,7 @@ def show
end

private

def get_list_ids(params)
list_ids = []
['ruby-talk', 'ruby-core', 'ruby-list', 'ruby-dev'].each do |name|
Expand All @@ -46,4 +33,22 @@ def get_list_ids(params)
end
list_ids
end

def search(query)
page = params[:page].to_i
list_ids = get_list_ids(params)
if list_ids.empty?
raise "Need to select at least one list"
end

# %> and <-> are defined by pg_trgm.
# https://www.postgresql.org/docs/17/pgtrgm.html
message_where = if Rails.env.production?
Message.where('body %> ? AND list_id IN (?)', query, list_ids)
.order(Arel.sql('body <-> ?', query))
else
Message.where('body LIKE ? AND list_id IN (?)', "%#{query}%", list_ids)
end
@messages = message_where.offset(page * PER_PAGE).limit(PER_PAGE)
end
end
File renamed without changes.
Loading