diff --git a/lib/pagy.rb b/lib/pagy.rb index dbfa571aa..95ed36226 100644 --- a/lib/pagy.rb +++ b/lib/pagy.rb @@ -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] diff --git a/lib/pagy/countless.rb b/lib/pagy/countless.rb index 35abdb06d..69d9f02f8 100644 --- a/lib/pagy/countless.rb +++ b/lib/pagy/countless.rb @@ -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 diff --git a/lib/pagy/exceptions.rb b/lib/pagy/exceptions.rb index ef83f6003..ebfb88c38 100644 --- a/lib/pagy/exceptions.rb +++ b/lib/pagy/exceptions.rb @@ -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 diff --git a/lib/pagy/extras/items.rb b/lib/pagy/extras/items.rb index e262a5270..646e627b9 100644 --- a/lib/pagy/extras/items.rb +++ b/lib/pagy/extras/items.rb @@ -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 @@ -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 diff --git a/lib/pagy/extras/shared.rb b/lib/pagy/extras/shared.rb index 57c1d3b76..98172b948 100644 --- a/lib/pagy/extras/shared.rb +++ b/lib/pagy/extras/shared.rb @@ -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 diff --git a/lib/pagy/frontend.rb b/lib/pagy/frontend.rb index 12df6b529..f61554784 100644 --- a/lib/pagy/frontend.rb +++ b/lib/pagy/frontend.rb @@ -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 @@ -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 @@ -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