Skip to content

Commit 331d3cd

Browse files
committed
Bump to pagy 43.x
1 parent 77e0310 commit 331d3cd

File tree

12 files changed

+71
-37
lines changed

12 files changed

+71
-37
lines changed

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,5 @@ gem 'rubocop-performance', require: false
3030
gem 'rubocop-rake', require: false
3131
gem 'rubocop-rspec', require: false
3232
gem 'rubocop-rspec_rails', require: false
33+
34+
gem 'pagy', git: 'https://github.com/ddnexus/pagy.git', branch: 'dev'

app/views/smart_listing/_pagination.html.erb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
<%#
2-
This template is i18n-ready: if you don't use i18n, then you can replace the pagy_t
2+
This template is i18n-ready: if you don't use i18n, then you can replace the Pagy::I18n.translate
33
calls with the actual strings ("&lsaquo; Prev", "Next &rsaquo;", "&hellip;").
44
55
The link variable is set to a proc that returns the link tag.
66
Usage: link.call( page_number [, text [, extra_attributes_string ]])
77
-%>
8-
<% link = pagy_anchor(pagy, anchor_string: (remote ? 'data-remote="true"' : '')) -%>
8+
<% link = pagy.send(:a_lambda, anchor_string: (remote ? 'data-remote="true"' : '')) -%>
99
<%# -%><div class="m-lg">
1010
<%# -%><nav aria-label="pager" class="pagy-bootstrap-nav text-center" role="navigation">
1111
<%# -%> <ul class="pagination justify-content-center">
12-
<% if pagy.prev -%> <li class="page-item prev"><%== link.call(pagy.prev, pagy_t('pagy.aria_label.prev'), aria_label: 'previous') %></li>
13-
<% else -%> <li class="page-item prev disabled"><a href="#" class="page-link"><%== pagy_t('pagy.aria_label.prev') %></a></li>
12+
<% if pagy.previous -%> <li class="page-item prev"><%== link.call(pagy.previous, Pagy::I18n.translate('pagy.aria_label.previous'), aria_label: 'previous') %></li>
13+
<% else -%> <li class="page-item prev disabled"><a href="#" class="page-link"><%== Pagy::I18n.translate('pagy.aria_label.previous') %></a></li>
1414
<% end -%>
15-
<% pagy.series.each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36] -%>
15+
<% pagy.send(:series).each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36] -%>
1616
<% if item.is_a?(Integer) -%> <li class="page-item"><%== link.call(item) %></li>
1717
<% elsif item.is_a?(String) -%> <li class="page-item active"><%== link.call(item, classes: 'page-link') %></li>
18-
<% elsif item == :gap -%> <li class="page-item disabled gap"><a href="#" class="page-link"><%== pagy_t('pagy.gap') %></a></li>
18+
<% elsif item == :gap -%> <li class="page-item disabled gap"><a href="#" class="page-link"><%== Pagy::I18n.translate('pagy.gap') %></a></li>
1919
<% end -%>
2020
<% end -%>
21-
<% if pagy.next -%> <li class="page-item next"><%== link.call(pagy.next, pagy_t('pagy.aria_label.next'), aria_label: 'next') %></li>
22-
<% else -%> <li class="page-item next disabled"><a href="#" class="page-link"><%== pagy_t('pagy.aria_label.next') %></a></li>
21+
<% if pagy.next -%> <li class="page-item next"><%== link.call(pagy.next, Pagy::I18n.translate('pagy.aria_label.next'), aria_label: 'next') %></li>
22+
<% else -%> <li class="page-item next disabled"><a href="#" class="page-link"><%== Pagy::I18n.translate('pagy.aria_label.next') %></a></li>
2323
<% end -%>
2424
<%# -%> </ul>
2525
<%# -%></nav>

gemfiles/rails_7.2.gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ gem "rubocop-performance", require: false
2525
gem "rubocop-rake", require: false
2626
gem "rubocop-rspec", require: false
2727
gem "rubocop-rspec_rails", require: false
28+
gem "pagy", git: "https://github.com/ddnexus/pagy.git", branch: "dev"
2829
gem "rails", "~> 7.2.0"
2930

3031
gemspec path: "../"

gemfiles/rails_8.0.gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ gem "rubocop-performance", require: false
2525
gem "rubocop-rake", require: false
2626
gem "rubocop-rspec", require: false
2727
gem "rubocop-rspec_rails", require: false
28+
gem "pagy", git: "https://github.com/ddnexus/pagy.git", branch: "dev"
2829
gem "rails", "~> 8.0.0"
2930

3031
gemspec path: "../"

gemfiles/rails_8.1.gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ gem "rubocop-performance", require: false
2525
gem "rubocop-rake", require: false
2626
gem "rubocop-rspec", require: false
2727
gem "rubocop-rspec_rails", require: false
28+
gem "pagy", git: "https://github.com/ddnexus/pagy.git", branch: "dev"
2829
gem "rails", "~> 8.1.0"
2930

3031
gemspec path: "../"

lib/smart_listing.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
# require external dependencies
44
require 'pagy'
5-
require 'pagy/extras/array'
5+
require 'pagy/toolbox/helpers/support/series'
6+
require 'pagy/toolbox/helpers/support/a_lambda'
67
require 'zeitwerk'
78

89
# load zeitwerk

lib/smart_listing/base.rb

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22

33
module SmartListing
44
class Base
5-
include Pagy::Backend
5+
include Pagy::Method
66

77
attr_reader :name, :collection, :options, :per_page, :sort, :page, :count, :params, :pagy_collection
88

99
# Params that should not be visible in pagination links (pages, per-page, sorting, etc.)
1010
UNSAFE_PARAMS = %i[authenticity_token commit utf8 _method script_name].freeze
1111

12-
def initialize(name, collection, options = {})
12+
def initialize(name, collection, view_context, options = {})
1313
@name = name
14+
@view_context = view_context
1415
@pagy_collection = nil
1516

1617
config_profile = options.delete(:config_profile)
@@ -53,7 +54,7 @@ def setup(params, cookies)
5354
end
5455

5556
sort!(@options[:array])
56-
paginate!(@options[:array])
57+
paginate!
5758
end
5859
# rubocop:enable Layout/LineLength
5960

@@ -117,6 +118,10 @@ def base_param
117118
"#{name}_smart_listing"
118119
end
119120

121+
def request
122+
@view_context.request
123+
end
124+
120125
private
121126

122127
def get_param(key, store = @params)
@@ -162,16 +167,20 @@ def sort!(is_array)
162167
is_array ? sort_array : sort_active_record
163168
end
164169

165-
def paginate!(is_array)
170+
def paginate!
166171
return unless @options[:paginate] && @per_page > 0
167172

168-
params = pagy_options.fetch(:params, {}).merge(smart_listing_name: name)
173+
root_key = :"#{name}_smart_listing"
174+
sort = @view_context.params.dig(root_key, :sort)&.to_unsafe_h
175+
params = pagy_options.fetch(:params, {}).merge(smart_listing_name: name)
176+
querify = ->(p) { merge_params(p, params, root_key, sort) }
169177

170-
if is_array
171-
@pagy_collection, @collection = pagy_array(@collection, page: @page, limit: @per_page, params: params)
172-
else
173-
@pagy_collection, @collection = pagy(@collection, page: @page, limit: @per_page, params: params)
174-
end
178+
@pagy_collection, @collection = pagy(:offset, @collection, page: @page, limit: @per_page, root_key: root_key, querify: querify)
179+
end
180+
181+
def merge_params(pagy_params, params, root_key, sort)
182+
pagy_params[root_key][:sort] = sort if sort
183+
pagy_params.merge!(params)
175184
end
176185

177186
def sort_array

lib/smart_listing/builder.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def name
1515
end
1616

1717
def paginate(_options = {})
18-
return unless @smart_listing.pagy_collection.series.size > 1
18+
return unless @smart_listing.pagy_collection.send(:series).size > 1
1919

2020
render(partial: @smart_listing.pagy_options[:partial], locals: { pagy: @smart_listing.pagy_collection, remote: @smart_listing.remote? }).html_safe
2121
end

lib/smart_listing/controller_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def smart_listing_create(*args)
1616
view_context = respond_to?(:controller) ? controller.view_context : self.view_context
1717
options = { config_profile: view_context.smart_listing_config_profile }.merge(options)
1818

19-
list = SmartListing::Base.new(name, collection, options)
19+
list = SmartListing::Base.new(name, collection, view_context, options)
2020
list.setup(params, cookies)
2121

2222
@smart_listings ||= {}

lib/smart_listing/view_helper.rb

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
module SmartListing
44
module ViewHelper
5-
include Pagy::Frontend
65

76
def smart_listing_config_profile
87
defined?(super) ? super : :default
@@ -115,14 +114,6 @@ def smart_listing_limit_left(name)
115114
smart_listing.max_count - smart_listing.count
116115
end
117116

118-
def pagy_url_for(pagy, page, absolute: false) # rubocop:disable Lint/UnusedMethodArgument
119-
list = pagy.vars[:params][:smart_listing_name]
120-
url_params = params.to_unsafe_h.merge(pagy.vars[:params])
121-
url_params[:"#{list}_smart_listing"] ||= {}
122-
url_params[:"#{list}_smart_listing"][pagy.vars[:page_param]] = page
123-
url_for(url_params)
124-
end
125-
126117
#################################################################################################
127118
# JS helpers:
128119

0 commit comments

Comments
 (0)