From 1607a5db98e2906694b8ffecf2095bcb07a99218 Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Sat, 25 Oct 2025 10:53:25 +0900 Subject: [PATCH 1/4] bundle action_args --- Gemfile | 2 ++ Gemfile.lock | 2 ++ 2 files changed, 4 insertions(+) diff --git a/Gemfile b/Gemfile index 4c116e2..b0670a7 100644 --- a/Gemfile +++ b/Gemfile @@ -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 diff --git a/Gemfile.lock b/Gemfile.lock index c4d225a..8dd6c74 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -349,6 +350,7 @@ PLATFORMS x86_64-linux-musl DEPENDENCIES + action_args active_decorator aws-sdk-s3 (~> 1) bootsnap From 1e7417c49b9916b1e77ae3a400984b7703e1f86e Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Sat, 25 Oct 2025 11:07:14 +0900 Subject: [PATCH 2/4] Saner and cleaner actions with action_args --- app/controllers/messages_controller.rb | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/app/controllers/messages_controller.rb b/app/controllers/messages_controller.rb index 960cc4f..5204fa5 100644 --- a/app/controllers/messages_controller.rb +++ b/app/controllers/messages_controller.rb @@ -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 @@ -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 @@ -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" @@ -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) From 90eeb56672ad2cf838ac75ad6e024b8d72e2fbaf Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Sat, 25 Oct 2025 11:20:16 +0900 Subject: [PATCH 3/4] Properly raise RecordNotFound if the message was not found --- app/controllers/messages_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/messages_controller.rb b/app/controllers/messages_controller.rb index 5204fa5..56e26fc 100644 --- a/app/controllers/messages_controller.rb +++ b/app/controllers/messages_controller.rb @@ -23,7 +23,7 @@ def index(list_name: nil, q: nil, page: nil) # 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) + @message = Message.find_by!(list_id: @list, list_seq: list_seq) end private From 1ff1d9780404d123a50bc891b1e0690f5998a799 Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Tue, 28 Oct 2025 00:01:57 +0900 Subject: [PATCH 4/4] What's wrong with `q`? --- .rubocop.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.rubocop.yml b/.rubocop.yml index 144320f..c2f7d07 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -13,3 +13,6 @@ Layout: Metrics: Enabled: false + +Naming/MethodParameterName: + Enabled: false