lib/jsonapionify/api/resource/builders.rb in jsonapionify-0.0.1.pre vs lib/jsonapionify/api/resource/builders.rb in jsonapionify-0.9.0
- old
+ new
@@ -2,22 +2,30 @@
module Resource::Builders
extend ActiveSupport::Concern
module ClassMethods
def build_resource(request, instance, fields: api.fields, relationships: true, links: true)
+ relationships = false if JSONAPIonify::FALSEY_STRINGS.include? request.params['include-relationships']
return nil unless instance
resource_url = build_url(request, instance)
id = build_id(instance)
JSONAPIonify::Structure::Objects::Resource.new.tap do |resource|
- resource[:id] = id
- resource[:type] = type
- resource[:attributes] = fields[type.to_sym].each_with_object(JSONAPIonify::Structure::Objects::Attributes.new) do |member, attributes|
+ resource[:id] = id
+ resource[:type] = type
+
+ resource[:attributes] = fields[type.to_sym].each_with_object(JSONAPIonify::Structure::Objects::Attributes.new) do |member, attributes|
attributes[member.to_sym] = instance.public_send(member)
end
- resource[:links] = JSONAPIonify::Structure::Objects::Links.new(
+
+ resource[:links] = JSONAPIonify::Structure::Objects::Links.new(
self: resource_url
) if links
+
+ resource[:meta] = {
+ cursor: build_cursor_from_instance(request, instance)
+ }
+
resource[:relationships] = relationship_definitions.each_with_object(JSONAPIonify::Structure::Maps::Relationships.new) do |rel, hash|
hash[rel.name] = build_relationship(request, instance, rel.name)
end if relationships
end
end
@@ -27,16 +35,32 @@
id: build_id(instance),
type: type.to_s
)
end
- def build_collection(request, collection, fields: api.fields, relationships: false)
+ def build_collection(request, collection, fields: api.fields)
+ relationships = JSONAPIonify::TRUTHY_STRINGS.include? request.params['include-relationships']
collection.each_with_object(JSONAPIonify::Structure::Collections::Resources.new) do |instance, resources|
resources << build_resource(request, instance, fields: fields, relationships: relationships)
end
end
+ def build_cursor_from_instance(request, instance)
+ sort_string = request.params['sort']
+ sort_fields = sort_fields_from_sort_string(sort_string)
+ attrs_with_values = sort_fields.each_with_object({}) do |field, hash|
+ hash[field.name] = instance.send(field.name)
+ end
+ Base64.urlsafe_encode64(JSON.dump(
+ {
+ t: type,
+ s: sort_string,
+ a: attrs_with_values
+ }
+ ))
+ end
+
def build_identifier_collection(collection)
collection.each_with_object(JSONAPIonify::Structure::Collections::ResourceIdentifiers.new) do |instance, resource_identifiers|
resource_identifiers << build_resource_identifier(instance)
end
end
@@ -59,14 +83,19 @@
end
end
end
def build_url(request, instance = nil)
- if instance
- File.join(request.root_url, type, build_id(instance))
- else
- File.join(request.root_url, type)
- end
+ URI.parse(request.root_url).tap do |uri|
+ uri.path =
+ if instance
+ File.join(uri.path, type, build_id(instance))
+ else
+ File.join(request.root_url, type)
+ end
+ sticky_params = self.sticky_params(request.params)
+ uri.query = sticky_params.to_param if sticky_params.present?
+ end.to_s
end
def build_id(instance)
instance.send(id_attribute).to_s
end