Skip to content

Commit

Permalink
small syntax normalization
Browse files Browse the repository at this point in the history
  • Loading branch information
ddnexus committed Mar 13, 2021
1 parent 618d226 commit df3c8d6
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 15 deletions.
4 changes: 2 additions & 2 deletions lib/pagy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ def initialize(vars)
@page <= @last or raise(OverflowError.new(self), "expected :page in 1..#{@last}; got #{@page.inspect}")
@offset = @items * (@page - 1) + @outset # pagination offset + outset (initial offset)
@items = @count - ((@pages-1) * @items) if @page == @last && @count > 0 # adjust items for last non-empty page
@from = @count == 0 ? 0 : @offset+1 - @outset # page begins from item
@from = @count == 0 ? 0 : @offset + 1 - @outset # page begins from item
@to = @count == 0 ? 0 : @offset + @items - @outset # page ends to item
@prev = (@page-1 unless @page == 1) # nil if no prev page
@next = @page == @last ? (1 if @vars[:cycle]) : @page+1 # nil if no next page, 1 if :cycle
@next = @page == @last ? (1 if @vars[:cycle]) : @page + 1 # nil if no next page, 1 if :cycle
end

# Return the array of page numbers and :gap items e.g. [1, :gap, 7, 8, "9", 10, 11, :gap, 36]
Expand Down
4 changes: 2 additions & 2 deletions lib/pagy/countless.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ def finalize(fetched)
fetched == 0 && @page > 1 and raise(OverflowError.new(self), "page #{@page} got no items")
@pages = @last = (fetched > @items ? @page + 1 : @page) # set the @pages and @last
@items = fetched if fetched < @items && fetched > 0 # adjust items for last non-empty page
@from = fetched == 0 ? 0 : @offset+1 - @outset # page begins from item
@from = fetched == 0 ? 0 : @offset + 1 - @outset # page begins from item
@to = fetched == 0 ? 0 : @offset + @items - @outset # page ends to item
@prev = (@page-1 unless @page == 1) # nil if no prev page
@next = @page == @last ? (1 if @vars[:cycle]) : @page+1 # nil if no next page, 1 if :cycle
@next = @page == @last ? (1 if @vars[:cycle]) : @page + 1 # nil if no next page, 1 if :cycle
self
end

Expand Down
4 changes: 1 addition & 3 deletions lib/pagy/exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ def variable
$1.to_sym if $1
end

def value
pagy.vars[variable]
end
def value = pagy.vars[variable]
end

class OverflowError < VariableError; end
Expand Down
6 changes: 4 additions & 2 deletions lib/pagy/extras/items.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Pagy
module Items ; private

[:pagy_get_vars, :pagy_countless_get_vars, :pagy_elasticsearch_rails_get_vars, :pagy_searchkick_get_vars].each do |meth|
if Pagy::Backend.private_method_defined?(meth, true)
if Backend.private_method_defined?(meth, true)
define_method(meth) do |collection, vars|
vars[:items] ||= (items = params[vars[:items_param] || VARS[:items_param]]) && # :items from :items_param
[items.to_i, vars.key?(:max_items) ? vars[:max_items] : VARS[:max_items]].compact.min # :items capped to :max_items
Expand All @@ -32,7 +32,9 @@ module Frontend

module Items
def pagy_url_for(page, pagy, url=false)
p_vars = pagy.vars; params = request.GET.merge(p_vars[:params]); params[p_vars[:page_param].to_s] = page
p_vars = pagy.vars
params = request.GET.merge(p_vars[:params])
params[p_vars[:page_param].to_s] = page
params[p_vars[:items_param].to_s] = p_vars[:items]
"#{request.base_url if url}#{request.path}?#{Rack::Utils.build_nested_query(pagy_get_params(params))}#{p_vars[:anchor]}"
end
Expand Down
4 changes: 3 additions & 1 deletion lib/pagy/extras/shared.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ class Pagy
def sequels
steps = @vars[:steps] || {0 => @vars[:size]}
steps.key?(0) or raise(VariableError.new(self), "expected :steps to define the 0 width; got #{steps.inspect}")
sequels = {}; steps.each {|width, size| sequels[width.to_s] = series(size)}; sequels
sequels = {}
steps.each {|width, size| sequels[width.to_s] = series(size)}
sequels
end

module Frontend
Expand Down
12 changes: 7 additions & 5 deletions lib/pagy/frontend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class Pagy

PAGE_PLACEHOLDER = '__pagy_page__' # string used for search and replace, hardcoded also in the pagy.js file
PAGE_PLACEHOLDER = '__pagy_page__' # string used for search and replace, hardcoded also in the pagy.js file

# I18n static hash loaded at startup, used as default alternative to the i18n gem.
# see https://ddnexus.github.io/pagy/api/frontend#i18n
Expand All @@ -15,12 +15,14 @@ class Pagy
module Helpers
# This works with all Rack-based frameworks (Sinatra, Padrino, Rails, ...)
def pagy_url_for(page, pagy, url=false)
p_vars = pagy.vars; params = request.GET.merge(p_vars[:params]); params[p_vars[:page_param].to_s] = page
p_vars = pagy.vars
params = request.GET.merge(p_vars[:params])
params[p_vars[:page_param].to_s] = page
"#{request.base_url if url}#{request.path}?#{Rack::Utils.build_nested_query(pagy_get_params(params))}#{p_vars[:anchor]}"
end

# Sub-method called only by #pagy_url_for: here for easy customization of params by overriding
def pagy_get_params(params) params end
def pagy_get_params(params) = params
end

# All the code here has been optimized for performance: it may not look very pretty
Expand Down Expand Up @@ -49,8 +51,8 @@ def pagy_nav(pagy)
# Return examples: "Displaying items 41-60 of 324 in total" of "Displaying Products 41-60 of 324 in total"
def pagy_info(pagy, item_name=nil)
key = if (count = pagy.count) == 0 ; 'pagy.info.no_items'
else pagy.pages == 1 ? 'pagy.info.single_page' : 'pagy.info.multiple_pages'
end
else pagy.pages == 1 ? 'pagy.info.single_page' : 'pagy.info.multiple_pages'
end
pagy_t(key, item_name: item_name || pagy_t(pagy.vars[:i18n_key], count: count), count: count, from: pagy.from, to: pagy.to)
end

Expand Down

0 comments on commit df3c8d6

Please sign in to comment.