Skip to content

Commit

Permalink
fix: convert per_page param to integer for will_paginate (#6)
Browse files Browse the repository at this point in the history
* fix: convert per_page param to integer for will_paginate

* chore: add specs

* chore: update version
  • Loading branch information
tian-im committed Feb 17, 2020
1 parent f34a9b3 commit d308904
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 5 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [TODO]

- Move translation from wallaby-core to here

## [0.2.1](https://github.com/wallaby-rails/wallaby-active_record/releases/tag/0.2.1) - 2020-02-17

### Changed

- fix: convert per_page param to integer for will_paginate ([#6](https://github.com/wallaby-rails/wallaby-active_record/pull/6))

## [0.2.0](https://github.com/wallaby-rails/wallaby-active_record/releases/tag/0.2.0) - 2020-02-16

### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def total
# {https://rubydoc.info/gems/wallaby-core/Wallaby/Configuration/Pagination#page_size-instance_method page_size}
# Wallaby configuration
def page_size
@params[:per].try(:to_i) || Wallaby.configuration.pagination.page_size
(@params[:per] || Wallaby.configuration.pagination.page_size).to_i
end

# @return [Integer] page number from parameters starting from 1
Expand Down
3 changes: 2 additions & 1 deletion lib/adapters/wallaby/active_record/model_service_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ def collection(params, authorizer)
# @return [ActiveRecord::Relation] paginated query
# @see Wallaby::ModelServiceProvider#paginate
def paginate(query, params)
per = params[:per] || Wallaby.configuration.pagination.page_size
# NOTE: do not take out the `.to_i` as will_paginate requires an integer `per_page`
per = (params[:per] || Wallaby.configuration.pagination.page_size).to_i
query = query.page params[:page] if query.respond_to? :page
query = query.per per if query.respond_to? :per # kaminari
query = query.per_page per if query.respond_to? :per_page # will_paginate
Expand Down
2 changes: 1 addition & 1 deletion lib/wallaby/active_record/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module Wallaby
module ActiveRecordGem
VERSION = '0.2.0' # :nodoc:
VERSION = '0.2.1' # :nodoc:
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@
describe '#paginate' do
it 'paginates the query' do
if version? '>= 6'
expect(subject.paginate(model_class.where(nil), parameters(page: 10, per: 8)).to_sql).to eq 'SELECT "all_postgres_types".* FROM "all_postgres_types" LIMIT 8 OFFSET 72'
expect(subject.paginate(model_class.where(nil), parameters(page: '10', per: '8')).to_sql).to eq 'SELECT "all_postgres_types".* FROM "all_postgres_types" LIMIT 8 OFFSET 72'
else
expect(subject.paginate(model_class.where(nil), parameters(page: 10, per: 8)).to_sql).to eq 'SELECT "all_postgres_types".* FROM "all_postgres_types" LIMIT 8 OFFSET 72'
expect(subject.paginate(model_class.where(nil), parameters(page: '10', per: '8')).to_sql).to eq 'SELECT "all_postgres_types".* FROM "all_postgres_types" LIMIT 8 OFFSET 72'
end
end
end
Expand Down

0 comments on commit d308904

Please sign in to comment.