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 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 diff --git a/app/controllers/messages_controller.rb b/app/controllers/messages_controller.rb index 960cc4f..56e26fc 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)