Skip to content

Commit 09f3362

Browse files
committed
Add a #as_json method to the serializer base
1 parent 40d95c2 commit 09f3362

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

lib/active_model/serializer.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,10 @@ def json_key
177177
end
178178
end
179179

180+
def as_json
181+
self.class.adapter.new(self).as_json
182+
end
183+
180184
def id
181185
object.id if object
182186
end

test/adapter_test.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ def test_create_adapter_with_override
3838
adapter = ActiveModel::Serializer::Adapter.create(@serializer, { adapter: :json_api})
3939
assert_equal ActiveModel::Serializer::Adapter::JsonApi, adapter.class
4040
end
41+
42+
def test_as_json_is_sent_to_adapter
43+
assert_equal @serializer.as_json, {name: nil, description: nil}
44+
end
4145
end
4246
end
4347
end

0 commit comments

Comments
 (0)