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
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ Layout:

Metrics:
Enabled: false

Naming/MethodParameterName:
Enabled: false
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ gem "stimulus-rails"

gem 'tailwindcss-rails'

gem 'action_args'

gem 'active_decorator'

# Use Redis adapter to run Action Cable in production
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
GEM
remote: https://rubygems.org/
specs:
action_args (2.7.3)
actioncable (8.0.3)
actionpack (= 8.0.3)
activesupport (= 8.0.3)
Expand Down Expand Up @@ -349,6 +350,7 @@ PLATFORMS
x86_64-linux-musl

DEPENDENCIES
action_args
active_decorator
aws-sdk-s3 (~> 1)
bootsnap
Expand Down
23 changes: 11 additions & 12 deletions app/controllers/messages_controller.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
class MessagesController < ApplicationController
PER_PAGE = 50

# GET /messages
def index
if (list_name = params[:list_name])
# GET /ruby-dev or /q=searchterm
def index(list_name: nil, q: nil, page: nil)
if list_name
@list = List.find_by_name list_name

messages = Message.with_recursive(parent_and_children: [Message.where(list_id: @list, parent_id: nil).order(:id).limit(100), Message.joins('inner join parent_and_children on messages.parent_id = parent_and_children.id')])
.joins('inner join parent_and_children on parent_and_children.id = messages.id')
@messages = compose_tree(messages)
elsif (query = params[:q])
search query
elsif q
search q, page

render :search
else
Expand All @@ -20,10 +20,10 @@ def index
end
end

# GET /messages/ruby-dev/1
def show
@list = List.find_by_name(params[:list_name])
@message = Message.find_by(list_id: @list, list_seq: params[:list_seq])
# GET /ruby-dev/1
def show(list_name:, list_seq:)
@list = List.find_by_name(list_name)
@message = Message.find_by!(list_id: @list, list_seq: list_seq)
end

private
Expand All @@ -38,8 +38,7 @@ def get_list_ids(params)
list_ids
end

def search(query)
page = params[:page].to_i
def search(query, page)
list_ids = get_list_ids(params)
if list_ids.empty?
raise "Need to select at least one list"
Expand All @@ -48,7 +47,7 @@ def search(query)
# %> and <-> are defined by pg_trgm.
# https://www.postgresql.org/docs/17/pgtrgm.html
message_where = Message.where('body %> ? AND list_id IN (?)', query, list_ids).order(Arel.sql('body <-> ?', query))
@messages = message_where.offset(page * PER_PAGE).limit(PER_PAGE)
@messages = message_where.offset(page.to_i * PER_PAGE).limit(PER_PAGE)
end

def compose_tree(messages)
Expand Down