Skip to content

Commit 06fcd34

Browse files
bf4wasifhossain
authored andcommitted
Follows #2100
1 parent 7896ad6 commit 06fcd34

File tree

3 files changed

+91
-1
lines changed

3 files changed

+91
-1
lines changed

lib/active_model/serializer.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,9 @@ def associations(include_directive = ActiveModelSerializers.default_include_dire
365365
def serializable_hash(adapter_options = nil, options = {}, adapter_instance = self.class.serialization_adapter_instance)
366366
adapter_options ||= {}
367367
options[:include_directive] ||= ActiveModel::Serializer.include_directive_from_options(adapter_options)
368+
if (fieldset = adapter_options[:fieldset])
369+
options[:fields] = fieldset.fields_for(json_key)
370+
end
368371
resource = attributes_hash(adapter_options, options, adapter_instance)
369372
relationships = associations_hash(adapter_options, options, adapter_instance)
370373
resource.merge(relationships)
@@ -379,7 +382,12 @@ def as_json(adapter_opts = nil)
379382

380383
# Used by adapter as resource root.
381384
def json_key
382-
root || _type || object.class.model_name.to_s.underscore
385+
root || _type ||
386+
begin
387+
object.class.model_name.to_s.underscore
388+
rescue ArgumentError
389+
'anonymous_object'
390+
end
383391
end
384392

385393
def read_attribute_for_serialization(attr)

lib/active_model_serializers/adapter/attributes.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,32 @@
33
module ActiveModelSerializers
44
module Adapter
55
class Attributes < Base
6+
def initialize(*)
7+
super
8+
instance_options[:fieldset] ||= ActiveModel::Serializer::Fieldset.new(fields_to_fieldset(instance_options.delete(:fields)))
9+
end
10+
611
def serializable_hash(options = nil)
712
options = serialization_options(options)
813
options[:fields] ||= instance_options[:fields]
914
serialized_hash = serializer.serializable_hash(instance_options, options, self)
1015

1116
self.class.transform_key_casing!(serialized_hash, instance_options)
1217
end
18+
19+
def fields_to_fieldset(fields)
20+
return fields if fields.nil?
21+
resource_fields = []
22+
relationship_fields = {}
23+
fields.each do |field|
24+
case field
25+
when Symbol, String then resource_fields << field
26+
when Hash then relationship_fields.merge!(field)
27+
else fail ArgumentError, "Unknown conversion of fields to fieldset: '#{field.inspect}' in '#{fields.inspect}'"
28+
end
29+
end
30+
relationship_fields.merge(serializer.json_key.to_sym => resource_fields)
31+
end
1332
end
1433
end
1534
end

test/adapter/json/fields_test.rb

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
require 'test_helper'
2+
3+
module ActiveModelSerializers
4+
module Adapter
5+
class Json
6+
class FieldsTest < ActiveSupport::TestCase
7+
class Post < ::Model
8+
attributes :title, :body
9+
associations :author, :comments
10+
end
11+
class Author < ::Model
12+
attributes :name, :birthday
13+
end
14+
class Comment < ::Model
15+
attributes :title, :body
16+
associations :author, :post
17+
end
18+
19+
class PostSerializer < ActiveModel::Serializer
20+
type 'posts'
21+
attributes :title, :body
22+
belongs_to :author
23+
has_many :comments
24+
end
25+
26+
class AuthorSerializer < ActiveModel::Serializer
27+
attributes :name, :birthday
28+
end
29+
30+
class CommentSerializer < ActiveModel::Serializer
31+
type 'comments'
32+
attributes :title, :body
33+
belongs_to :author
34+
end
35+
36+
def setup
37+
@author = Author.new(id: 1, name: 'Lucas', birthday: '10.01.1990')
38+
@comment1 = Comment.new(id: 7, body: 'cool', author: @author)
39+
@comment2 = Comment.new(id: 12, body: 'awesome', author: @author)
40+
@post = Post.new(id: 1337, title: 'Title 1', body: 'Body 1',
41+
author: @author, comments: [@comment1, @comment2])
42+
@comment1.post = @post
43+
@comment2.post = @post
44+
end
45+
46+
def test_fields_attributes
47+
fields = [:title]
48+
hash = serializable(@post, adapter: :json, fields: fields, include: []).serializable_hash
49+
expected = { title: 'Title 1' }
50+
assert_equal(expected, hash[:posts])
51+
end
52+
53+
def test_fields_included
54+
fields = [:title, { comments: [:body] }]
55+
hash = serializable(@post, adapter: :json, include: [:comments], fields: fields).serializable_hash
56+
expected = [{ body: @comment1.body }, { body: @comment2.body }]
57+
58+
assert_equal(expected, hash[:posts][:comments])
59+
end
60+
end
61+
end
62+
end
63+
end

0 commit comments

Comments
 (0)