From ece4daa299ac35ae77311b3ef47fc880f5ee8009 Mon Sep 17 00:00:00 2001 From: Domizio Demichelis Date: Sun, 11 Apr 2021 12:46:19 +0700 Subject: [PATCH] minor code restyling --- .simplecov | 2 ++ lib/pagy.rb | 3 ++- lib/pagy/extras/items.rb | 2 ++ lib/pagy/frontend.rb | 16 +++++++--------- pagy.gemspec | 4 +++- 5 files changed, 16 insertions(+), 11 deletions(-) diff --git a/.simplecov b/.simplecov index d5c1d493a..88b7636a1 100644 --- a/.simplecov +++ b/.simplecov @@ -1,3 +1,5 @@ +# frozen_string_literal: true + SimpleCov.start do if ENV['RUN_CODECOV'] == 'true' require 'codecov' diff --git a/lib/pagy.rb b/lib/pagy.rb index e90e5bc5f..6e65826ab 100644 --- a/lib/pagy.rb +++ b/lib/pagy.rb @@ -29,7 +29,8 @@ def initialize(vars) unless @vars[name] && instance_variable_set(:"@#{name}", @vars[name].to_i) >= min end @pages = @last = [(@count.to_f / @items).ceil, 1].max - raise OverflowError.new(self), "expected :page in 1..#{@last}; got #{@page.inspect}" if @page > @last + raise OverflowError.new(self), "expected :page in 1..#{@last}; got #{@page.inspect}" \ + if @page > @last @offset = @items * (@page - 1) + @outset @items = @count - ((@pages-1) * @items) if @page == @last && @count.positive? diff --git a/lib/pagy/extras/items.rb b/lib/pagy/extras/items.rb index 99188d063..f58309e15 100644 --- a/lib/pagy/extras/items.rb +++ b/lib/pagy/extras/items.rb @@ -36,6 +36,7 @@ module UseItemsExtra module Frontend module UseItemsExtra + def pagy_url_for(page, pagy, url=nil) p_vars = pagy.vars params = request.GET.merge(p_vars[:params]) @@ -43,6 +44,7 @@ def pagy_url_for(page, pagy, url=nil) 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 + end prepend UseItemsExtra diff --git a/lib/pagy/frontend.rb b/lib/pagy/frontend.rb index 18bb27417..50870d8f9 100644 --- a/lib/pagy/frontend.rb +++ b/lib/pagy/frontend.rb @@ -65,9 +65,7 @@ def pagy_info(pagy, item_name=nil) else '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 + count: count, from: pagy.from, to: pagy.to end # Returns a performance optimized proc to generate the HTML links @@ -77,16 +75,16 @@ def pagy_link_proc(pagy, link_extra='') p_next = pagy.next left, right = %(" + %(#{left}#{num}#{right}#{ case num + when p_prev then ' rel="prev"' + when p_next then ' rel="next"' + else '' + end } #{extra}>#{text}) end end # Similar to I18n.t: just ~18x faster using ~10x less memory - # (@pagy_locale explicitly initilized in order to avoid warning) + # (@pagy_locale explicitly initialized in order to avoid warning) def pagy_t(key, **opts) Pagy::I18n.t @pagy_locale||=nil, key, **opts end diff --git a/pagy.gemspec b/pagy.gemspec index 51a2c3d29..b1afe15bd 100644 --- a/pagy.gemspec +++ b/pagy.gemspec @@ -1,4 +1,6 @@ -lib = File.expand_path('../lib', __FILE__) +# frozen_string_literal: true + +lib = File.expand_path('lib', __dir__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require 'pagy'