Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add kaminari paginator #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ This template uses [jsonapi-serializer](https://github.com/jsonapi-serializer/js
3. Foreman
For running multiple processes

4. [Kaminari](https://github.com/kaminari/kaminari) paginator

### Authentication
__Registration__

Expand Down
5 changes: 3 additions & 2 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ class ApplicationController < ActionController::API
# acts_as_token_authentication_handler_for User
around_action :handle_errors

def render_api_success(serializer, obj, _options = {})
render json: serializer.new(obj).serializable_hash
def render_api_success(serializer, obj, options = {})
options = Kaminari.meta_pagination(obj, options) if defined? obj.current_page
render json: serializer.new(obj, options).serializable_hash
end

def render_api_error(messages, code)
Expand Down
20 changes: 20 additions & 0 deletions app/services/kaminari.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module Kaminari
def self.meta_pagination(paginated_obj, options = {})
options[:meta] = {} unless options.key?(:meta)
meta_options = options[:meta].merge(generate_pagination(paginated_obj))
options[:meta] = meta_options
options
end

def self.generate_pagination(paginated_obj)
{
pagination: {
current_page: paginated_obj.current_page,
prev_page: paginated_obj.prev_page,
next_page: paginated_obj.next_page,
total_pages: paginated_obj.total_pages,
total_count: paginated_obj.total_count
}
}
end
end
1 change: 1 addition & 0 deletions template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def add_template_repository_to_source_path

# Gems
def add_gems
gem 'kaminari'
gem 'devise', '~> 4.7', '>= 4.7.1'
gem 'jwt', '~> 2.2', '>= 2.2.1'
gem 'jsonapi-serializer', '~> 2.1.0'
Expand Down