Skip to content

Commit

Permalink
count (and page) in *get_variables methods are set only when nil (alt…
Browse files Browse the repository at this point in the history
…ernative implementation of #112)
  • Loading branch information
ddnexus committed Dec 8, 2018
1 parent ed8b7dd commit 0723228
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 18 deletions.
5 changes: 3 additions & 2 deletions docs/how-to.md
Original file line number Diff line number Diff line change
Expand Up @@ -376,8 +376,9 @@ Pagy gets the collection count through its `pagy_get_vars` method, so you can ov
```ruby
# in your controller: override the pagy_get_vars method so it will call your cache_count method
def pagy_get_vars(collection, vars)
{ count: cache_count(collection),
page: params[vars[:page_param]||Pagy::VARS[:page_param]] }.merge!(vars)
vars[:count] ||= cache_count(collection)
vars[:page] ||= params[ vars[:page_param] || Pagy::VARS[:page_param] ]
vars
end

# add Rails.cache wrapper around the count call
Expand Down
7 changes: 3 additions & 4 deletions lib/pagy/backend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ def pagy(collection, vars={})

# Sub-method called only by #pagy: here for easy customization of variables by overriding
def pagy_get_vars(collection, vars)
count = collection.count(:all) # work with AR collections: other ORMs may need to change this
count = count.count if count.is_a?(Hash) # fix for the AR grouping count inconsistency (Hash instead of Integer)
# Return the merged variables to initialize the Pagy object
{ count: count, page: params[vars[:page_param]||VARS[:page_param]] }.merge!(vars)
vars[:count] ||= (c = collection.count(:all)).is_a?(Hash) ? c.count : c
vars[:page] ||= params[ vars[:page_param] || VARS[:page_param] ]
vars
end

# Sub-method called only by #pagy: here for easy customization of record-extraction by overriding
Expand Down
7 changes: 4 additions & 3 deletions lib/pagy/extras/array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ def pagy_array(array, vars={})
return pagy, array[pagy.offset, pagy.items]
end

# Sub-method called only by #pagy_array: here for easy customization of variables by overriding
def pagy_array_get_vars(array, vars)
# Return the merged variables to initialize the Pagy object
{ count: array.count,
page: params[vars[:page_param]||VARS[:page_param]] }.merge!(vars)
vars[:count] ||= array.count
vars[:page] ||= params[ vars[:page_param] || VARS[:page_param] ]
vars
end

end
Expand Down
4 changes: 2 additions & 2 deletions lib/pagy/extras/countless.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ def pagy_countless(collection, vars={})

# Sub-method called only by #pagy_countless: here for easy customization of variables by overriding
def pagy_countless_get_vars(_collection, vars)
# Return the merged variables to initialize the Pagy object
{ page: params[vars[:page_param]||VARS[:page_param]] }.merge!(vars)
vars[:page] ||= params[ vars[:page_param] || VARS[:page_param] ]
vars
end

# Sub-method called only by #pagy_countless: here for easy customization of record-extraction by overriding
Expand Down
7 changes: 4 additions & 3 deletions lib/pagy/extras/elasticsearch_rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ def pagy_elasticsearch_rails(results, vars={})
return pagy, results.offset(pagy.offset).limit(pagy.items)
end

# Sub-method called only by #pagy_elasticsearch_rails: here for easy customization of variables by overriding
def pagy_elasticsearch_rails_get_vars(results, vars)
# Return the merged variables to initialize the Pagy object
{ count: results.total,
page: (params[:page] || 1)}.merge!(vars)
vars[:count] ||= results.total
vars[:page] ||= params[:page] || 1
vars
end
end
end
9 changes: 5 additions & 4 deletions lib/pagy/extras/searchkick.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ def pagy_searchkick(results, vars={})
return pagy, results
end

# Sub-method called only by #pagy_searchkick: here for easy customization of variables by overriding
def pagy_searchkick_get_vars(results, vars)
# Return the merged variables to initialize the Pagy object
{ count: results.total_count,
page: results.options[:page],
items: results.options[:per_page] }.merge!(vars)
vars[:count] ||= results.total_count
vars[:page] ||= results.options[:page]
vars[:items] ||= results.options[:per_page]
vars
end
end
end
9 changes: 9 additions & 0 deletions test/pagy/backend_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@
merged[:link_extra].must_equal 'X'
end

it 'overrides count and page' do
vars = {count: 10, page: 32}
merged = backend.send :pagy_get_vars, @collection, vars
merged.keys.must_include :count
merged[:count].must_equal 10
merged.keys.must_include :page
merged[:page].must_equal 32
end

end

describe "#pagy_get_items" do
Expand Down

0 comments on commit 0723228

Please sign in to comment.