From ac65df65aeeef3f143d73386d8815594e15f694c Mon Sep 17 00:00:00 2001 From: Stepan Tischenko Date: Tue, 3 Oct 2023 22:07:31 +0300 Subject: [PATCH] pagination, sort (#20) * pagination, sort * pagination, sort 2 * pagination, sort 3 * pagination, sort 4 --------- Co-authored-by: Denis Zakharov <78980031+DenisZackharov@users.noreply.github.com> --- .gitignore | 2 ++ Gemfile | 1 + Gemfile.lock | 15 ++++++++++++++- app/controllers/projects_controller.rb | 2 +- app/views/projects/index.html.erb | 6 ++++++ config/initializers/kaminari_config.rb | 14 ++++++++++++++ 6 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 config/initializers/kaminari_config.rb diff --git a/.gitignore b/.gitignore index f22dd347..7a80b93d 100644 --- a/.gitignore +++ b/.gitignore @@ -34,3 +34,5 @@ /yarn-error.log yarn-debug.log* .yarn-integrity + +.idea/ \ No newline at end of file diff --git a/Gemfile b/Gemfile index d84f4986..737c7ea3 100644 --- a/Gemfile +++ b/Gemfile @@ -17,6 +17,7 @@ gem "webpacker", "~> 5.0" gem "turbolinks", "~> 5" # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder gem "jbuilder", "~> 2.7" +gem "kaminari" gem "pry" # Use Redis adapter to run Action Cable in production # gem "redis", "~> 4.0" diff --git a/Gemfile.lock b/Gemfile.lock index 72c4162f..0f8e8b89 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -92,6 +92,18 @@ GEM actionview (>= 5.0.0) activesupport (>= 5.0.0) 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) language_server-protocol (3.17.0.3) listen (3.8.0) rb-fsevent (~> 0.10, >= 0.10.3) @@ -129,7 +141,7 @@ GEM ast (~> 2.4.1) racc pg (1.5.4) - pry (0.14.1) + pry (0.14.2) coderay (~> 1.1) method_source (~> 1.0) public_suffix (5.0.3) @@ -271,6 +283,7 @@ DEPENDENCIES byebug capybara (>= 3.26) jbuilder (~> 2.7) + kaminari listen (~> 3.3) pg (~> 1.1) pry diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index f1eb08d8..f5f0af4c 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -2,7 +2,7 @@ class ProjectsController < ApplicationController before_action :set_project, only: %i[show edit update destroy] def index - @projects = Project.order(:id) + @projects = Project.order(params[:sort]).page(params[:page]).per(3) end def show; end diff --git a/app/views/projects/index.html.erb b/app/views/projects/index.html.erb index 57b40db5..bd97171f 100644 --- a/app/views/projects/index.html.erb +++ b/app/views/projects/index.html.erb @@ -2,6 +2,10 @@

<%= link_to "New Project", new_project_path %>

+<%= link_to "Name", :sort => "name asc"%> +<%= link_to "Name reverse", :sort => "name desc"%> +<%= link_to "Description", :sort => "description asc" %> +<%= link_to "Description reverse", :sort => "description desc" %> <% @projects.each do |project| %>

<%= project.id %>. <%= link_to project.name, project_path(project) %>

@@ -10,3 +14,5 @@

<%= button_to "Destroy", project, method: :delete %>

<% end %> + +<%= paginate @projects %> diff --git a/config/initializers/kaminari_config.rb b/config/initializers/kaminari_config.rb new file mode 100644 index 00000000..986bc099 --- /dev/null +++ b/config/initializers/kaminari_config.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +Kaminari.configure do |config| + config.default_per_page = 25 + # config.max_per_page = nil + # config.window = 4 + # config.outer_window = 0 + # config.left = 0 + # config.right = 0 + # config.page_method_name = :page + # config.param_name = :page + # config.max_pages = nil + # config.params_on_first_page = false +end