-
Notifications
You must be signed in to change notification settings - Fork 411
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
174 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
# frozen_string_literal: true | ||
|
||
# DESCRIPTION | ||
# Showcase the keyset infinite pagination | ||
# | ||
# DOC | ||
# https://ddnexus.github.io/pagy/playground/#5-keyset-app | ||
# | ||
# BIN HELP | ||
# bundle exec pagy -h | ||
# | ||
# DEV USAGE | ||
# bundle exec pagy clone keyset_infinite | ||
# bundle exec pagy ./keyset_infinite.ru | ||
# | ||
# URL | ||
# http://0.0.0.0:8000 | ||
|
||
VERSION = '9.2.0' | ||
|
||
# Gemfile | ||
require 'bundler/inline' | ||
require 'bundler' | ||
Bundler.configure | ||
gemfile(ENV['PAGY_INSTALL_BUNDLE'] == 'true') do | ||
source 'https://rubygems.org' | ||
gem 'oj' | ||
gem 'propshaft' | ||
gem 'puma' | ||
gem 'rails', '~> 8.0' | ||
gem 'sqlite3' | ||
gem 'turbo-rails' | ||
end | ||
|
||
# require 'rails/all' # too much stuff | ||
require 'action_controller/railtie' | ||
require 'active_record' | ||
|
||
OUTPUT = Rails.env.showcase? ? IO::NULL : $stdout | ||
|
||
# Rails config | ||
class PagyKeysetInfinite < Rails::Application # :nodoc: | ||
config.root = __dir__ | ||
config.session_store :cookie_store, key: 'cookie_store_key' | ||
Rails.application.credentials.secret_key_base = 'absolute_secret' | ||
|
||
config.logger = Logger.new(OUTPUT) | ||
Rails.logger = config.logger | ||
|
||
routes.draw do | ||
root to: 'products#index' | ||
end | ||
end | ||
|
||
dir = Rails.env.development? ? '.' : Dir.pwd # app dir in dev or pwd otherwise | ||
unless File.writable?(dir) | ||
warn "ERROR: directory #{dir.inspect} is not writable (the pagy-rails-app needs to create DB files)" | ||
exit 1 | ||
end | ||
|
||
# Pagy initializer | ||
require 'pagy/extras/keyset' | ||
require 'pagy/extras/pagy' | ||
Pagy::DEFAULT.freeze | ||
|
||
# Activerecord initializer | ||
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: "#{dir}/tmp/pagy-keyset-infinite.sqlite3") | ||
ActiveRecord::Schema.define do | ||
create_table :products, force: true do |t| | ||
t.string :name | ||
end | ||
end | ||
|
||
# Models | ||
class Product < ActiveRecord::Base | ||
end | ||
|
||
# DB seed (create 10_000 simple record) | ||
id = 0 | ||
10.times do | ||
products = [] | ||
1.upto(1000) { products << { name: "Product #{id += 1}" } } | ||
Product.insert_all(products) | ||
end | ||
|
||
# Enable after seed to avoid logging 10_000 records | ||
ActiveRecord::Base.logger = Logger.new(OUTPUT) | ||
|
||
module ProductsHelper | ||
include Pagy::Frontend | ||
end | ||
|
||
# Controllers | ||
class ProductsController < ActionController::Base # :nodoc: | ||
include Rails.application.routes.url_helpers | ||
include Pagy::Backend | ||
|
||
def index | ||
@pagy, @products = pagy_keyset(Product.order(:id)) # only id for the sake of demo simplicity | ||
render inline: TEMPLATE | ||
end | ||
end | ||
|
||
TEMPLATE = <<~ERB | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<html> | ||
<head> | ||
<title>Pagy Keyset App</title> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<%= csrf_meta_tags %> | ||
<script type="importmap"> | ||
{ | ||
"imports": { | ||
"@hotwired/turbo-rails": "<%= asset_path("turbo.js") %>" | ||
} | ||
} | ||
</script> | ||
<script type="module"> | ||
import "@hotwired/turbo-rails" | ||
addEventListener("turbo:load", () => document.body.innerHTML = "Loaded with Turbo") | ||
</script> | ||
<style type="text/css"> | ||
@media screen { html, body { | ||
font-size: 1rem; | ||
line-height: 1.2s; | ||
padding: 0; | ||
margin: 0; | ||
} } | ||
body { | ||
background: white !important; | ||
margin: 0 !important; | ||
font-family: sans-serif !important; | ||
} | ||
.content { | ||
padding: 1rem 1.5rem 2rem !important; | ||
} | ||
<%== Pagy.root.join('stylesheets', 'pagy.css').read %> | ||
</style> | ||
</head> | ||
<body> | ||
<div class="content"> | ||
<h1>Pagy Keyset App</h1> | ||
<p>Self-contained, standalone Rails infinite scrolling demo app.</p> | ||
<p>Please, report the following versions in any new issue.</p> | ||
<h2>Versions</h2> | ||
<ul> | ||
<li>Ruby: <%== RUBY_VERSION %></li> | ||
<li>Rack: <%== Rack::RELEASE %></li> | ||
<li>Rails: <%== Rails.version %></li> | ||
<li>Pagy: <%== Pagy::VERSION %></li> | ||
</ul> | ||
<h3>Collection</h3> | ||
<div id="records" class="collection"> | ||
<% @products.each do |product| %> | ||
<div><%= product.name %></div> | ||
<% end %> | ||
</div> | ||
<p> | ||
<nav class="pagy" id="next" aria-label="Pagy next"> | ||
<%== pagy_next_a(@pagy, text: 'Next page >') %> | ||
</nav> | ||
</div> | ||
</body> | ||
</html> | ||
ERB | ||
|
||
run PagyKeysetInfinite |