Skip to content

Commit f00fe55

Browse files
author
Tema Bolshakov
committed
* Rename NullAdapter to SimpleAdapter
* Introduce abstract Adapter class * Organaze test structure to match convemtions
1 parent 0c13956 commit f00fe55

File tree

7 files changed

+52
-10
lines changed

7 files changed

+52
-10
lines changed

lib/action_controller/serialization.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def _render_option_json(resource, options)
1212
if serializer
1313
# omg hax
1414
object = serializer.new(resource)
15-
adapter = ActiveModel::Serializer::Adapter::NullAdapter.new(object)
15+
adapter = ActiveModel::Serializer::Adapter::SimpleAdapter.new(object)
1616

1717
super(adapter, options)
1818
else

lib/active_model/serializer.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ class Serializer
33
extend ActiveSupport::Autoload
44
autoload :Configuration
55
autoload :ArraySerializer
6+
autoload :Adapter
67
include Configuration
78

89
class << self
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module ActiveModel
2+
class Serializer
3+
class Adapter
4+
extend ActiveSupport::Autoload
5+
autoload :SimpleAdapter
6+
7+
def initialize(serializer)
8+
@attributes = serializer.attributes
9+
end
10+
11+
def serializable_hash(options = {})
12+
raise NotImplementedError, 'This is abstract method. Should be implemented at concrete adapter.'
13+
end
14+
15+
def to_json(options = {})
16+
raise NotImplementedError, 'This is abstract method. Should be implemented at concrete adapter.'
17+
end
18+
end
19+
end
20+
end

lib/active_model/serializer/adapter/null_adapter.rb renamed to lib/active_model/serializer/adapter/simple_adapter.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
module ActiveModel
22
class Serializer
33
class Adapter
4-
class NullAdapter
5-
def initialize(adapter)
6-
@attributes = adapter.attributes
7-
end
8-
4+
class SimpleAdapter < Adapter
95
def to_json(options={})
106
@attributes.each_with_object({}) do |(attr, value), h|
117
h[attr] = value

lib/active_model_serializers.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require "active_model"
22
require "active_model/serializer/version"
33
require "active_model/serializer"
4-
require "active_model/serializer/adapter/null_adapter"
4+
require "active_model/serializer/adapter/simple_adapter"
55

66
begin
77
require 'action_controller'

test/adapters/null_adapter_test.rb renamed to test/adapter/simple_adapter_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
module ActiveModel
44
class Serializer
55
class Adapter
6-
class NullAdapterTest < Minitest::Test
6+
class SimpleAdapterTest < Minitest::Test
77
def setup
88
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
99
@profile_serializer = ProfileSerializer.new(@profile)
1010

11-
@adapter = NullAdapter.new(@profile_serializer)
11+
@adapter = SimpleAdapter.new(@profile_serializer)
1212
end
1313

14-
def test_null_adapter
14+
def test_simple_adapter
1515
assert_equal('{"name":"Name 1","description":"Description 1"}',
1616
@adapter.to_json)
1717

test/adapter_test.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
require 'test_helper'
2+
3+
module ActiveModel
4+
class Serializer
5+
class AdapterTest < Minitest::Test
6+
def setup
7+
profile = Profile.new
8+
serializer = ProfileSerializer.new(profile)
9+
@adapter = ActiveModel::Serializer::Adapter.new(serializer)
10+
end
11+
12+
def test_serializable_hash_is_abstract_method
13+
assert_raises(NotImplementedError) do
14+
@adapter.serializable_hash(only: [:name])
15+
end
16+
end
17+
18+
def test_serializable_hash_is_abstract_method
19+
assert_raises(NotImplementedError) do
20+
@adapter.to_json(only: [:name])
21+
end
22+
end
23+
end
24+
end
25+
end

0 commit comments

Comments
 (0)