Skip to content

Commit

Permalink
Add a #as_json method to the serializer base
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinAiken committed Jun 9, 2015
1 parent 7b0a85f commit 3315b01
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ def json_key
end
end

def as_json
self.class.adapter.new(self).as_json
end

def id
object.id if object
end
Expand Down
4 changes: 4 additions & 0 deletions test/adapter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ def test_create_adapter_with_override
adapter = ActiveModel::Serializer::Adapter.create(@serializer, { adapter: :json_api})
assert_equal ActiveModel::Serializer::Adapter::JsonApi, adapter.class
end

def test_as_json_is_sent_to_adapter
assert_equal @serializer.as_json, {name: nil, description: nil}
end
end
end
end

0 comments on commit 3315b01

Please sign in to comment.