Skip to content

Commit

Permalink
move DefaultSerializer to a separate file, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
teeparham committed Jun 14, 2013
1 parent 4f185ef commit c96095f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 15 deletions.
18 changes: 18 additions & 0 deletions lib/active_model/default_serializer.rb
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
15 changes: 0 additions & 15 deletions lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -457,19 +457,4 @@ def default_embed_options
end
end

# 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
1 change: 1 addition & 0 deletions lib/active_model_serializers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require "active_support/notifications"
require "active_model"
require "active_model/array_serializer"
require "active_model/default_serializer"
require "active_model/serializer"
require "active_model/serializer/associations"
require "set"
Expand Down
25 changes: 25 additions & 0 deletions test/default_serializer_test.rb
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

0 comments on commit c96095f

Please sign in to comment.