-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
exchange pagination class to inside json_api scope
- Loading branch information
Showing
8 changed files
with
81 additions
and
83 deletions.
There are no files selected for viewing
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
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
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
52 changes: 52 additions & 0 deletions
52
lib/active_model/serializer/adapter/json_api/pagination_links.rb
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
module ActiveModel | ||
class Serializer | ||
class Adapter | ||
class JsonApi < Adapter | ||
class PaginationLinks | ||
FIRST_PAGE = 1 | ||
|
||
attr_reader :collection | ||
|
||
def initialize(collection) | ||
raise_unless_any_gem_installed | ||
@collection = collection | ||
end | ||
|
||
def page_links | ||
build_links | ||
end | ||
|
||
private | ||
|
||
def build_links | ||
pages_from.each_with_object({}) do |(key, value), hash| | ||
hash[key] = "?page=#{value}&per_page=#{collection.size}" | ||
end | ||
end | ||
|
||
def pages_from | ||
return {} if collection.total_pages == FIRST_PAGE | ||
|
||
{}.tap do |pages| | ||
unless collection.current_page == FIRST_PAGE | ||
pages[:first] = FIRST_PAGE | ||
pages[:prev] = collection.current_page - FIRST_PAGE | ||
end | ||
|
||
unless collection.current_page == collection.total_pages | ||
pages[:next] = collection.current_page + FIRST_PAGE | ||
pages[:last] = collection.total_pages | ||
end | ||
end | ||
end | ||
|
||
def raise_unless_any_gem_installed | ||
return if defined?(WillPaginate) || defined?(Kaminari) | ||
raise "AMS relies on either Kaminari or WillPaginate." + | ||
"Please install either dependency by adding one of those to your Gemfile" | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
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
This file was deleted.
Oops, something went wrong.
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
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