Module: RocketPants::Pagination
- Defined in:
- lib/rocket_pants/pagination.rb,
lib/rocket_pants/pagination/version.rb,
lib/rocket_pants/pagination/swagger_schema.rb
Overview
Pagination support for RocketPants
Include this module in your RocketPants controllers to add pagination support.
Defined Under Namespace
Modules: SwaggerSchema
Constant Summary collapse
- RESERVED_PAGINATION_KEYS =
Reserved root keys
[:count, :pagination]
- VERSION =
'1.0.0'
Instance Method Summary collapse
-
#expose_with_pagination(hash) ⇒ Object
Exposes the given collection with pagination metadata.
-
#paginate(relation, options = {}) ⇒ ActiveRecord::Relation
Paginates the given relation.
-
#paginate_and_expose(hash) ⇒ Object
Paginates and exposes the given collection with pagination metadata.
Instance Method Details
#expose_with_pagination(hash) ⇒ Object
Exposes the given collection with pagination metadata.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/rocket_pants/pagination.rb', line 46 def expose_with_pagination(hash) root_key, collection = extract_pagination_elements_from hash response = { "#{root_key}" => ActiveModel::ArraySerializer.new(collection), count: collection.count, pagination: { pages: collection.total_pages, current: collection.current_page, count: collection.count, per_page: collection.per_page, previous: collection.previous_page, next: collection.next_page } } expose response end |
#paginate(relation, options = {}) ⇒ ActiveRecord::Relation
Paginates the given relation.
The page param is ‘page’ by default.
34 35 36 |
# File 'lib/rocket_pants/pagination.rb', line 34 def paginate(relation, = {}) relation.paginate({ page: params[:page] }.merge()) end |
#paginate_and_expose(hash) ⇒ Object
Paginates and exposes the given collection with pagination metadata.
Basically, just calls #paginate, then #expose_with_pagination.
75 76 77 78 |
# File 'lib/rocket_pants/pagination.rb', line 75 def paginate_and_expose(hash) root_key, collection = extract_pagination_elements_from hash expose_with_pagination "#{root_key}" => paginate(collection) end |