From 2a59c19a38e1620f66faddbfe588dce8dc50c7f5 Mon Sep 17 00:00:00 2001 From: Yannick Date: Mon, 27 Apr 2020 15:31:51 +0200 Subject: [PATCH 1/2] Add form submissions pagination Added pagy gem to enable pagination and added it's bulma template to the form submissions page --- Gemfile | 1 + Gemfile.lock | 2 + app/controllers/application_controller.rb | 1 + app/controllers/forms_controller.rb | 2 +- app/helpers/application_helper.rb | 1 + app/views/forms/show.html.erb | 1 + config/initializers/pagy.rb | 170 ++++++++++++++++++++++ 7 files changed, 177 insertions(+), 1 deletion(-) create mode 100644 config/initializers/pagy.rb diff --git a/Gemfile b/Gemfile index 569b399..52c36aa 100644 --- a/Gemfile +++ b/Gemfile @@ -30,6 +30,7 @@ gem 'rack-cors' gem 'sentry-raven' gem 'sequenced' gem 'sorcery' +gem 'pagy', '~> 3.8' group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console diff --git a/Gemfile.lock b/Gemfile.lock index dedd391..31e5697 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -153,6 +153,7 @@ GEM multi_xml (~> 0.5) rack (>= 1.2, < 3) os (1.1.0) + pagy (3.8.0) pg (1.2.3) public_suffix (4.0.4) puma (4.3.3) @@ -268,6 +269,7 @@ DEPENDENCIES jbuilder listen lockbox + pagy (~> 3.8) pg puma rack-cors diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 1be493f..569d38a 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,5 +1,6 @@ class ApplicationController < ActionController::Base helper_method :current_user, :logged_in? + include Pagy::Backend def require_login redirect_to login_url unless current_user.present? diff --git a/app/controllers/forms_controller.rb b/app/controllers/forms_controller.rb index 095ede0..7f0a6f0 100644 --- a/app/controllers/forms_controller.rb +++ b/app/controllers/forms_controller.rb @@ -10,7 +10,7 @@ class FormsController < ApplicationController def show @form = current_user.forms.find_by!(token: params[:id]) - @submissions = @form.submissions + @pagy, @submissions = pagy(@form.submissions, items: 25) end def index diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index de6be79..41244f6 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,2 +1,3 @@ module ApplicationHelper + include Pagy::Frontend end diff --git a/app/views/forms/show.html.erb b/app/views/forms/show.html.erb index 1f2270d..df66cd8 100644 --- a/app/views/forms/show.html.erb +++ b/app/views/forms/show.html.erb @@ -67,6 +67,7 @@ + <%== pagy_bulma_nav(@pagy) %> <% end %> diff --git a/config/initializers/pagy.rb b/config/initializers/pagy.rb new file mode 100644 index 0000000..3b416b3 --- /dev/null +++ b/config/initializers/pagy.rb @@ -0,0 +1,170 @@ +# encoding: utf-8 +# frozen_string_literal: true + +# Pagy initializer file (3.8.0) +# Customize only what you really need and notice that Pagy works also without any of the following lines. +# Should you just cherry pick part of this file, please maintain the require-order of the extras + + +# Extras +# See https://ddnexus.github.io/pagy/extras + + +# Backend Extras + +# Array extra: Paginate arrays efficiently, avoiding expensive array-wrapping and without overriding +# See https://ddnexus.github.io/pagy/extras/array +# require 'pagy/extras/array' + +# Countless extra: Paginate without any count, saving one query per rendering +# See https://ddnexus.github.io/pagy/extras/countless +# require 'pagy/extras/countless' +# Pagy::VARS[:cycle] = false # default + +# Elasticsearch Rails extra: Paginate `ElasticsearchRails::Results` objects +# See https://ddnexus.github.io/pagy/extras/elasticsearch_rails +# require 'pagy/extras/elasticsearch_rails' + +# Searchkick extra: Paginate `Searchkick::Results` objects +# See https://ddnexus.github.io/pagy/extras/searchkick +# require 'pagy/extras/searchkick' + + +# Frontend Extras + +# Bootstrap extra: Add nav, nav_js and combo_nav_js helpers and templates for Bootstrap pagination +# See https://ddnexus.github.io/pagy/extras/bootstrap +# require 'pagy/extras/bootstrap' + +# Bulma extra: Add nav, nav_js and combo_nav_js helpers and templates for Bulma pagination +# See https://ddnexus.github.io/pagy/extras/bulma +require 'pagy/extras/bulma' + +# Foundation extra: Add nav, nav_js and combo_nav_js helpers and templates for Foundation pagination +# See https://ddnexus.github.io/pagy/extras/foundation +# require 'pagy/extras/foundation' + +# Materialize extra: Add nav, nav_js and combo_nav_js helpers for Materialize pagination +# See https://ddnexus.github.io/pagy/extras/materialize +# require 'pagy/extras/materialize' + +# Navs extra: Add nav_js and combo_nav_js javascript helpers +# Notice: the other frontend extras add their own framework-styled versions, +# so require this extra only if you need the unstyled version +# See https://ddnexus.github.io/pagy/extras/navs +# require 'pagy/extras/navs' + +# Semantic extra: Add nav, nav_js and combo_nav_js helpers for Semantic UI pagination +# See https://ddnexus.github.io/pagy/extras/semantic +# require 'pagy/extras/semantic' + +# UIkit extra: Add nav helper and templates for UIkit pagination +# See https://ddnexus.github.io/pagy/extras/uikit +# require 'pagy/extras/uikit' + +# Multi size var used by the *_nav_js helpers +# See https://ddnexus.github.io/pagy/extras/navs#steps +# Pagy::VARS[:steps] = { 0 => [2,3,3,2], 540 => [3,5,5,3], 720 => [5,7,7,5] } # example + + +# Feature Extras + +# Headers extra: http response headers (and other helpers) useful for API pagination +# See http://ddnexus.github.io/pagy/extras/headers +# require 'pagy/extras/headers' +# Pagy::VARS[:headers] = { page: 'Current-Page', items: 'Page-Items', count: 'Total-Count', pages: 'Total-Pages' } # default + +# Support extra: Extra support for features like: incremental, infinite, auto-scroll pagination +# See https://ddnexus.github.io/pagy/extras/support +# require 'pagy/extras/support' + +# Items extra: Allow the client to request a custom number of items per page with an optional selector UI +# See https://ddnexus.github.io/pagy/extras/items +# require 'pagy/extras/items' +# Pagy::VARS[:items_param] = :items # default +# Pagy::VARS[:max_items] = 100 # default + +# Overflow extra: Allow for easy handling of overflowing pages +# See https://ddnexus.github.io/pagy/extras/overflow +# require 'pagy/extras/overflow' +# Pagy::VARS[:overflow] = :empty_page # default (other options: :last_page and :exception) + +# Metadata extra: Provides the pagination metadata to Javascript frameworks like Vue.js, react.js, etc. +# See https://ddnexus.github.io/pagy/extras/metadata +# you must require the shared internal extra (BEFORE the metadata extra) ONLY if you need also the :sequels +# require 'pagy/extras/shared' +# require 'pagy/extras/metadata' +# For performance reason, you should explicitly set ONLY the metadata you use in the frontend +# Pagy::VARS[:metadata] = [:scaffold_url, :count, :page, :prev, :next, :last] # example + +# Trim extra: Remove the page=1 param from links +# See https://ddnexus.github.io/pagy/extras/trim +# require 'pagy/extras/trim' + + + +# Pagy Variables +# See https://ddnexus.github.io/pagy/api/pagy#variables +# All the Pagy::VARS are set for all the Pagy instances but can be overridden +# per instance by just passing them to Pagy.new or the #pagy controller method + + +# Instance variables +# See https://ddnexus.github.io/pagy/api/pagy#instance-variables +# Pagy::VARS[:items] = 20 # default + + +# Other Variables +# See https://ddnexus.github.io/pagy/api/pagy#other-variables +# Pagy::VARS[:size] = [1,4,4,1] # default +# Pagy::VARS[:page_param] = :page # default +# Pagy::VARS[:params] = {} # default +# Pagy::VARS[:anchor] = '#anchor' # example +# Pagy::VARS[:link_extra] = 'data-remote="true"' # example + + +# Rails + +# Rails: extras assets path required by the helpers that use javascript +# (pagy*_nav_js, pagy*_combo_nav_js, and pagy_items_selector_js) +# See https://ddnexus.github.io/pagy/extras#javascript +# Rails.application.config.assets.paths << Pagy.root.join('javascripts') + + +# I18n + +# Pagy internal I18n: ~18x faster using ~10x less memory than the i18n gem +# See https://ddnexus.github.io/pagy/api/frontend#i18n +# Notice: No need to configure anything in this section if your app uses only "en" +# or if you use the i18n extra below +# +# Examples: +# load the "de" built-in locale: +# Pagy::I18n.load(locale: 'de') +# +# load the "de" locale defined in the custom file at :filepath: +# Pagy::I18n.load(locale: 'de', filepath: 'path/to/pagy-de.yml') +# +# load the "de", "en" and "es" built-in locales: +# (the first passed :locale will be used also as the default_locale) +# Pagy::I18n.load({locale: 'de'}, +# {locale: 'en'}, +# {locale: 'es'}) +# +# load the "en" built-in locale, a custom "es" locale, +# and a totally custom locale complete with a custom :pluralize proc: +# (the first passed :locale will be used also as the default_locale) +# Pagy::I18n.load({locale: 'en'}, +# {locale: 'es', filepath: 'path/to/pagy-es.yml'}, +# {locale: 'xyz', # not built-in +# filepath: 'path/to/pagy-xyz.yml', +# pluralize: lambda{|count| ... } ) + + +# I18n extra: uses the standard i18n gem which is ~18x slower using ~10x more memory +# than the default pagy internal i18n (see above) +# See https://ddnexus.github.io/pagy/extras/i18n +# require 'pagy/extras/i18n' + +# Default i18n key +# Pagy::VARS[:i18n_key] = 'pagy.item_name' # default \ No newline at end of file From a5be6af3f77ed9a5805cb3af44ea37bd7b5683f6 Mon Sep 17 00:00:00 2001 From: Yannick Date: Tue, 28 Apr 2020 01:10:45 +0200 Subject: [PATCH 2/2] Sort submissions by latest --- Gemfile | 2 +- Gemfile.lock | 2 +- app/controllers/forms_controller.rb | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Gemfile b/Gemfile index 52c36aa..2293fd7 100644 --- a/Gemfile +++ b/Gemfile @@ -30,7 +30,7 @@ gem 'rack-cors' gem 'sentry-raven' gem 'sequenced' gem 'sorcery' -gem 'pagy', '~> 3.8' +gem 'pagy' group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console diff --git a/Gemfile.lock b/Gemfile.lock index 31e5697..3dca72a 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -269,7 +269,7 @@ DEPENDENCIES jbuilder listen lockbox - pagy (~> 3.8) + pagy pg puma rack-cors diff --git a/app/controllers/forms_controller.rb b/app/controllers/forms_controller.rb index 7f0a6f0..7cd7f96 100644 --- a/app/controllers/forms_controller.rb +++ b/app/controllers/forms_controller.rb @@ -10,7 +10,7 @@ class FormsController < ApplicationController def show @form = current_user.forms.find_by!(token: params[:id]) - @pagy, @submissions = pagy(@form.submissions, items: 25) + @pagy, @submissions = pagy(@form.submissions.order(created_at: :desc), items: 25) end def index