Move id and json_api_type methods from Serializer to JsonApi.

This commit is contained in:
Lucas Hosseini 2015-08-31 02:32:38 +02:00
parent 64168cbecd
commit 8482abfac7
3 changed files with 22 additions and 30 deletions

View File

@ -146,18 +146,6 @@ module ActiveModel
@root || object.class.model_name.to_s.underscore @root || object.class.model_name.to_s.underscore
end end
def id
object.id if object
end
def json_api_type
if config.jsonapi_resource_type == :plural
object.class.model_name.plural
else
object.class.model_name.singular
end
end
def attributes(options = {}) def attributes(options = {})
attributes = attributes =
if options[:fields] if options[:fields]
@ -166,8 +154,6 @@ module ActiveModel
self.class._attributes.dup self.class._attributes.dup
end end
attributes += options[:required_fields] if options[:required_fields]
attributes.each_with_object({}) do |name, hash| attributes.each_with_object({}) do |name, hash|
unless self.class._fragmented unless self.class._fragmented
hash[name] = send(name) hash[name] = send(name)

View File

@ -44,19 +44,34 @@ module ActiveModel
private private
def resource_identifier(serializer)
type = if ActiveModel::Serializer.config.jsonapi_resource_type == :plural
serializer.object.class.model_name.plural
else
serializer.object.class.model_name.singular
end
id = serializer.object.id.to_s
{ id: id, type: type }
end
def add_relationships(resource, name, serializers) def add_relationships(resource, name, serializers)
resource[:relationships] ||= {} resource[:relationships] ||= {}
resource[:relationships][name] ||= { data: [] } resource[:relationships][name] ||= { data: [] }
resource[:relationships][name][:data] += serializers.map { |serializer| { type: serializer.json_api_type, id: serializer.id.to_s } } resource[:relationships][name][:data] += serializers.map { |serializer| resource_identifier(serializer) }
end end
def add_relationship(resource, name, serializer, val=nil) def add_relationship(resource, name, serializer, val=nil)
resource[:relationships] ||= {} resource[:relationships] ||= {}
resource[:relationships][name] = { data: val }
if serializer && serializer.object resource[:relationships][name] ||= {}
resource[:relationships][name][:data] = { type: serializer.json_api_type, id: serializer.id.to_s } resource[:relationships][name][:data] = if val
end val
elsif serializer && serializer.object
resource_identifier(serializer)
else
nil
end
end end
def add_included(resource_name, serializers, parent = nil) def add_included(resource_name, serializers, parent = nil)
@ -100,15 +115,12 @@ module ActiveModel
def resource_object_for(serializer, options) def resource_object_for(serializer, options)
options[:fields] = @fieldset && @fieldset.fields_for(serializer) options[:fields] = @fieldset && @fieldset.fields_for(serializer)
options[:required_fields] = [:id, :json_api_type]
cache_check(serializer) do cache_check(serializer) do
attributes = serializer.attributes(options) attributes = serializer.attributes(options)
attributes.delete(:id)
result = { result = resource_identifier(serializer)
id: attributes.delete(:id).to_s,
type: attributes.delete(:json_api_type)
}
result[:attributes] = attributes if attributes.any? result[:attributes] = attributes if attributes.any?
result result

View File

@ -23,12 +23,6 @@ module ActiveModel
@profile_serializer.attributes(fields: [:name])) @profile_serializer.attributes(fields: [:name]))
end end
def test_required_fields
assert_equal({name: 'Name 1', description: 'Description 1'},
@profile_serializer.attributes(fields: [:name, :description], required_fields: [:name]))
end
def test_attributes_inheritance_definition def test_attributes_inheritance_definition
assert_equal([:id, :body], @serializer_klass._attributes) assert_equal([:id, :body], @serializer_klass._attributes)
end end