Skip to content

Commit

Permalink
Replace kaminari by pagy (nanego#189)
Browse files Browse the repository at this point in the history
  • Loading branch information
MBausson authored Nov 6, 2024
1 parent c278320 commit 2a29482
Show file tree
Hide file tree
Showing 15 changed files with 333 additions and 96 deletions.
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ gem "simple_token_authentication", "~> 1.0"

gem "datagrid"
gem "faraday"
gem "kaminari"
gem "kaminari" # TODO: Remove when removing datagrid
gem "pagy"
gem "pg"
gem "virtus"

Expand Down
6 changes: 4 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,9 @@ GEM
validate_url
webfinger (~> 2.0)
orm_adapter (0.5.0)
parallel (1.26.3)
parser (3.3.5.0)
pagy (9.1.1)
parallel (1.24.0)
parser (3.3.1.0)
ast (~> 2.4.1)
racc
passenger (6.0.23)
Expand Down Expand Up @@ -630,6 +631,7 @@ DEPENDENCIES
omniauth (~> 2.1.0)
omniauth-rails_csrf_protection
omniauth_openid_connect (~> 0.7.0)
pagy
passenger
pg
puma
Expand Down
4 changes: 4 additions & 0 deletions app/assets/stylesheets/application.scss
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,10 @@ body {
max-width: 100px;
}

nav.pagy-bootstrap.nav .pagination {
margin-bottom: 0;
}

/* Tom-Select */
/* Fix for dark mode */
.ts-dropdown, .ts-control, .ts-control input {
Expand Down
1 change: 1 addition & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
class ApplicationController < ActionController::Base
include ChangelogContextApplication
include Localization
include Pagy::Backend

acts_as_token_authentication_handler_for User

Expand Down
4 changes: 2 additions & 2 deletions app/controllers/cables_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ class CablesController < ApplicationController
before_action :set_cable, only: [:destroy]

def index
@cables = sorted Cable.includes(:connections, connections: [:port])
.order(created_at: :desc).page(params[:page]).per(100)
@cables = sorted(Cable.includes(:connections, connections: [:port]).order(created_at: :desc))
@pagy, @cables = pagy(@cables)
end

def destroy
Expand Down
3 changes: 2 additions & 1 deletion app/controllers/changelog_entries_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class ChangelogEntriesController < ApplicationController
def index
@changelog_entries = sorted changelog_scope.order(created_at: :desc).page(params[:page]).per(per_page)
@changelog_entries = sorted(changelog_scope.includes(:author, :object).order(created_at: :desc))
@pagy, @changelog_entries = pagy(@changelog_entries)
end

def show
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/connections_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

class ConnectionsController < ApplicationController
def index
@connections = sorted Connection.includes(:port, :card, :server, :card_type, :port_type, cable: :connections)
.order(created_at: :desc).page(params[:page]).per(100)
@connections = sorted Connection.includes(:port, :card, :server, :card_type, :port_type, cable: :connections).order(created_at: :desc)
@pagy, @connections = pagy(@connections)
end

def edit
Expand Down
1 change: 1 addition & 0 deletions app/controllers/modeles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class ModelesController < ApplicationController
def index
@filter = ProcessorFilter.new(Modele.includes(:category, :enclosures).order(:name), params)
@modeles = @filter.results

@types = @modeles.group_by { |m| m.category.name }.sort_by { |categorie, modeles| categorie.to_s }
end

Expand Down
3 changes: 2 additions & 1 deletion app/controllers/servers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ def index
.references(:room, :islet, :bay, modele: :category)
.order(:name)
@filter = ProcessorFilter.new(@servers, params)
@servers = @filter.results

@pagy, @servers = pagy(@filter.results)
@search_params = search_params
end

Expand Down
2 changes: 2 additions & 0 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 accepted_format_for_attachment(model_klass, attribute_name)
validator = model_klass.validators_on(attribute_name.to_sym).find do |v|
v.is_a?(ActiveStorageValidations::ContentTypeValidator)
Expand Down
2 changes: 1 addition & 1 deletion app/views/cables/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@
<% end %>
<% end %>
<%= paginate @cables %>
<%== pagy_bootstrap_nav @pagy %>
</div>
68 changes: 36 additions & 32 deletions app/views/changelog_entries/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,47 +10,51 @@

<div class="p-4 border-top">
<%= turbo_frame_tag "changelog-entries" do %>
<%= render List::DataTableComponent.new(decorate(@changelog_entries)) do |table| %>
<% table.with_column("", style: "min-width: 20px; width: 20px") do |changelog_entry| %>
<%= link_to changelog_entry_path(changelog_entry), class: "btn btn-primary", target: :_top do %>
<span class="bi bi-eye" aria-hidden="true" data-controller="tooltip" data-bs-placement="top"
title="<%= t("action.show") %>"></span>
<span class="visually-hidden"><%= t("action.show") %></span>
<div class="d-flex flex-column row-gap-4">
<%= render List::DataTableComponent.new(decorate(@changelog_entries)) do |table| %>
<% table.with_column("", style: "min-width: 20px; width: 20px") do |changelog_entry| %>
<%= link_to changelog_entry_path(changelog_entry), class: "btn btn-primary", target: :_top do %>
<span class="bi bi-eye" aria-hidden="true" data-controller="tooltip" data-bs-placement="top"
title="<%= t("action.show") %>"></span>
<span class="visually-hidden"><%= t("action.show") %></span>
<% end %>
<% end %>
<% end %>
<% table.with_column(ChangelogEntry.human_attribute_name(:action), sort_by: :action) do |changelog_entry| %>
<%= render changelog_entry.action_label_to_component %>
<% end %>
<% table.with_column(ChangelogEntry.human_attribute_name(:action), sort_by: :action) do |changelog_entry| %>
<%= render changelog_entry.action_label_to_component %>
<% end %>
<% table.with_column(ChangelogEntry.human_attribute_name(:author), sort_by: :author_id) do |changelog_entry| %>
<%= link_to_if changelog_entry.author_id?, changelog_entry.author_display_name, changelog_entry.author, target: :_top %>
<% end %>
<% table.with_column(ChangelogEntry.human_attribute_name(:author), sort_by: :author_id) do |changelog_entry| %>
<%= link_to_if changelog_entry.author_id?, changelog_entry.author_display_name, changelog_entry.author, target: :_top %>
<% end %>
<% table.with_column(ChangelogEntry.human_attribute_name(:object), sort_by: :object_id) do |changelog_entry| %>
<% begin %>
<%= link_to changelog_entry.object_display_name, changelog_entry.object, target: :_top %>
<% rescue NoMethodError %>
<%= changelog_entry.object_display_name %>
<% table.with_column(ChangelogEntry.human_attribute_name(:object), sort_by: :object_id) do |changelog_entry| %>
<% begin %>
<%= link_to changelog_entry.object_display_name, changelog_entry.object, target: :_top %>
<% rescue NoMethodError %>
<%= changelog_entry.object_display_name %>
<% end %>
<% end %>
<% end %>
<% table.with_column(ChangelogEntry.human_attribute_name(:attributes)) do |changelog_entry| %>
<div class="row">
<div class="col-lg-6">
<%= sanitize changelog_entry.split_diff.left %>
<% table.with_column(ChangelogEntry.human_attribute_name(:attributes)) do |changelog_entry| %>
<div class="row">
<div class="col-lg-6">
<%= sanitize changelog_entry.split_diff.left %>
</div>
<div class="col-lg-6">
<%= sanitize changelog_entry.split_diff.right %>
</div>
</div>
<div class="col-lg-6">
<%= sanitize changelog_entry.split_diff.right %>
</div>
</div>
<% end %>
<% end %>
<% table.with_column(t("label_date"), sort_by: :created_at) do |changelog_entry| %>
<%= l changelog_entry.created_at %>
<% table.with_column(t("label_date"), sort_by: :created_at) do |changelog_entry| %>
<%= l changelog_entry.created_at %>
<% end %>
<% end %>
<% end %>
<%= paginate @changelog_entries %>
<% if @pagy.pages > 1 %>
<%== pagy_bootstrap_nav(@pagy) %>
<% end %>
</div>
<% end %>
</div>
2 changes: 1 addition & 1 deletion app/views/connections/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,5 @@
<% end %>
<% end %>
<%= paginate @connections %>
<%== pagy_bootstrap_nav(@pagy) %>
</div>
112 changes: 59 additions & 53 deletions app/views/servers/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -125,61 +125,67 @@
<% end %>
<%= turbo_frame_tag(dom_id(Server, :table), data: { turbo_action: :advance }) do %>
<%= render List::DataTableComponent.new(@servers) do |table| %>
<% table.with_column(Server.human_attribute_name(:name), sort_by: :name) do |server| %>
<%= link_to server.name, server_path(server), class: "fw-bold", data: { turbo_frame: :_top } %>
<% end %>
<% table.with_column(Server.human_attribute_name(:numero), sort_by: :numero) do |server| %>
<%= link_to server.numero, server_path(server), class: "fw-bold", data: { turbo_frame: :_top } %>
<% end %>
<% table.with_column(Server.human_attribute_name(:type), sort_by: :"categories.name") do |server| %>
<%= link_to server.modele.category, category_path(server.modele.category),
data: { turbo_frame: :_top } if server.modele.try(:category) %>
<% end %>
<% table.with_column(Server.human_attribute_name(:room), sort_by: :"rooms.name") do |server| %>
<%= link_to server.room, room_path(server.room), data: { turbo_frame: :_top } if server.room %>
<% end %>
<% table.with_column(Islet.model_name.human, sort_by: :"islets.name") do |server| %>
<%= link_to server.islet, islet_path(server.islet), data: { turbo_frame: :_top } if server.islet %>
<% end %>
<% table.with_column(Bay.model_name.human, sort_by: :"bays.id") do |server| %>
<%= link_to server.bay, bay_path(server.bay), data: { turbo_frame: :_top } if server.bay %>
<% end %>
<% table.with_column(Server.human_attribute_name(:network_types)) do |server| %>
<%= server.decorated.network_types_to_human %>
<% end %>
<% table.with_column(style: "min-width: 100px; width: 100px") do |server| %>
<div class="btn-group btn-group-sm" role="group" aria-label="...">
<%= link_to duplicate_server_path(server), class: "btn btn-success", data: { turbo_frame: :_top } do %>
<span class="bi bi-copy" title="<%= t("action.duplicate") %>" aria-hidden="true"
data-controller="tooltip"
data-bs-placement="left"></span>
<span class="visually-hidden"><%= t("action.duplicate") %></span>
<% end %>
<%= link_to edit_server_path(server), class: "btn btn-info", data: { turbo_frame: :_top } do %>
<span class="bi bi-pencil" title="<%= t("action.edit") %>" aria-hidden="true"
data-controller="tooltip"
data-bs-placement="left"></span>
<span class="visually-hidden"><%= t("action.edit") %></span>
<% end %>
<%= link_to server_path(server, @search_params),
method: :delete,
data: { turbo_frame: :_top, confirm: t("action.confirm") },
class: "btn btn-danger" do %>
<span class="bi bi-trash" title="<%= t("action.delete") %>" aria-hidden="true"
<div class="d-flex flex-column row-gap-4">
<%= render List::DataTableComponent.new(@servers) do |table| %>
<% table.with_column(Server.human_attribute_name(:name), sort_by: :name) do |server| %>
<%= link_to server.name, server_path(server), class: "fw-bold", data: { turbo_frame: :_top } %>
<% end %>
<% table.with_column(Server.human_attribute_name(:numero), sort_by: :numero) do |server| %>
<%= link_to server.numero, server_path(server), class: "fw-bold", data: { turbo_frame: :_top } %>
<% end %>
<% table.with_column(Server.human_attribute_name(:type), sort_by: :"categories.name") do |server| %>
<%= link_to server.modele.category, category_path(server.modele.category),
data: { turbo_frame: :_top } if server.modele.try(:category) %>
<% end %>
<% table.with_column(Server.human_attribute_name(:room), sort_by: :"rooms.name") do |server| %>
<%= link_to server.room, room_path(server.room), data: { turbo_frame: :_top } if server.room %>
<% end %>
<% table.with_column(Islet.model_name.human, sort_by: :"islets.name") do |server| %>
<%= link_to server.islet, islet_path(server.islet), data: { turbo_frame: :_top } if server.islet %>
<% end %>
<% table.with_column(Bay.model_name.human, sort_by: :"bays.id") do |server| %>
<%= link_to server.bay, bay_path(server.bay), data: { turbo_frame: :_top } if server.bay %>
<% end %>
<% table.with_column(Server.human_attribute_name(:network_types)) do |server| %>
<%= server.decorated.network_types_to_human %>
<% end %>
<% table.with_column(style: "min-width: 100px; width: 100px") do |server| %>
<div class="btn-group btn-group-sm" role="group" aria-label="...">
<%= link_to duplicate_server_path(server), class: "btn btn-success", data: { turbo_frame: :_top } do %>
<span class="bi bi-copy" title="<%= t("action.duplicate") %>" aria-hidden="true"
data-controller="tooltip"
data-bs-placement="left"></span>
<span class="visually-hidden"><%= t("action.duplicate") %></span>
<% end %>
<%= link_to edit_server_path(server), class: "btn btn-info", data: { turbo_frame: :_top } do %>
<span class="bi bi-pencil" title="<%= t("action.edit") %>" aria-hidden="true"
data-controller="tooltip"
data-bs-placement="left"></span>
<span class="visually-hidden"><%= t("action.delete") %></span>
<% end %>
</div>
<span class="visually-hidden"><%= t("action.edit") %></span>
<% end %>
<%= link_to server_path(server, @search_params),
method: :delete,
data: { turbo_frame: :_top, confirm: t("action.confirm") },
class: "btn btn-danger" do %>
<span class="bi bi-trash" title="<%= t("action.delete") %>" aria-hidden="true"
data-controller="tooltip"
data-bs-placement="left"></span>
<span class="visually-hidden"><%= t("action.delete") %></span>
<% end %>
</div>
<% end %>
<% end %>
<% end %>
<% if @pagy.pages > 1 %>
<%== pagy_bootstrap_nav(@pagy) %>
<% end %>
</div>
<% end %>
</div>
Loading

0 comments on commit 2a29482

Please sign in to comment.