mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 14:56:50 +00:00
allow for a type attribute
* "namespace" json_api specific type method
This commit is contained in:
parent
4af98852b8
commit
033ce8e88d
@ -134,7 +134,7 @@ module ActiveModel
|
|||||||
object.id if object
|
object.id if object
|
||||||
end
|
end
|
||||||
|
|
||||||
def type
|
def json_api_type
|
||||||
object.class.model_name.plural
|
object.class.model_name.plural
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -44,7 +44,7 @@ module ActiveModel
|
|||||||
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.type, id: serializer.id.to_s } }
|
resource[:relationships][name][:data] += serializers.map { |serializer| { type: serializer.json_api_type, id: serializer.id.to_s } }
|
||||||
end
|
end
|
||||||
|
|
||||||
def add_relationship(resource, name, serializer, val=nil)
|
def add_relationship(resource, name, serializer, val=nil)
|
||||||
@ -52,7 +52,7 @@ module ActiveModel
|
|||||||
resource[:relationships][name] = { data: val }
|
resource[:relationships][name] = { data: val }
|
||||||
|
|
||||||
if serializer && serializer.object
|
if serializer && serializer.object
|
||||||
resource[:relationships][name][:data] = { type: serializer.type, id: serializer.id.to_s }
|
resource[:relationships][name][:data] = { type: serializer.json_api_type, id: serializer.id.to_s }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -97,14 +97,14 @@ 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, :type]
|
options[:required_fields] = [:id, :json_api_type]
|
||||||
|
|
||||||
cache_check(serializer) do
|
cache_check(serializer) do
|
||||||
attributes = serializer.attributes(options)
|
attributes = serializer.attributes(options)
|
||||||
|
|
||||||
result = {
|
result = {
|
||||||
id: attributes.delete(:id).to_s,
|
id: attributes.delete(:id).to_s,
|
||||||
type: attributes.delete(:type)
|
type: attributes.delete(:json_api_type)
|
||||||
}
|
}
|
||||||
|
|
||||||
result[:attributes] = attributes if attributes.any?
|
result[:attributes] = attributes if attributes.any?
|
||||||
|
|||||||
@ -4,7 +4,7 @@ module ActiveModel
|
|||||||
class Serializer
|
class Serializer
|
||||||
class AttributeTest < Minitest::Test
|
class AttributeTest < Minitest::Test
|
||||||
def setup
|
def setup
|
||||||
@blog = Blog.new({ id: 1, name: 'AMS Hints' })
|
@blog = Blog.new({ id: 1, name: 'AMS Hints', type: "stuff" })
|
||||||
@blog_serializer = AlternateBlogSerializer.new(@blog)
|
@blog_serializer = AlternateBlogSerializer.new(@blog)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -42,6 +42,21 @@ module ActiveModel
|
|||||||
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer.new(@blog))
|
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer.new(@blog))
|
||||||
assert_equal({ blog: { id: "AMS Hints" } }, adapter.serializable_hash)
|
assert_equal({ blog: { id: "AMS Hints" } }, adapter.serializable_hash)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_type_attribute
|
||||||
|
attribute_serializer = Class.new(ActiveModel::Serializer) do
|
||||||
|
attribute :id, key: :type
|
||||||
|
end
|
||||||
|
attributes_serializer = Class.new(ActiveModel::Serializer) do
|
||||||
|
attributes :type
|
||||||
|
end
|
||||||
|
|
||||||
|
adapter = ActiveModel::Serializer::Adapter::Json.new(attribute_serializer.new(@blog))
|
||||||
|
assert_equal({ blog: { type: 1} }, adapter.serializable_hash)
|
||||||
|
|
||||||
|
adapter = ActiveModel::Serializer::Adapter::Json.new(attributes_serializer.new(@blog))
|
||||||
|
assert_equal({ blog: { type: "stuff" } }, adapter.serializable_hash)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user