Skip to content

Commit be01fc8

Browse files
vasilakisfilbf4
authored andcommitted
Pass fields down from constructor
1 parent cd96896 commit be01fc8

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

lib/active_model_serializers/adapter/attributes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module Adapter
33
class Attributes < Base
44
def serializable_hash(options = nil)
55
options = serialization_options(options)
6+
options[:fields] ||= instance_options[:fields]
67
serializer.serializable_hash(instance_options, options, self)
78
end
89
end

test/adapter/json/collection_test.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,20 @@ def test_include_option
8484

8585
assert_equal(expected, actual)
8686
end
87+
88+
def test_fields_with_no_associations_include_option
89+
actual = ActiveModelSerializers::SerializableResource.new(
90+
[@first_post, @second_post], adapter: :json, fields: [:id], include: []
91+
).as_json
92+
93+
expected = { posts: [{
94+
id: 1
95+
}, {
96+
id: 2
97+
}] }
98+
99+
assert_equal(expected, actual)
100+
end
87101
end
88102
end
89103
end

0 commit comments

Comments
 (0)