-
Notifications
You must be signed in to change notification settings - Fork 1
Open
Description
Currently jsonapi-query-builder only support pagy https://github.com/infinum/jsonapi-query_builder/blob/master/lib/jsonapi/query_builder/mixins/paginate.rb
It also takes care of building meta and liks via pagination_options.
I believe we can do better!
Idea is to create a warpper/builder class that can be used in controllers and would take the responsibiliy of building links and meta out from SerializerOptions
Lets take a look first at how the API would look:
class UserController
builder = JsonApi::Builder.new(scope: User.all, params: params, query: Api::Users::Query)
respond_with builder, serializer: Api::Users::Serializer, meta: {additional: 'meta'}
end
class JsonApi::Builder
# initialziers
def records
query.new(scope, params, paginator: paginator).records
end
def options
{ meta: paginator.build_meta, links: paginator.build_links }
end
def paginator
if cursor_pagination?
JsonApi::Paginators::Cursor
else
JsonApi::Paginators::Page
end
end
end
# in the intitializer, render method:
ActiveSupport::Notifications.instrument('render.json_api', builder: builder, opts: opts) do
# old code
serializer.new(builder.records, builder.options.merge(opts)).serializable_hash.to_json
end
This also means we need to do work on the query-builder where it can accept a paginator class and work with it.
What do you think?