From 6d3a269231626f803534d99317892765a436bac4 Mon Sep 17 00:00:00 2001 From: david Date: Sun, 21 Jul 2024 00:33:38 +0200 Subject: [PATCH] add pagy, improve checks#index --- Gemfile | 1 + Gemfile.lock | 2 + app/assets/stylesheets/layout.scss | 4 + app/controllers/application_controller.rb | 2 + app/controllers/checks_controller.rb | 6 +- app/helpers/application_helper.rb | 3 + app/models/check.rb | 4 + .../_checklist_entry.html.erb | 23 +- app/views/checks/index.html.erb | 15 ++ app/views/layouts/_navigation.html.erb | 4 +- config/initializers/pagy.rb | 220 ++++++++++++++++++ 11 files changed, 274 insertions(+), 10 deletions(-) create mode 100644 config/initializers/pagy.rb diff --git a/Gemfile b/Gemfile index f4dd1ed..2bb2912 100644 --- a/Gemfile +++ b/Gemfile @@ -51,6 +51,7 @@ gem 'bootstrap_form' gem 'caxlsx' gem 'caxlsx_rails' gem 'image_processing', '~> 1.2' +gem 'pagy', '~> 9.0' gem 'prawn-rails' gem 'sablon' diff --git a/Gemfile.lock b/Gemfile.lock index 9e24a71..5acfb48 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -178,6 +178,7 @@ GEM racc (~> 1.4) nokogiri (1.16.6-x86_64-linux) racc (~> 1.4) + pagy (9.0.2) parallel (1.25.1) parser (3.3.4.0) ast (~> 2.4.1) @@ -350,6 +351,7 @@ DEPENDENCIES image_processing (~> 1.2) jbuilder jsbundling-rails + pagy (~> 9.0) prawn-rails puma (>= 5.0) rails (~> 7.1.3, >= 7.1.3.4) diff --git a/app/assets/stylesheets/layout.scss b/app/assets/stylesheets/layout.scss index 67b1497..3367b24 100644 --- a/app/assets/stylesheets/layout.scss +++ b/app/assets/stylesheets/layout.scss @@ -53,4 +53,8 @@ border: 0; border-left: 4px solid var(--bs-warning); +} + +.navbar-brand { + color: $pink; } \ No newline at end of file diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 3ce22a9..59e71f6 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,6 +1,8 @@ # frozen_string_literal: true class ApplicationController < ActionController::Base + include Pagy::Backend + before_action :initialize_navbar private diff --git a/app/controllers/checks_controller.rb b/app/controllers/checks_controller.rb index 7f26d7e..90c7a88 100644 --- a/app/controllers/checks_controller.rb +++ b/app/controllers/checks_controller.rb @@ -3,7 +3,7 @@ class ChecksController < ApplicationController # GET /checks or /checks.json def index - @checks = Check.all + @pagy, @checks = pagy(Check.search(filter_params[:s])) end # GET /checks/1 or /checks/1.json @@ -66,6 +66,10 @@ class ChecksController < ApplicationController end end + def filter_params + @filter_params ||= params.permit(filter: %i[s level])[:filter] || {} + end + private # Use callbacks to share common setup or constraints between actions. diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 15b06f0..ebb9bc8 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,4 +1,7 @@ # frozen_string_literal: true module ApplicationHelper + include Pagy::Frontend + + delegate :filter_params, to: :controller end diff --git a/app/models/check.rb b/app/models/check.rb index 2b14c9f..582b491 100644 --- a/app/models/check.rb +++ b/app/models/check.rb @@ -4,4 +4,8 @@ class Check < ApplicationRecord enum :level, %i[A AA AAA] has_rich_text :success_criterion_html + + scope :search, lambda { |term| + joins("INNER JOIN action_text_rich_texts ON action_text_rich_texts.record_id = checks.id AND record_type = 'Check'").where('checks.name LIKE :term OR action_text_rich_texts.body LIKE :term', term: "%#{term}%") + } end diff --git a/app/views/checklist_entries/_checklist_entry.html.erb b/app/views/checklist_entries/_checklist_entry.html.erb index 79e526d..b274b1d 100644 --- a/app/views/checklist_entries/_checklist_entry.html.erb +++ b/app/views/checklist_entries/_checklist_entry.html.erb @@ -1,10 +1,17 @@ -
-
- <%= button_to tag.i(class: "bi bi-arrow-down"), checklist_entry_path(checklist_entry), method: :patch, class: "btn btn-link p-0 float-start", data: { turbo_frame: "checklist_entries" }, params: { checklist_entry: { position: checklist_entry.position + 1 }} %> - <%= button_to tag.i(class: "bi bi-arrow-up"), checklist_entry_path(checklist_entry), method: :patch, class: "btn btn-link p-0 pe-3 float-start", data: { turbo_frame: "checklist_entries" }, params: { checklist_entry: { position: checklist_entry.position - 1 }} %> - <%= button_to tag.i(class: "bi bi-trash"), checklist_entry_path(checklist_entry), method: :delete, class: "btn btn-link p-0 ps-3 float-end" %> - <%= link_to tag.i(class: "bi bi-pencil"), edit_checklist_entry_path(checklist_entry), class: "float-end" %> +<% is_first = checklist_entry == checklist_entry.checklist.checklist_entries.first %> +<% is_last = checklist_entry == checklist_entry.checklist.checklist_entries.last %> + +
+
+
+ <%= button_to tag.i(class: "bi bi-arrow-down"), checklist_entry_path(checklist_entry), method: :patch, class: "btn btn-link p-0 #{"pe-3" if is_last } float-start", data: { turbo_frame: "checklist_entries" }, params: { checklist_entry: { position: checklist_entry.position + 1 }} unless is_last %> + <%= button_to tag.i(class: "bi bi-arrow-up"), checklist_entry_path(checklist_entry), method: :patch, class: "btn btn-link p-0 pe-3 float-start", data: { turbo_frame: "checklist_entries" }, params: { checklist_entry: { position: checklist_entry.position - 1 }} unless is_first %> +
+
<%# checklist_entry.position %> - <%= link_to(checklist_entry.check.name, checklist_entry.check, data: { turbo_frame: "_top" }) %> -
+ <%= link_to(checklist_entry.check.name, checklist_entry.check, data: { turbo_frame: "_top" }, class: "flex-grow-1") %> + <%= button_to tag.i(class: "bi bi-trash"), checklist_entry_path(checklist_entry), method: :delete, class: "btn btn-link" %> + <%= link_to tag.i(class: "bi bi-pencil"), edit_checklist_entry_path(checklist_entry), class: "btn btn-link" %> +
+
diff --git a/app/views/checks/index.html.erb b/app/views/checks/index.html.erb index bf029ef..b14aa48 100644 --- a/app/views/checks/index.html.erb +++ b/app/views/checks/index.html.erb @@ -1,5 +1,18 @@

<%= t("scaffold.pagetitle_index", model: Check.model_name.human(count: 2)) %>

+<%= bootstrap_form_with(url: checks_path(page: params[:page]), method: :get, scope: :filter) do |form| %> + <%= form.hidden_field :page, value: params[:page] if params[:page] %> +
+
+ <%= form.text_field(:s, placeholder: "suchen...", hide_label: true, value: filter_params[:s]) %> +
+
+ <%= form.submit name: "", value: "Suchen" %> + <%= link_to "Filter löschen", checks_path, class: "btn btn-outline-secondary" if filter_params[:s] %> +
+
+<% end %> +<%== pagy_info(@pagy) %> @@ -20,6 +33,8 @@
+<%== pagy_bootstrap_nav(@pagy) %> +
<%= link_to t("scaffold.link_new", model: Check.model_name.human), new_check_path %>
\ No newline at end of file diff --git a/app/views/layouts/_navigation.html.erb b/app/views/layouts/_navigation.html.erb index 36067c1..9768444 100644 --- a/app/views/layouts/_navigation.html.erb +++ b/app/views/layouts/_navigation.html.erb @@ -1,6 +1,8 @@