mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06: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)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def fields_to_fieldset(fields)
|
||||
return fields if fields.nil?
|
||||
resource_fields = []
|
||||
@ -27,7 +29,7 @@ module ActiveModelSerializers
|
||||
else fail ArgumentError, "Unknown conversion of fields to fieldset: '#{field.inspect}' in '#{fields.inspect}'"
|
||||
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
|
||||
|
||||
@ -17,7 +17,7 @@ module ActiveModelSerializers
|
||||
end
|
||||
|
||||
class PostSerializer < ActiveModel::Serializer
|
||||
type 'posts'
|
||||
type 'post'
|
||||
attributes :title, :body
|
||||
belongs_to :author
|
||||
has_many :comments
|
||||
@ -28,7 +28,7 @@ module ActiveModelSerializers
|
||||
end
|
||||
|
||||
class CommentSerializer < ActiveModel::Serializer
|
||||
type 'comments'
|
||||
type 'comment'
|
||||
attributes :title, :body
|
||||
belongs_to :author
|
||||
end
|
||||
@ -47,7 +47,7 @@ module ActiveModelSerializers
|
||||
fields = [:title]
|
||||
hash = serializable(@post, adapter: :json, fields: fields, include: []).serializable_hash
|
||||
expected = { title: 'Title 1' }
|
||||
assert_equal(expected, hash[:posts])
|
||||
assert_equal(expected, hash[:post])
|
||||
end
|
||||
|
||||
def test_fields_included
|
||||
@ -55,7 +55,7 @@ module ActiveModelSerializers
|
||||
hash = serializable(@post, adapter: :json, include: [:comments], fields: fields).serializable_hash
|
||||
expected = [{ body: @comment1.body }, { body: @comment2.body }]
|
||||
|
||||
assert_equal(expected, hash[:posts][:comments])
|
||||
assert_equal(expected, hash[:post][:comments])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Loading…
Reference in New Issue
Block a user