Skip to content

Commit

Permalink
Replace kaminari with pagy
Browse files Browse the repository at this point in the history
Removes the need for PaginatedDecorator.
Pagy returns two objects instead of mixing them together
  • Loading branch information
Earlopain committed Sep 21, 2023
1 parent f7312e8 commit 40d3dda
Show file tree
Hide file tree
Showing 15 changed files with 30 additions and 64 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ source "https://rubygems.org"
gem "addressable"
gem "good_job"
gem "httparty"
gem "kaminari"
gem "listen"
gem "nokogiri"
gem "open3"
gem "pagy"
gem "pg", "~> 1.1"
gem "puma"
gem "rails", "~> 7.0"
Expand Down
15 changes: 2 additions & 13 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -117,18 +117,6 @@ GEM
concurrent-ruby (~> 1.0)
jaro_winkler (1.5.6)
json (2.6.3)
kaminari (1.2.2)
activesupport (>= 4.1.0)
kaminari-actionview (= 1.2.2)
kaminari-activerecord (= 1.2.2)
kaminari-core (= 1.2.2)
kaminari-actionview (1.2.2)
actionview
kaminari-core (= 1.2.2)
kaminari-activerecord (1.2.2)
activerecord
kaminari-core (= 1.2.2)
kaminari-core (1.2.2)
kramdown (2.4.0)
rexml
kramdown-parser-gfm (1.1.0)
Expand Down Expand Up @@ -175,6 +163,7 @@ GEM
mini_portile2 (~> 2.8.2)
racc (~> 1.4)
open3 (0.1.2)
pagy (6.0.4)
parallel (1.23.0)
parser (3.2.2.3)
ast (~> 2.4.1)
Expand Down Expand Up @@ -316,13 +305,13 @@ DEPENDENCIES
factory_bot_rails
good_job
httparty
kaminari
listen
minitest-rails
minitest-reporters
mocha
nokogiri
open3
pagy
pg (~> 1.1)
puma
rails (~> 7.0)
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/artists_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ class ArtistsController < ApplicationController
respond_to :html

def index
@artists = Artist.includes(:artist_urls).search(index_search_params).page params[:page]
@pagy, @artists = Artist.includes(:artist_urls).search(index_search_params).pagy(params)
end

def show
@artist = Artist.includes(:artist_urls).find(params[:id])
@search_params = instance_search_params.merge(artist_id: @artist.id)
@submission_files = SubmissionFile.search(@search_params).with_everything.page params[:page]
@pagy, @submission_files = SubmissionFile.search(@search_params).with_everything.pagy(params)
respond_with(@artist)
end

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/log_events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class LogEventsController < ApplicationController
def index
@log_events = LogEvent.search(search_params).page(params[:page]).decorate
@pagy, @log_events = LogEvent.search(search_params).pagy_and_decorate(params)
end

def show
Expand Down
10 changes: 5 additions & 5 deletions app/controllers/submission_files_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class SubmissionFilesController < ApplicationController

def index
@search_params = search_params
@submission_files = SubmissionFile.search(@search_params).with_everything.page(params[:page])
@pagy, @submission_files = SubmissionFile.search(@search_params).with_everything.pagy(params)
end

def show
Expand Down Expand Up @@ -38,18 +38,18 @@ def update_matching_e6_posts

def backlog
@search_params = search_params.merge(in_backlog: true)
@submission_files = SubmissionFile.search(@search_params)
@pagy, @submission_files = SubmissionFile.search(@search_params)
.with_everything
.reorder(added_to_backlog_at: :desc)
.page params[:page]
.pagy(params)
end

def hidden
@search_params = search_params.merge(hidden_from_search: true)
@submission_files = SubmissionFile.search(@search_params)
@pagy, @submission_files = SubmissionFile.search(@search_params)
.with_everything
.reorder(hidden_from_search_at: :desc)
.page params[:page]
.pagy(params)
end

def hide_many
Expand Down
14 changes: 0 additions & 14 deletions app/decorators/paginated_decorator.rb

This file was deleted.

6 changes: 2 additions & 4 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

module ApplicationHelper
include Pagy::Frontend

def error_messages_for(value)
value.errors.full_messagess.join(",")
end
Expand Down Expand Up @@ -52,10 +54,6 @@ def hideable_search(path, &)
tag.div(search)
end

def paginated(values)
content_for(:paginator) { paginate values }
end

def job_stats
@job_stats ||= JobStats.new
end
Expand Down
14 changes: 11 additions & 3 deletions app/models/application_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def join_hash(input)
end
end

concerning :Decoration do
concerning :ControllerMethods do
def decorate
self.class.decorator_class.new(self)
end
Expand All @@ -125,8 +125,16 @@ def decorator_class
"#{model_name}Decorator".constantize
end

def decorate
PaginatedDecorator.new(all)
def pagy(params)
page = [params[:page].to_i, 1].max
limit = params[:limit].to_i <= 0 ? nil : params[:limit]
pagy = Pagy.new(page: page, items: limit, count: count)
[pagy, offset(pagy.offset).limit(pagy.items)]
end

def pagy_and_decorate(params)
pagy, elements = pagy(params)
[pagy, elements.map(&:decorate)]
end
end
end
Expand Down
2 changes: 0 additions & 2 deletions app/views/artists/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,4 @@
</tbody>
</table>

<%= paginated @artists %>
<% page_title "Artists" %>
2 changes: 1 addition & 1 deletion app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<% end %>
<%= yield %>
</div>
<%= content_for(:paginator) if content_for? :paginator %>
<%= pagy_nav(@pagy).html_safe if instance_variable_defined?(:@pagy) && @pagy.pages > 1 %>
</div>
<div class="footer-padding"></div>
<%= render "application/footer" %>
Expand Down
2 changes: 0 additions & 2 deletions app/views/log_events/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,4 @@
</tbody>
</table>

<%= paginated @log_events %>
<% page_title "Logs" %>
1 change: 0 additions & 1 deletion app/views/submission_files/_list_with_search.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@
<% submission_files.each do |file| %>
<%= render "submission_files/sample", submission_file: file %>
<% end %>
<%= paginated submission_files %>
1 change: 0 additions & 1 deletion app/views/submission_files/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@
<% @submission_files.each do |file| %>
<%= render "submission_files/sample", submission_file: file %>
<% end %>
<%= paginated @submission_files %>
<% page_title "Submission Files" %>
14 changes: 0 additions & 14 deletions config/initializers/kaminari_config.rb

This file was deleted.

5 changes: 5 additions & 0 deletions config/initializers/pagy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

require "pagy/extras/overflow"

Pagy::DEFAULT[:items] = 25

0 comments on commit 40d3dda

Please sign in to comment.