-
-
Save caseyprovost/f1e7cc2348d8d64b8665a88c4f68099f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Pagination | |
extend ActiveSupport::Concern | |
included do | |
before_action :set_v2_default_pagination_params, if: -> do | |
request.fullpath.include?('v2') && action_name == 'index' | |
end | |
end | |
def paginated_collection(collection) | |
collection.page(params[:page]).per(params[:per_page]) | |
end | |
def v2_paginated_collection(resources) | |
data = resources.scope.instance_variable_get(:@unpaginated_object) | |
data.page(params[:page][:number]).per(params[:page][:size]) | |
end | |
def set_v2_default_pagination_params | |
params[:page] ||= { | |
number: 1, | |
size: per_page | |
} | |
end | |
def pagination_links(collection, class_name = nil) | |
{ | |
"self": current_page_link(collection, class_name.to_s), | |
"first": first_page_link(collection, class_name.to_s), | |
"prev": prev_page_link(collection, class_name.to_s), | |
"next": next_page_link(collection, class_name.to_s), | |
"last": last_page_link(collection, class_name.to_s) | |
} | |
end | |
def current_page_link(collection, class_name = nil) | |
pages = { page: collection.current_page, per_page: per_page } | |
url_for_collection(collection, class_name, pages) | |
end | |
def first_page_link(collection, class_name = nil) | |
pages = { page: 1, per_page: per_page } | |
url_for_collection(collection, class_name, pages) | |
end | |
def last_page_link(collection, class_name = nil) | |
pages = { page: collection.total_pages, per_page: per_page } | |
url_for_collection(collection, class_name, pages) | |
end | |
def prev_page_link(collection, class_name = nil) | |
potential_prev_page = collection.current_page - 1 | |
prev_page = potential_prev_page >= 1 ? potential_prev_page : nil | |
return nil if prev_page.nil? | |
pages = { page: prev_page, per_page: per_page } | |
url_for_collection(collection, class_name, pages) | |
end | |
def next_page_link(collection, class_name = nil) | |
potential_next_page = collection.current_page + 1 | |
next_page = potential_next_page <= collection.total_pages ? potential_next_page : nil | |
return nil if next_page.nil? | |
pages = { page: next_page, per_page: per_page } | |
url_for_collection(collection, class_name, pages) | |
end | |
def url_for_collection(collection, class_name = nil, pagination = {}) | |
if collection.exists? | |
model_name = collection.first.class.name | |
else | |
model_name = class_name | |
end | |
resource = model_name.underscore.downcase.pluralize | |
send("#{resource}_url", incoming_params.merge(pagination)) | |
end | |
def pages_meta(collection) | |
{ | |
total_count: collection.total_count, | |
total_pages: collection.total_pages, | |
current_page: collection.current_page, | |
per_page: per_page | |
}.transform_keys { |k| k.to_s.gsub('_', '-') } | |
end | |
def per_page | |
return @per_page if @per_page | |
@per_page = params[:per_page].to_i | |
if @per_page > Kaminari.config[:default_per_page] || @per_page == 0 | |
@per_page = Kaminari.config[:default_per_page] | |
else | |
@per_page | |
end | |
end | |
def incoming_params | |
params.slice(:include, :filter).to_unsafe_h | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment