-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move DefaultSerializer to a separate file, add tests
- Loading branch information
Showing
4 changed files
with
44 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
module ActiveModel | ||
# DefaultSerializer | ||
# | ||
# Provides a constant interface for all items, particularly | ||
# for ArraySerializer. | ||
class DefaultSerializer | ||
attr_reader :object, :options | ||
|
||
def initialize(object, options={}) | ||
@object, @options = object, options | ||
end | ||
|
||
def serializable_hash | ||
@object.as_json(@options) | ||
end | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
require "test_helper" | ||
require "test_fakes" | ||
|
||
class DefaultSerializerTest < ActiveModel::TestCase | ||
|
||
def test_object_with_no_options | ||
object = Model.new | ||
serializer = ActiveModel::DefaultSerializer.new(object) | ||
assert_equal object.as_json, serializer.serializable_hash | ||
end | ||
|
||
def test_object_with_options | ||
object = Model.new | ||
serializer = ActiveModel::DefaultSerializer.new(object, something: "hello") | ||
assert_equal({something: "hello"}, serializer.options) | ||
assert_equal object.as_json, serializer.serializable_hash | ||
end | ||
|
||
def test_array | ||
array = [ {name: "Fred"} ] | ||
serializer = ActiveModel::DefaultSerializer.new(array) | ||
assert_equal array.as_json, serializer.serializable_hash | ||
end | ||
|
||
end |