From 6ba87e4ee7dc4e4a4afb1576b985dba2cf53afff Mon Sep 17 00:00:00 2001 From: Rikuoh <mail@riq0h.jp> Date: Sat, 22 Feb 2025 22:33:09 +0900 Subject: [PATCH] =?UTF-8?q?Rails=E6=A8=99=E6=BA=96=E3=81=AE=E3=83=9A?= =?UTF-8?q?=E3=83=BC=E3=82=B8=E3=83=8D=E3=83=BC=E3=82=B7=E3=83=A7=E3=83=B3?= =?UTF-8?q?=E3=81=AB=E5=B7=AE=E3=81=97=E6=9B=BF=E3=81=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Gemfile | 2 -- Gemfile.lock | 2 -- app/controllers/application_controller.rb | 1 - app/controllers/memos_controller.rb | 8 +++++++- app/helpers/application_helper.rb | 1 - app/views/memos/index.html.erb | 24 +++++++++++------------ app/views/memos/index.turbo_stream.erb | 14 ++++++------- config/initializers/pagy.rb | 1 - 8 files changed, 25 insertions(+), 28 deletions(-) delete mode 100644 config/initializers/pagy.rb diff --git a/Gemfile b/Gemfile index 73825c6..5826ff2 100644 --- a/Gemfile +++ b/Gemfile @@ -72,5 +72,3 @@ group :test do end gem 'tailwindcss-rails', '~> 4.0' - -gem 'pagy' diff --git a/Gemfile.lock b/Gemfile.lock index 32901a0..4f8f150 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -160,7 +160,6 @@ GEM racc (~> 1.4) nokogiri (1.18.3-x86_64-linux-musl) racc (~> 1.4) - pagy (9.3.3) pp (0.6.2) prettyprint prettyprint (0.2.0) @@ -306,7 +305,6 @@ DEPENDENCIES debug importmap-rails jbuilder - pagy puma (>= 5.0) rails (= 8.0.1) rails-i18n diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index c2a36d8..1c07694 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,4 +1,3 @@ class ApplicationController < ActionController::Base protect_from_forgery with: :exception - include Pagy::Backend end diff --git a/app/controllers/memos_controller.rb b/app/controllers/memos_controller.rb index 4825eb8..4c30c81 100644 --- a/app/controllers/memos_controller.rb +++ b/app/controllers/memos_controller.rb @@ -2,7 +2,13 @@ class MemosController < ApplicationController before_action :set_memo, only: %i[edit update destroy] def index - @pagy, @memos = pagy_countless(Memo.search(params[:query]), items: 12) + page = (params[:page] || 0).to_i + @memos = Memo.search(params[:query]) + .order(created_at: :desc) + .limit(4) + .offset(page * 4) + + @has_next = Memo.search(params[:query]).count > (page + 1) * 4 respond_to do |format| format.html diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index ed6a048..de6be79 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,3 +1,2 @@ module ApplicationHelper - include Pagy::Frontend end diff --git a/app/views/memos/index.html.erb b/app/views/memos/index.html.erb index d5d2681..586f9cc 100644 --- a/app/views/memos/index.html.erb +++ b/app/views/memos/index.html.erb @@ -21,19 +21,17 @@ <div id="memos"> <%= turbo_frame_tag "memos" do %> - <div class="flex flex-col gap-4"> - <%= render @memos %> - <%= render "empty_results", query: params[:query] if @memos.empty? %> - - </div> - <% end %> -</div> - -<div id="load-more" class="text-center mt-8"> - <% if @pagy.next %> - <%= link_to "もっと見る", - pagy_url_for(@pagy, @pagy.next), + <div id="memos-container" class="flex flex-col gap-4"> + <%= render @memos %> + <%= render "empty_results", query: params[:query] if @memos.empty? %> + </div> +<% end %> + <div id="load-more" class="text-center mt-8"> + <% if @has_next %> + <%= link_to "気力十分😤", + memos_path(page: (params[:page] || 0).to_i + 1, query: params[:query]), class: "inline-flex items-center px-4 py-2 bg-blue-500 hover:bg-blue-600 text-white font-medium rounded-lg transition-colors duration-200", - data: { turbo_method: :get } %> + data: { turbo_frame: "memos" } %> <% end %> </div> +</div> diff --git a/app/views/memos/index.turbo_stream.erb b/app/views/memos/index.turbo_stream.erb index 6d4b460..e1105c2 100644 --- a/app/views/memos/index.turbo_stream.erb +++ b/app/views/memos/index.turbo_stream.erb @@ -1,11 +1,11 @@ -<%= turbo_stream.append "memos" do %> +<%= turbo_stream.append "memos-container" do %> <%= render @memos %> <% end %> -<%= turbo_stream.update "load-more" do %> - <% if @pagy.next %> - <%= link_to "もっと見る", - pagy_url_for(@pagy, @pagy.next), +<div id="load-more" class="text-center mt-8"> + <% if @has_next %> + <%= link_to "気力十分😤", + memos_path(page: (params[:page] || 0).to_i + 1, query: params[:query]), class: "inline-flex items-center px-4 py-2 bg-blue-500 hover:bg-blue-600 text-white font-medium rounded-lg transition-colors duration-200", - data: { turbo_method: :get } %> + data: { turbo_frame: "memos" } %> <% end %> -<% end %> +</div> diff --git a/config/initializers/pagy.rb b/config/initializers/pagy.rb deleted file mode 100644 index 09f56c9..0000000 --- a/config/initializers/pagy.rb +++ /dev/null @@ -1 +0,0 @@ -require 'pagy/extras/countless'