Skip to content

Commit

Permalink
WIP: Add keyset_infinite app
Browse files Browse the repository at this point in the history
  • Loading branch information
ddnexus committed Nov 8, 2024
1 parent cd7b243 commit 7dd8e1a
Showing 1 changed file with 174 additions and 0 deletions.
174 changes: 174 additions & 0 deletions gem/apps/keyset_infinite.ru
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 &gt;') %>
</nav>
</div>
</body>
</html>
ERB

run PagyKeysetInfinite

0 comments on commit 7dd8e1a

Please sign in to comment.