# frozen_string_literal: true
# DESCRIPTION
# Reproduce rails related issues
#
# DOC
# https://ddnexus.github.io/pagy/playground/#rails-app
#
# BIN HELP
# pagy -h
#
# DEV USAGE
# pagy clone rails
# pagy ./rails.ru
#
# URL
# http://127.0.0.1:8000
VERSION = '43.4.4'
if VERSION != Pagy::VERSION
Warning.warn("\n>>> WARNING! '#{File.basename(__FILE__)}-#{VERSION}' running with 'pagy-#{Pagy::VERSION}'! <<< \n\n")
end
# Bundle
require 'bundler/inline'
gemfile(!Pagy::ROOT.join('pagy.gemspec').exist?) do
source 'https://rubygems.org'
gem 'oj'
gem 'puma'
gem 'rails', '~> 8.0'
gem 'sqlite3'
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
# Pagy initializer
# require Pagy::ROOT.join('apps/enable_rails_page_segment.rb') # Uncomment to test the enable_rails_page_segment.rb override
routes.draw do
root to: 'comments#index'
# get '/comments(/:page)', to: 'comments#index' # Uncomment to test the enable_rails_page_segment.rb override
get '/javascripts/:file', to: 'pagy#javascripts', file: /.*/
end
end
# Activerecord initializer
ActiveRecord::Base.logger = Logger.new(OUTPUT)
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: 'file:memdb1?mode=memory&cache=shared')
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:
# Unused model, useful to test overriding conflicts
module Calendar
end
# 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
# Controllers
class CommentsController < ActionController::Base # :nodoc:
include Rails.application.routes.url_helpers
include Pagy::Method
def index
@pagy, @comments = pagy(:offset, Comment.all, limit: 10, client_max_limit: 100)
# Reload the page in the network tab of the Chrome Inspector to check
# response.headers.merge!(@pagy.headers_hash)
render inline: TEMPLATE
end
end
# You don't need this in real rails apps (see https://ddnexus.github.io/pagy/resources/javascripts)
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.
Versions
- Ruby: <%== RUBY_VERSION %>
- Rack: <%== Rack::RELEASE %>
- Rails: <%== Rails.version %>
- Pagy: <%== Pagy::VERSION %>
Collection
<% @comments.each do |comment| %>
<%= comment.body %>
<% end %>
@pagy.series_nav
<%== @pagy.series_nav(id: 'series-nav',
aria_label: 'Pages nav') %>
@pagy.series_nav_js
<%== @pagy.series_nav_js(id: 'series-nav-js',
aria_label: 'Pages nav_js') %>
@pagy.input_nav_js
<%== @pagy.input_nav_js(id: 'input-nav-js',
aria_label: 'Pages input_nav_js') %>
@pagy.limit_tag_js
<%== @pagy.limit_tag_js(id: 'limit-tag-js') %>
@pagy.info_tag
<%== @pagy.info_tag(id: 'pagy-info') %>
ERB