mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Encapsulate serialization in ActiveModel::SerializableResource
Usage: ActiveModel::SerializableResource.serialize(resource, options)
This commit is contained in:
@@ -7,8 +7,8 @@ module ActionController
|
||||
rescue_from Exception, with: :handle_error
|
||||
|
||||
def render_using_raise_error_serializer
|
||||
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
||||
render json: [@profile], serializer: RaiseErrorSerializer
|
||||
profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
||||
render json: [profile], serializer: RaiseErrorSerializer
|
||||
end
|
||||
|
||||
def handle_error(exception)
|
||||
@@ -25,7 +25,7 @@ module ActionController
|
||||
errors: ['Internal Server Error']
|
||||
}.to_json
|
||||
|
||||
assert_equal expected, @response.body
|
||||
assert_equal expected, response.body
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -4,6 +4,7 @@ require 'test_helper'
|
||||
module ActionController
|
||||
module Serialization
|
||||
class ImplicitSerializerTest < ActionController::TestCase
|
||||
include ActiveSupport::Testing::Stream
|
||||
class ImplicitSerializationTestController < ActionController::Base
|
||||
def render_using_implicit_serializer
|
||||
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
||||
@@ -140,10 +141,7 @@ module ActionController
|
||||
|
||||
private
|
||||
def generate_cached_serializer(obj)
|
||||
serializer_class = ActiveModel::Serializer.serializer_for(obj)
|
||||
serializer = serializer_class.new(obj)
|
||||
adapter = ActiveModel::Serializer.adapter.new(serializer)
|
||||
adapter.to_json
|
||||
ActiveModel::SerializableResource.new(obj).to_json
|
||||
end
|
||||
|
||||
def with_adapter(adapter)
|
||||
@@ -400,6 +398,28 @@ module ActionController
|
||||
assert_equal 'application/json', @response.content_type
|
||||
assert_equal expected.to_json, @response.body
|
||||
end
|
||||
|
||||
def test_warn_overridding_use_adapter_as_falsy_on_controller_instance
|
||||
controller = Class.new(ImplicitSerializationTestController) {
|
||||
def use_adapter?
|
||||
false
|
||||
end
|
||||
}.new
|
||||
assert_match /adapter: false/, (capture(:stderr) {
|
||||
controller.get_serializer(@profile)
|
||||
})
|
||||
end
|
||||
|
||||
def test_dont_warn_overridding_use_adapter_as_truthy_on_controller_instance
|
||||
controller = Class.new(ImplicitSerializationTestController) {
|
||||
def use_adapter?
|
||||
true
|
||||
end
|
||||
}.new
|
||||
assert_equal "", (capture(:stderr) {
|
||||
controller.get_serializer(@profile)
|
||||
})
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
27
test/serializable_resource_test.rb
Normal file
27
test/serializable_resource_test.rb
Normal file
@@ -0,0 +1,27 @@
|
||||
require 'test_helper'
|
||||
|
||||
module ActiveModel
|
||||
class SerializableResourceTest < Minitest::Test
|
||||
def setup
|
||||
@resource = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
||||
@serializer = ProfileSerializer.new(@resource)
|
||||
@adapter = ActiveModel::Serializer::Adapter.create(@serializer)
|
||||
@serializable_resource = ActiveModel::SerializableResource.new(@resource)
|
||||
end
|
||||
|
||||
def test_serializable_resource_delegates_serializable_hash_to_the_adapter
|
||||
options = nil
|
||||
assert_equal @adapter.serializable_hash(options), @serializable_resource.serializable_hash(options)
|
||||
end
|
||||
|
||||
def test_serializable_resource_delegates_to_json_to_the_adapter
|
||||
options = nil
|
||||
assert_equal @adapter.to_json(options), @serializable_resource.to_json(options)
|
||||
end
|
||||
|
||||
def test_serializable_resource_delegates_as_json_to_the_adapter
|
||||
options = nil
|
||||
assert_equal @adapter.as_json(options), @serializable_resource.as_json(options)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -131,10 +131,7 @@ module ActiveModel
|
||||
|
||||
private
|
||||
def render_object_with_cache(obj)
|
||||
serializer_class = ActiveModel::Serializer.serializer_for(obj)
|
||||
serializer = serializer_class.new(obj)
|
||||
adapter = ActiveModel::Serializer.adapter.new(serializer)
|
||||
adapter.serializable_hash
|
||||
ActiveModel::SerializableResource.new(obj).serializable_hash
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -113,11 +113,8 @@ module ActiveModel
|
||||
private
|
||||
|
||||
def load_adapter(options)
|
||||
adapter_opts, serializer_opts =
|
||||
options.partition { |k, _| ActionController::Serialization::ADAPTER_OPTION_KEYS.include? k }.map { |h| Hash[h] }
|
||||
|
||||
serializer = AlternateBlogSerializer.new(@blog, serializer_opts)
|
||||
ActiveModel::Serializer::Adapter::FlattenJson.new(serializer, adapter_opts)
|
||||
options = options.merge(adapter: :flatten_json, serializer: AlternateBlogSerializer)
|
||||
ActiveModel::SerializableResource.new(@blog, options)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -13,6 +13,55 @@ Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
|
||||
|
||||
require 'active_model_serializers'
|
||||
|
||||
# Use cleaner stream testing interface from Rails 5 if available
|
||||
# see https://github.com/rails/rails/blob/29959eb59d/activesupport/lib/active_support/testing/stream.rb
|
||||
begin
|
||||
require "active_support/testing/stream"
|
||||
rescue LoadError
|
||||
module ActiveSupport
|
||||
module Testing
|
||||
module Stream #:nodoc:
|
||||
private
|
||||
|
||||
def silence_stream(stream)
|
||||
old_stream = stream.dup
|
||||
stream.reopen(IO::NULL)
|
||||
stream.sync = true
|
||||
yield
|
||||
ensure
|
||||
stream.reopen(old_stream)
|
||||
old_stream.close
|
||||
end
|
||||
|
||||
def quietly
|
||||
silence_stream(STDOUT) do
|
||||
silence_stream(STDERR) do
|
||||
yield
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def capture(stream)
|
||||
stream = stream.to_s
|
||||
captured_stream = Tempfile.new(stream)
|
||||
stream_io = eval("$#{stream}")
|
||||
origin_stream = stream_io.dup
|
||||
stream_io.reopen(captured_stream)
|
||||
|
||||
yield
|
||||
|
||||
stream_io.rewind
|
||||
return captured_stream.read
|
||||
ensure
|
||||
captured_stream.close
|
||||
captured_stream.unlink
|
||||
stream_io.reopen(origin_stream)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class Foo < Rails::Application
|
||||
if Rails::VERSION::MAJOR >= 4
|
||||
config.eager_load = false
|
||||
|
||||
Reference in New Issue
Block a user