# frozen_string_literal: true
# Starting point to reproduce rails related pagy issues
# DEV USAGE
# pagy clone rails
# pagy ./rails.ru
# URL
# http://0.0.0.0:8000
# HELP
# pagy -h
# DOC
# https://ddnexus.github.io/pagy/playground/#2-rails-app
VERSION = '8.4.4'
# Gemfile
require 'bundler/inline'
require 'bundler'
Bundler.configure
gemfile(ENV['PAGY_INSTALL_BUNDLE'] == 'true') do
source 'https://rubygems.org'
gem 'oj'
gem 'puma'
gem 'rails'
gem 'sqlite3', '~> 1.4.0'
end
# require 'rails/all' # too much stuff
require 'action_controller/railtie'
require 'active_record'
OUTPUT = Rails.env.showcase? ? IO::NULL : $stdout
# Rails config
class PagyRails < 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: 'comments#index'
get '/javascripts/:file', to: 'pagy#javascripts', file: /.*/
end
end
# AR config
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/pagy'
require 'pagy/extras/items'
require 'pagy/extras/overflow'
Pagy::DEFAULT[:size] = [1, 4, 4, 1]
Pagy::DEFAULT[:items] = 10
Pagy::DEFAULT[:overflow] = :empty_page
Pagy::DEFAULT.freeze
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: "#{dir}/tmp/pagy-rails.sqlite3")
ActiveRecord::Schema.define do
create_table :posts, force: true do |t|
t.string :title
end
create_table :comments, force: true do |t|
t.string :body
t.integer :post_id
end
end
# Models
class Post < ActiveRecord::Base # :nodoc:
has_many :comments
end # :nodoc:
class Comment < ActiveRecord::Base # :nodoc:
belongs_to :post
end # :nodoc:
# DB seed
1.upto(11) do |pi|
Post.create(title: "Post #{pi + 1}")
end
Post.all.each_with_index do |post, pi|
2.times { |ci| Comment.create(post:, body: "Comment #{ci + 1} to Post #{pi + 1}") }
end
# Down here to avoid logging the DB seed above at each restart
ActiveRecord::Base.logger = Logger.new(OUTPUT)
# Helpers
module CommentsHelper
include Pagy::Frontend
end
# Controllers
class CommentsController < ActionController::Base # :nodoc:
include Rails.application.routes.url_helpers
include Pagy::Backend
def index
@pagy, @comments = pagy(Comment.all)
render inline: TEMPLATE
end
end
# You don't need this in real rails apps (see https://ddnexus.github.io/pagy/docs/api/javascript/setup/#2-configure)
class PagyController < ActionController::Base
def javascripts
format = params[:file].split('.').last
if format == 'js'
render js: Pagy.root.join('javascripts', params[:file]).read
elsif format == 'map'
render json: Pagy.root.join('javascripts', params[:file]).read
end
end
end
run PagyRails
TEMPLATE = <<~ERB
Pagy Rails App
Self-contained, standalone Rails app usable to easily reproduce any rails related pagy issue.
Please, report the following versions in any new issue.
Versions
- Ruby: <%== RUBY_VERSION %>
- Rack: <%== Rack::RELEASE %>
- Rails: <%== Rails.version %>
- Pagy: <%== Pagy::VERSION %>
Collection
<% @comments.each do |comment| %>
<%= comment.body %>
<% end %>
pagy_nav
<%== pagy_nav(@pagy, id: 'nav', aria_label: 'Pages nav') %>
pagy_nav_js
<%== pagy_nav_js(@pagy, id: 'nav-js', aria_label: 'Pages nav_js') %>
pagy_combo_nav_js
<%== pagy_combo_nav_js(@pagy, id: 'combo-nav-js', aria_label: 'Pages combo_nav_js') %>
pagy_items_selector_js
<%== pagy_items_selector_js(@pagy, id: 'items-selector-js') %>
pagy_info
<%== pagy_info(@pagy, id: 'pagy-info') %>
ERB