Skip to content

Commit

Permalink
minor code restyling
Browse files Browse the repository at this point in the history
  • Loading branch information
ddnexus committed Apr 19, 2021
1 parent 0e3a09d commit ece4daa
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .simplecov
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

SimpleCov.start do
if ENV['RUN_CODECOV'] == 'true'
require 'codecov'
Expand Down
3 changes: 2 additions & 1 deletion lib/pagy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
2 changes: 2 additions & 0 deletions lib/pagy/extras/items.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@ 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])
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

end
prepend UseItemsExtra

Expand Down
16 changes: 7 additions & 9 deletions lib/pagy/frontend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -77,16 +75,16 @@ def pagy_link_proc(pagy, link_extra='')
p_next = pagy.next
left, right = %(<a href="#{pagy_url_for(PAGE_PLACEHOLDER, pagy)}" #{pagy.vars[:link_extra]} #{link_extra}).split(PAGE_PLACEHOLDER, 2)
lambda do |num, text=num, extra=''|
"#{left}#{num}#{right}#{ case num
when p_prev then ' rel="prev"'
when p_next then ' rel="next"'
else ''
end } #{extra}>#{text}</a>"
%(#{left}#{num}#{right}#{ case num
when p_prev then ' rel="prev"'
when p_next then ' rel="next"'
else ''
end } #{extra}>#{text}</a>)
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
Expand Down
4 changes: 3 additions & 1 deletion pagy.gemspec
Original file line number Diff line number Diff line change
@@ -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'

Expand Down

0 comments on commit ece4daa

Please sign in to comment.