Last active
October 11, 2015 21:27
-
-
Save jims3ne1/3921771 to your computer and use it in GitHub Desktop.
pagination setter in the base controller
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
class Api::BaseController < ActionController::Base | |
protected | |
def set_pagination(name, options = {}) | |
scope = instance_variable_get("@#{name}") | |
request_params = request.query_parameters | |
url_without_params = request.original_url.slice(0..(request.original_url.index("?")-1)) unless request_params.empty? | |
url_without_params ||= request.original_url | |
page = {} | |
page[:first] = 1 if scope.total_pages > 1 && !scope.first_page? | |
page[:last] = scope.total_pages if scope.total_pages > 1 && !scope.last_page? | |
page[:next] = scope.current_page + 1 unless scope.last_page? | |
page[:prev] = scope.current_page - 1 unless scope.first_page? | |
pagination_links = [] | |
page.each do |k,v| | |
new_request_hash= request_params.merge({:page => v}) | |
pagination_links << "<#{url_without_params}?#{new_request_hash.to_param}>;rel=\"#{k}\">" | |
end | |
headers[:Link] = pagination_links.join(",") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Line 19 you should remove > at the end and line 21 it should be
response['Links']