mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
* Rename NullAdapter to SimpleAdapter
* Introduce abstract Adapter class * Organaze test structure to match convemtions
This commit is contained in:
parent
0c13956311
commit
f00fe5595d
@ -12,7 +12,7 @@ module ActionController
|
||||
if serializer
|
||||
# omg hax
|
||||
object = serializer.new(resource)
|
||||
adapter = ActiveModel::Serializer::Adapter::NullAdapter.new(object)
|
||||
adapter = ActiveModel::Serializer::Adapter::SimpleAdapter.new(object)
|
||||
|
||||
super(adapter, options)
|
||||
else
|
||||
|
||||
@ -3,6 +3,7 @@ module ActiveModel
|
||||
extend ActiveSupport::Autoload
|
||||
autoload :Configuration
|
||||
autoload :ArraySerializer
|
||||
autoload :Adapter
|
||||
include Configuration
|
||||
|
||||
class << self
|
||||
|
||||
20
lib/active_model/serializer/adapter.rb
Normal file
20
lib/active_model/serializer/adapter.rb
Normal file
@ -0,0 +1,20 @@
|
||||
module ActiveModel
|
||||
class Serializer
|
||||
class Adapter
|
||||
extend ActiveSupport::Autoload
|
||||
autoload :SimpleAdapter
|
||||
|
||||
def initialize(serializer)
|
||||
@attributes = serializer.attributes
|
||||
end
|
||||
|
||||
def serializable_hash(options = {})
|
||||
raise NotImplementedError, 'This is abstract method. Should be implemented at concrete adapter.'
|
||||
end
|
||||
|
||||
def to_json(options = {})
|
||||
raise NotImplementedError, 'This is abstract method. Should be implemented at concrete adapter.'
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -1,11 +1,7 @@
|
||||
module ActiveModel
|
||||
class Serializer
|
||||
class Adapter
|
||||
class NullAdapter
|
||||
def initialize(adapter)
|
||||
@attributes = adapter.attributes
|
||||
end
|
||||
|
||||
class SimpleAdapter < Adapter
|
||||
def to_json(options={})
|
||||
@attributes.each_with_object({}) do |(attr, value), h|
|
||||
h[attr] = value
|
||||
@ -1,7 +1,7 @@
|
||||
require "active_model"
|
||||
require "active_model/serializer/version"
|
||||
require "active_model/serializer"
|
||||
require "active_model/serializer/adapter/null_adapter"
|
||||
require "active_model/serializer/adapter/simple_adapter"
|
||||
|
||||
begin
|
||||
require 'action_controller'
|
||||
|
||||
@ -3,15 +3,15 @@ require 'test_helper'
|
||||
module ActiveModel
|
||||
class Serializer
|
||||
class Adapter
|
||||
class NullAdapterTest < Minitest::Test
|
||||
class SimpleAdapterTest < Minitest::Test
|
||||
def setup
|
||||
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
||||
@profile_serializer = ProfileSerializer.new(@profile)
|
||||
|
||||
@adapter = NullAdapter.new(@profile_serializer)
|
||||
@adapter = SimpleAdapter.new(@profile_serializer)
|
||||
end
|
||||
|
||||
def test_null_adapter
|
||||
def test_simple_adapter
|
||||
assert_equal('{"name":"Name 1","description":"Description 1"}',
|
||||
@adapter.to_json)
|
||||
|
||||
25
test/adapter_test.rb
Normal file
25
test/adapter_test.rb
Normal file
@ -0,0 +1,25 @@
|
||||
require 'test_helper'
|
||||
|
||||
module ActiveModel
|
||||
class Serializer
|
||||
class AdapterTest < Minitest::Test
|
||||
def setup
|
||||
profile = Profile.new
|
||||
serializer = ProfileSerializer.new(profile)
|
||||
@adapter = ActiveModel::Serializer::Adapter.new(serializer)
|
||||
end
|
||||
|
||||
def test_serializable_hash_is_abstract_method
|
||||
assert_raises(NotImplementedError) do
|
||||
@adapter.serializable_hash(only: [:name])
|
||||
end
|
||||
end
|
||||
|
||||
def test_serializable_hash_is_abstract_method
|
||||
assert_raises(NotImplementedError) do
|
||||
@adapter.to_json(only: [:name])
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Reference in New Issue
Block a user