Class: ActiveModelSerializers::Adapter::JsonApi::PaginationLinks
- Inherits:
-
Object
- Object
- ActiveModelSerializers::Adapter::JsonApi::PaginationLinks
- Defined in:
- lib/active_model_serializers/adapter/json_api/pagination_links.rb
Constant Summary collapse
- MissingSerializationContextError =
Class.new(KeyError)
- FIRST_PAGE =
1
Instance Attribute Summary collapse
-
#collection ⇒ Object
readonly
Returns the value of attribute collection.
-
#context ⇒ Object
readonly
Returns the value of attribute context.
Instance Method Summary collapse
- #as_json ⇒ Object
-
#initialize(collection, adapter_options) ⇒ PaginationLinks
constructor
A new instance of PaginationLinks.
Constructor Details
#initialize(collection, adapter_options) ⇒ PaginationLinks
Returns a new instance of PaginationLinks.
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/active_model_serializers/adapter/json_api/pagination_links.rb', line 10 def initialize(collection, ) @collection = collection @adapter_options = @context = .fetch(:serialization_context) do fail MissingSerializationContextError, <<-EOF.freeze JsonApi::PaginationLinks requires a ActiveModelSerializers::SerializationContext. Please pass a ':serialization_context' option or override CollectionSerializer#paginated? to return 'false'. EOF end end |
Instance Attribute Details
#collection ⇒ Object (readonly)
Returns the value of attribute collection.
8 9 10 |
# File 'lib/active_model_serializers/adapter/json_api/pagination_links.rb', line 8 def collection @collection end |
#context ⇒ Object (readonly)
Returns the value of attribute context.
8 9 10 |
# File 'lib/active_model_serializers/adapter/json_api/pagination_links.rb', line 8 def context @context end |
Instance Method Details
#as_json ⇒ Object
22 23 24 25 26 27 28 29 |
# File 'lib/active_model_serializers/adapter/json_api/pagination_links.rb', line 22 def as_json per_page = collection.try(:per_page) || collection.try(:limit_value) || collection.size pages_from.each_with_object({}) do |(key, value), hash| params = query_parameters.merge(page: { number: value, size: per_page }).to_query hash[key] = "#{url()}?#{params}" end end |