mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
Refactoring
This commit is contained in:
parent
06fcd34807
commit
a9cb097109
@ -16,6 +16,8 @@ module ActiveModelSerializers
|
|||||||
self.class.transform_key_casing!(serialized_hash, instance_options)
|
self.class.transform_key_casing!(serialized_hash, instance_options)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
def fields_to_fieldset(fields)
|
def fields_to_fieldset(fields)
|
||||||
return fields if fields.nil?
|
return fields if fields.nil?
|
||||||
resource_fields = []
|
resource_fields = []
|
||||||
@ -27,7 +29,7 @@ module ActiveModelSerializers
|
|||||||
else fail ArgumentError, "Unknown conversion of fields to fieldset: '#{field.inspect}' in '#{fields.inspect}'"
|
else fail ArgumentError, "Unknown conversion of fields to fieldset: '#{field.inspect}' in '#{fields.inspect}'"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
relationship_fields.merge(serializer.json_key.to_sym => resource_fields)
|
relationship_fields.merge!(serializer.json_key.to_sym => resource_fields)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -17,7 +17,7 @@ module ActiveModelSerializers
|
|||||||
end
|
end
|
||||||
|
|
||||||
class PostSerializer < ActiveModel::Serializer
|
class PostSerializer < ActiveModel::Serializer
|
||||||
type 'posts'
|
type 'post'
|
||||||
attributes :title, :body
|
attributes :title, :body
|
||||||
belongs_to :author
|
belongs_to :author
|
||||||
has_many :comments
|
has_many :comments
|
||||||
@ -28,7 +28,7 @@ module ActiveModelSerializers
|
|||||||
end
|
end
|
||||||
|
|
||||||
class CommentSerializer < ActiveModel::Serializer
|
class CommentSerializer < ActiveModel::Serializer
|
||||||
type 'comments'
|
type 'comment'
|
||||||
attributes :title, :body
|
attributes :title, :body
|
||||||
belongs_to :author
|
belongs_to :author
|
||||||
end
|
end
|
||||||
@ -47,7 +47,7 @@ module ActiveModelSerializers
|
|||||||
fields = [:title]
|
fields = [:title]
|
||||||
hash = serializable(@post, adapter: :json, fields: fields, include: []).serializable_hash
|
hash = serializable(@post, adapter: :json, fields: fields, include: []).serializable_hash
|
||||||
expected = { title: 'Title 1' }
|
expected = { title: 'Title 1' }
|
||||||
assert_equal(expected, hash[:posts])
|
assert_equal(expected, hash[:post])
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_fields_included
|
def test_fields_included
|
||||||
@ -55,7 +55,7 @@ module ActiveModelSerializers
|
|||||||
hash = serializable(@post, adapter: :json, include: [:comments], fields: fields).serializable_hash
|
hash = serializable(@post, adapter: :json, include: [:comments], fields: fields).serializable_hash
|
||||||
expected = [{ body: @comment1.body }, { body: @comment2.body }]
|
expected = [{ body: @comment1.body }, { body: @comment2.body }]
|
||||||
|
|
||||||
assert_equal(expected, hash[:posts][:comments])
|
assert_equal(expected, hash[:post][:comments])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user