# 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.0.0' # Gemfile require 'bundler/inline' gemfile(true) do source 'https://rubygems.org' gem 'oj' gem 'puma' gem 'rails', '~> 7.1' gem 'sqlite3' 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 # 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 '/javascript' => 'pagy#javascript' 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 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 javascript file = "pagy#{'-dev' if ENV['DEBUG']}.js" render js: Pagy.root.join('javascripts', file).read 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

Collection

<% @comments.each do |comment| %>

<%= comment.body %>

<% end %>

pagy_nav

<%== pagy_nav(@pagy) %>

pagy_nav_js

<%== pagy_nav_js(@pagy) %>

pagy_combo_nav_js

<%== pagy_combo_nav_js(@pagy) %>

pagy_items_selector_js

<%== pagy_items_selector_js(@pagy) %>

pagy_info

<%== pagy_info(@pagy) %>
ERB