mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
Add NullAdapater
This commit is contained in:
parent
f00fe5595d
commit
56725b45a6
@ -3,6 +3,7 @@ module ActiveModel
|
||||
class Adapter
|
||||
extend ActiveSupport::Autoload
|
||||
autoload :SimpleAdapter
|
||||
autoload :NullAdapter
|
||||
|
||||
def initialize(serializer)
|
||||
@attributes = serializer.attributes
|
||||
|
||||
15
lib/active_model/serializer/adapter/null_adapter.rb
Normal file
15
lib/active_model/serializer/adapter/null_adapter.rb
Normal file
@ -0,0 +1,15 @@
|
||||
module ActiveModel
|
||||
class Serializer
|
||||
class Adapter
|
||||
class NullAdapter < Adapter
|
||||
def serializable_hash(options = {})
|
||||
{}
|
||||
end
|
||||
|
||||
def to_json(options = {})
|
||||
serializable_hash.to_json
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
21
test/adapter/null_adapter_test.rb
Normal file
21
test/adapter/null_adapter_test.rb
Normal file
@ -0,0 +1,21 @@
|
||||
require 'test_helper'
|
||||
|
||||
module ActiveModel
|
||||
class Serializer
|
||||
class Adapter
|
||||
class NullAdapterTest < Minitest::Test
|
||||
def setup
|
||||
profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
||||
serializer = ProfileSerializer.new(profile)
|
||||
|
||||
@adapter = NullAdapter.new(serializer)
|
||||
end
|
||||
|
||||
def test_it_returns_empty_json
|
||||
assert_equal('{}', @adapter.to_json)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -15,7 +15,7 @@ module ActiveModel
|
||||
end
|
||||
end
|
||||
|
||||
def test_serializable_hash_is_abstract_method
|
||||
def test_to_json_is_abstract_method
|
||||
assert_raises(NotImplementedError) do
|
||||
@adapter.to_json(only: [:name])
|
||||
end
|
||||
|
||||
Loading…
Reference in New Issue
Block a user