mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
Move id and json_api_type methods from Serializer to JsonApi.
This commit is contained in:
parent
64168cbecd
commit
8482abfac7
@ -146,18 +146,6 @@ module ActiveModel
|
||||
@root || object.class.model_name.to_s.underscore
|
||||
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 = {})
|
||||
attributes =
|
||||
if options[:fields]
|
||||
@ -166,8 +154,6 @@ module ActiveModel
|
||||
self.class._attributes.dup
|
||||
end
|
||||
|
||||
attributes += options[:required_fields] if options[:required_fields]
|
||||
|
||||
attributes.each_with_object({}) do |name, hash|
|
||||
unless self.class._fragmented
|
||||
hash[name] = send(name)
|
||||
|
||||
@ -44,19 +44,34 @@ module ActiveModel
|
||||
|
||||
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)
|
||||
resource[:relationships] ||= {}
|
||||
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
|
||||
|
||||
def add_relationship(resource, name, serializer, val=nil)
|
||||
resource[:relationships] ||= {}
|
||||
resource[:relationships][name] = { data: val }
|
||||
|
||||
if serializer && serializer.object
|
||||
resource[:relationships][name][:data] = { type: serializer.json_api_type, id: serializer.id.to_s }
|
||||
end
|
||||
resource[:relationships][name] ||= {}
|
||||
resource[:relationships][name][:data] = if val
|
||||
val
|
||||
elsif serializer && serializer.object
|
||||
resource_identifier(serializer)
|
||||
else
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
def add_included(resource_name, serializers, parent = nil)
|
||||
@ -100,15 +115,12 @@ module ActiveModel
|
||||
|
||||
def resource_object_for(serializer, options)
|
||||
options[:fields] = @fieldset && @fieldset.fields_for(serializer)
|
||||
options[:required_fields] = [:id, :json_api_type]
|
||||
|
||||
cache_check(serializer) do
|
||||
attributes = serializer.attributes(options)
|
||||
attributes.delete(:id)
|
||||
|
||||
result = {
|
||||
id: attributes.delete(:id).to_s,
|
||||
type: attributes.delete(:json_api_type)
|
||||
}
|
||||
result = resource_identifier(serializer)
|
||||
|
||||
result[:attributes] = attributes if attributes.any?
|
||||
result
|
||||
|
||||
@ -23,12 +23,6 @@ module ActiveModel
|
||||
@profile_serializer.attributes(fields: [:name]))
|
||||
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
|
||||
assert_equal([:id, :body], @serializer_klass._attributes)
|
||||
end
|
||||
|
||||
Loading…
Reference in New Issue
Block a user