lib/jsonapi/resource_serializer.rb in jsonapi-resources-0.0.6 vs lib/jsonapi/resource_serializer.rb in jsonapi-resources-0.0.7
- old
+ new
@@ -13,11 +13,11 @@
is_resource_collection = source.respond_to?(:to_ary)
return {} if source.nil? || (is_resource_collection && source.size == 0)
@fields = options.fetch(:fields, {})
include = options.fetch(:include, [])
- @context = options.fetch(:context, nil)
+
@key_formatter = options.fetch(:key_formatter, JSONAPI.configuration.key_formatter)
@linked_objects = {}
requested_associations = parse_includes(include)
@@ -108,11 +108,11 @@
add_linked_object(@primary_class_name, id, object_hash(source, requested_associations), true)
end
end
- # Returns a serialized hash for the source object, with
+ # Returns a serialized hash for the source model, with
def object_hash(source, requested_associations)
obj_hash = attribute_hash(source)
links = links_hash(source, requested_associations)
obj_hash.merge!({links: links}) unless links.empty?
return obj_hash
@@ -122,20 +122,19 @@
@fields[model] if @fields
end
def attribute_hash(source)
requested = requested_fields(source.class._type)
- fields = source.fetchable_fields(@context) & source.class._attributes.keys.to_a
+ fields = source.fetchable_fields & source.class._attributes.keys.to_a
unless requested.nil?
fields = requested & fields
end
fields.each_with_object({}) do |name, hash|
hash[format_key(name)] = format_value(source.send(name),
source.class._attribute_options(name)[:format],
- source,
- @context)
+ source)
end
end
# Returns a hash of links for the requested associations for a resource, filtered by the resource
# class's fetchable method
@@ -147,11 +146,11 @@
fields = requested & fields
end
field_set = Set.new(fields)
- included_associations = source.fetchable_fields(@context) & associations.keys
+ included_associations = source.fetchable_fields & associations.keys
associations.each_with_object({}) do |(name, association), hash|
if included_associations.include? name
key = association.key
if field_set.include?(name)
@@ -168,29 +167,29 @@
# If the object has been serialized once it will be in the related objects list,
# but it's possible all children won't have been captured. So we must still go
# through the associations.
if include_linked_object || include_linked_children
if association.is_a?(JSONAPI::Association::HasOne)
- object = source.send("_#{name}_object")
- if object
- id = object.send(association.primary_key)
+ resource = source.send("_#{name}_resource")
+ if resource
+ id = resource.send(association.primary_key)
associations_only = already_serialized?(type, id)
if include_linked_object && !associations_only
- add_linked_object(type, id, object_hash(object, ia[:include_related]))
+ add_linked_object(type, id, object_hash(resource, ia[:include_related]))
elsif include_linked_children || associations_only
- links_hash(object, ia[:include_related])
+ links_hash(resource, ia[:include_related])
end
end
elsif association.is_a?(JSONAPI::Association::HasMany)
- objects = source.send("_#{name}_objects")
- objects.each do |object|
- id = object.send(association.primary_key)
+ resources = source.send("_#{name}_resources")
+ resources.each do |resource|
+ id = resource.send(association.primary_key)
associations_only = already_serialized?(type, id)
if include_linked_object && !associations_only
- add_linked_object(type, id, object_hash(object, ia[:include_related]))
+ add_linked_object(type, id, object_hash(resource, ia[:include_related]))
elsif include_linked_children || associations_only
- links_hash(object, ia[:include_related])
+ links_hash(resource, ia[:include_related])
end
end
end
end
end
@@ -227,11 +226,11 @@
def format_key(key)
@key_formatter.format(key)
end
- def format_value(value, format, source, context)
+ def format_value(value, format, source)
value_formatter = JSONAPI::ValueFormatter.value_formatter_for(format)
- value_formatter.format(value, source, context)
+ value_formatter.format(value, source)
end
end
end