Last active
August 18, 2021 18:27
-
-
Save sunny/f261dce70f382b4af73c461430de2e6d to your computer and use it in GitHub Desktop.
Pagy Standalone to test trim + custom URLs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
# Self-contained, standalone Sinatra app usable to play with pagy | |
# and/or easily reproduce any pagy issue. | |
# Copy this file in your own machine and | |
# ensure rack is installed (or `gem install rack`) | |
# USAGE: | |
# rackup -o 0.0.0.0 -p 8080 pagy_standalone_app.ru | |
# ADVANCED USAGE (with automatic app reload if you edit it): | |
# gem install rerun | |
# rerun -- rackup -o 0.0.0.0 -p 8080 pagy_standalone_app.ru | |
# Point your browser at http://0.0.0.0:8080 | |
# read the comment below to edit this app | |
require 'bundler/inline' | |
# edit this gemfile declaration as you need | |
# and ensure to use gems updated to the latest versions | |
gemfile true do | |
source 'https://rubygems.org' | |
gem 'oj' | |
gem 'rack' | |
gem 'pagy' | |
gem 'puma' | |
gem 'sinatra' | |
gem 'sinatra-contrib' | |
end | |
# edit this section adding/removing the extras and Pagy::VARS as needed | |
# pagy initializer | |
require 'pagy/extras/navs' | |
require 'pagy/extras/items' | |
require 'pagy/extras/trim' | |
Pagy::VARS[:trim] = true # opt-in trim | |
# sinatra application | |
require 'sinatra/base' | |
class PagyStandaloneApp < Sinatra::Base | |
configure do | |
enable :inline_templates | |
end | |
include Pagy::Backend | |
# edit this section adding your own helpers as needed | |
helpers do | |
include Pagy::Frontend | |
def pagy_url_for(_pagy, page) | |
page == 1 ? "/" : "/page/#{page}" | |
end | |
def pagy_trim(_pagy, link) | |
link.to_s.sub(/\bpage\/1\b/, '') | |
end | |
end | |
get '/pagy.js' do | |
content_type 'application/javascript' | |
send_file Pagy.root.join('javascripts', 'pagy.js') | |
end | |
# edit this action as needed | |
get '/' do | |
collection = MockCollection.new | |
@pagy, @records = pagy(collection) | |
erb :pagy_demo # template available in the __END__ section as @@ pagy_demo | |
end | |
get '/page/:page' do | |
collection = MockCollection.new | |
@pagy, @records = pagy(collection) | |
erb :pagy_demo # template available in the __END__ section as @@ pagy_demo | |
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 a 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..-1] | |
self | |
end | |
def limit(value) | |
@collection[0, value] | |
end | |
def count(*) | |
size | |
end | |
end | |
run PagyStandaloneApp | |
__END__ | |
@@ layout | |
<html> | |
<head> | |
<script type="application/javascript" src="/pagy.js"></script> | |
<script type="application/javascript"> | |
window.addEventListener("load", Pagy.init); | |
</script> | |
</head> | |
<body> | |
<%= yield %> | |
</body> | |
</html> | |
@@ pagy_demo | |
<h3>Pagy Standalone Application</h3> | |
<p> Self-contained, standalone Sinatra app usable to play with pagy and/or easily reproduce any pagy issue.</p> | |
<p>Please, report the following versions in any new issue.</p> | |
<h4>Versions</h4> | |
<ul> | |
<li>Ruby: <%= RUBY_VERSION %></li> | |
<li>Rack: <%= Rack::RELEASE %></li> | |
<li>Sinatra: <%= Sinatra::VERSION %></li> | |
<li>Pagy: <%= Pagy::VERSION %></li> | |
</ul> | |
<hr> | |
<h4>Pagy Helpers</h4> | |
<p>@records: <%= @records.join(',') %></p> | |
<br> | |
<%= pagy_nav(@pagy) %> | |
<br> | |
<%= pagy_nav_js(@pagy) %> | |
<br> | |
<%= pagy_combo_nav_js(@pagy) %> | |
<br> | |
<%= pagy_items_selector_js(@pagy) %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment