Skip to content

Commit

Permalink
Add a #to_json method to the serializer base
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinAiken committed Apr 27, 2015
1 parent 40d95c2 commit db487a3
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 @@ -177,6 +177,10 @@ def json_key
end
end

def to_json
self.class.adapter.new(self).to_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_to_json_is_sent_to_adapter
assert_equal JSON.parse(@serializer.to_json), {"name" => nil, "description" => nil}
end
end
end
end

0 comments on commit db487a3

Please sign in to comment.