Setup pagy

This commit is contained in:
Nihal Mohammed
2021-06-23 14:44:03 +05:30
committed by Matt-Yorkley
parent 6e25dec617
commit e5bdaa603a
15 changed files with 203 additions and 40 deletions

View File

@@ -52,7 +52,7 @@ gem 'devise-token_authenticatable'
gem 'jwt', '~> 2.2'
gem 'oauth2', '~> 1.4.7' # Used for Stripe Connect
gem 'kaminari', '~> 1.2.1'
gem 'pagy', '~> 4.8'
gem 'andand'
gem 'angularjs-rails', '1.5.5'

View File

@@ -346,18 +346,6 @@ GEM
multi_json (~> 1.0)
rspec (>= 2.0, < 4.0)
jwt (2.2.3)
kaminari (1.2.1)
activesupport (>= 4.1.0)
kaminari-actionview (= 1.2.1)
kaminari-activerecord (= 1.2.1)
kaminari-core (= 1.2.1)
kaminari-actionview (1.2.1)
actionview
kaminari-core (= 1.2.1)
kaminari-activerecord (1.2.1)
activerecord
kaminari-core (= 1.2.1)
kaminari-core (1.2.1)
kgio (2.11.3)
knapsack (3.1.0)
rake
@@ -403,6 +391,7 @@ GEM
multi_xml (~> 0.5)
rack (>= 1.2, < 3)
orm_adapter (0.5.0)
pagy (4.10.1)
paper_trail (12.0.0)
activerecord (>= 5.2)
request_store (~> 1.1)
@@ -750,7 +739,6 @@ DEPENDENCIES
json
json_spec (~> 1.1.4)
jwt (~> 2.2)
kaminari (~> 1.2.1)
knapsack
letter_opener (>= 1.4.1)
mimemagic (> 0.3.5)
@@ -759,6 +747,7 @@ DEPENDENCIES
oauth2 (~> 1.4.7)
ofn-qz!
order_management!
pagy (~> 4.8)
paper_trail (~> 12.0.0)
paperclip (~> 3.4.1)
paranoia (~> 2.4)

View File

@@ -35,7 +35,7 @@ angular.module("admin.orders").controller "ordersCtrl", ($scope, $timeout, Reque
$scope.setDefaults()
$scope.fetchResults()
$scope.fetchResults = (page=1) ->
$scope.fetchResults = (page) ->
startDateWithTime = $scope.appendStringIfNotEmpty($scope.q?.completed_at_gteq, ' 00:00:00')
endDateWithTime = $scope.appendStringIfNotEmpty($scope.q?.completed_at_lteq, ' 23:59:59')

View File

@@ -6,6 +6,7 @@ require "spree/api/controller_setup"
module Api
module V0
class BaseController < ActionController::Metal
include Pagy::Backend
include RawParams
include ActionController::StrongParameters
include ActionController::RespondWith

View File

@@ -67,9 +67,7 @@ module Api
@products = product_query.
ransack(query_params_with_defaults).
result.
page(params[:page] || 1).
per(params[:per_page] || DEFAULT_PER_PAGE)
result
render_paged_products @products
end
@@ -130,11 +128,12 @@ module Api
includes(variants: [:product, :default_price, :stock_items]).
where(supplier_id: producer_ids).
by_producer.by_name.
ransack(params[:q]).result.
page(params[:page]).per(params[:per_page])
ransack(params[:q]).result
end
def render_paged_products(products, product_serializer = ::Api::Admin::ProductSerializer)
pagy, products = pagy(products, items: params[:per_page] || DEFAULT_PER_PAGE)
serialized_products = ActiveModel::ArraySerializer.new(
products,
each_serializer: product_serializer
@@ -142,7 +141,7 @@ module Api
render json: {
products: serialized_products,
pagination: pagination_data(products)
pagination: pagination_data(pagy)
}
end

View File

@@ -9,6 +9,7 @@ require 'spree/core/controller_helpers/common'
require 'open_food_network/referer_parser'
class ApplicationController < ActionController::Base
include Pagy::Backend
self.responder = ApplicationResponder
respond_to :html

View File

@@ -3,12 +3,12 @@
module PaginationData
extend ActiveSupport::Concern
def pagination_data(objects)
return unless objects.respond_to? :total_count
def pagination_data(pagination)
return unless pagination.respond_to? :pages
{
results: objects.total_count,
pages: objects.total_pages,
results: pagination.count,
pages: pagination.pages,
page: (params[:page] || 1).to_i,
per_page: (params[:per_page] || default_per_page).to_i
}

View File

@@ -140,9 +140,7 @@ module Spree
@collection = @search.result.
managed_by(spree_current_user).
group_by_products_id.
includes(product_includes).
page(params[:page]).
per(Spree::Config[:admin_products_per_page])
includes(product_includes)
if params[:q][:s].include?("master_default_price_amount")
# PostgreSQL compatibility

View File

@@ -2,6 +2,7 @@
module ApplicationHelper
include RawParams
include Pagy::Frontend
def feature?(feature, user = nil)
OpenFoodNetwork::FeatureToggle.enabled?(feature, user)

View File

@@ -52,9 +52,7 @@ class ProductsRenderer
def filter_and_paginate(query)
query.
ransack(args[:q]).
result.
page(args[:page] || 1).
per(args[:per_page] || DEFAULT_PER_PAGE)
result
end
def distributed_products

View File

@@ -27,7 +27,7 @@
%td.actions
= link_to_delete user, no_text: true
- _with_routes Spree::Core::Engine.routes do
= paginate @users
= pagy_nav @pagy
- content_for :sidebar_title do
= t(".search")

View File

@@ -8,7 +8,7 @@
= button_link_to t("spree.new_zone"), new_object_url, icon: 'icon-plus', id: 'admin_new_zone_link'
- _with_routes Spree::Core::Engine.routes do
= paginate @zones
= pagy_nav @pagy
- if @zones.empty?
.no-objects-found
@@ -40,4 +40,4 @@
= link_to_edit zone, no_text: true
= link_to_delete zone, no_text: true
- _with_routes Spree::Core::Engine.routes do
= paginate @zones
= pagy_nav @pagy

View File

@@ -1,5 +0,0 @@
# Sets a maximum number of returned records when Kaminari pagination is used on a query but no
# per_page value has been passed to the #per method.
Kaminari.configure do |config|
config.max_per_page = 100
end

181
config/initializers/pagy.rb Normal file
View File

@@ -0,0 +1,181 @@
# frozen_string_literal: true
# Pagy initializer file (4.8.1)
# 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
# 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[:page] = 1 # default
Pagy::VARS[:items] = 100 # default
# Pagy::VARS[:outset] = 0 # 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[:fragment] = '#fragment' # example
# Pagy::VARS[:link_extra] = 'data-remote="true"' # example
# Pagy::VARS[:i18n_key] = 'pagy.item_name' # default
# Pagy::VARS[:cycle] = true # example
# 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'
# Elasticsearch Rails extra: Paginate `ElasticsearchRails::Results` objects
# See https://ddnexus.github.io/pagy/extras/elasticsearch_rails
# default :pagy_search method: change only if you use
# also the searchkick extra that defines the same
# VARS[:elasticsearch_rails_search_method] = :pagy_search
# require 'pagy/extras/elasticsearch_rails'
# Searchkick extra: Paginate `Searchkick::Results` objects
# See https://ddnexus.github.io/pagy/extras/searchkick
# default :pagy_search method: change only if you use
# also the elasticsearch_rails extra that defines the same
# VARS[:searchkick_search_method] = :pagy_search
# 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] = :per_page # default
Pagy::VARS[:max_items] = 100 # default
# set to false if you want to make :enable_items_extra an opt-in variable
# Pagy::VARS[:enable_items_extra] = false # default true
# 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'
# after requiring it will trim by default
# set to false if you want to make :enable_trim_extra an opt-in variable
# Pagy::VARS[:enable_trim_extra] = false # default true
# 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

View File

@@ -4,7 +4,7 @@ require 'active_merchant'
require 'acts_as_list'
require 'awesome_nested_set'
require 'cancan'
require 'kaminari'
require 'pagy'
require 'mail'
require 'paperclip'
require 'paranoia'