# frozen_string_literal: true
# DESCRIPTION
# Reproduce generic/simple issues
#
# DOC
# https://ddnexus.github.io/pagy/playground/#repro-app
#
# BIN HELP
# pagy -h
#
# DEV USAGE
# pagy clone repro
# pagy ./repro.ru
#
# URL
# http://127.0.0.1:8000
VERSION = '43.4.4'
if VERSION != Pagy::VERSION
Warning.warn("\n>>> WARNING! '#{File.basename(__FILE__)}-#{VERSION}' running with 'pagy-#{Pagy::VERSION}'! <<< \n\n")
end
run_from_repo = Pagy::ROOT.join('pagy.gemspec').exist?
# Bundle
require 'bundler/inline'
gemfile(!run_from_repo) do
source 'https://rubygems.org'
gem 'oj'
gem 'puma'
gem 'sinatra'
end
# Edit this section adding the legacy as needed
# Pagy initializer
Pagy::OPTIONS[:client_max_limit] = 100
# Sinatra setup
require 'sinatra/base'
# Sinatra application
class PagyRepro < Sinatra::Base
include Pagy::Method
get('/javascripts/:file') do
format = params[:file].split('.').last
if format == 'js'
content_type 'application/javascript'
elsif format == 'map'
content_type 'application/json'
end
send_file Pagy::ROOT.join('javascripts', params[:file])
end
# Edit this action as needed
get '/' do
collection = MockCollection.new
@pagy, @records = pagy(collection) # simplest form
# @pagy, @records = pagy(:offset, collection, limit: 7, client_max_limit: 30)
# @pagy, @records = pagy(:countish, collection, ttl: 20)
# @pagy, @records = pagy(:countless, collection)
# @pagy, @records = pagy(Array(1..1000))
# response.headers.merge!(@pagy.headers_hash)
erb :main
end
# Views
template :layout do
<<~ERB
Pagy Repro App
Self-contained, standalone app usable to easily reproduce any pagy issue.
Versions
- Ruby: <%= RUBY_VERSION %>
- Rack: <%= Rack::RELEASE %>
- Sinatra: <%= Sinatra::VERSION %>
- Pagy: <%= Pagy::VERSION %>
Collection
@records: <%= @records.join(',') %>
@pagy.series_nav
<%= @pagy.series_nav(id: 'series-nav',
aria_label: 'Pages nav') %>
@pagy.series_nav_js (responsive)
<%= @pagy.series_nav_js(id: 'series-nav-js-responsive',
aria_label: 'Pages nav_js_responsive',
steps: { 0 => 5, 500 => 7, 600 => 9, 700 => 11 }) %>
@pagy.input_nav_js
<%= @pagy.input_nav_js(id: 'input-nav-js',
aria_label: 'Pages input_nav_js') %>
@pagy.limit_tag_js
<%= @pagy.limit_tag_js(id: 'limit-tag-js') %>
@pagy.info_tag
<%= @pagy.info_tag(id: 'pagy-info') %>
ERB
end
end
# Simple array-based collection that acts as a standard DB collection.
# Use it as a simple way to get a collection that acts as an AR scope, but without any DB
# or create an ActiveRecord class or anything else that you need instead
class MockCollection < Array
def initialize(arr = Array(1..1000))
super
@collection = clone
end
def offset(value)
@collection = self[value..] || []
self
end
def limit(value)
@collection.empty? ? [] : @collection[0, value]
end
def count(*)
size
end
end
run PagyRepro