Class: ActiveModelSerializers::Adapter::JsonApi::PaginationLinks

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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, adapter_options)
  @collection = collection
  @adapter_options = adapter_options
  @context = adapter_options.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

#collectionObject (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

#contextObject (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_jsonObject



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(adapter_options)}?#{params}"
  end
end