Skip to content

Commit fae5e0e

Browse files
committed
Rename FlattenJson to Attributes (allow plural adapter names)
1 parent f5ed923 commit fae5e0e

File tree

13 files changed

+25
-24
lines changed

13 files changed

+25
-24
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ AMS does this through two components: **serializers** and **adapters**.
1010
Serializers describe _which_ attributes and relationships should be serialized.
1111
Adapters describe _how_ attributes and relationships should be serialized.
1212

13-
By default AMS will use the **Flatten Json Adapter**. But we strongly advise you to use **JsonApi Adapter** that follows 1.0 of the format specified in [jsonapi.org/format](http://jsonapi.org/format).
13+
By default AMS will use the **Attributes Adapter**. But we strongly advise you to use **JsonApi Adapter** that follows 1.0 of the format specified in [jsonapi.org/format](http://jsonapi.org/format).
1414
Check how to change the adapter in the sections bellow.
1515

1616
# RELEASE CANDIDATE, PLEASE READ
@@ -68,7 +68,7 @@ ActiveModel::Serializer.config.adapter = :json_api
6868
You won't need to implement an adapter unless you wish to use a new format or
6969
media type with AMS.
7070

71-
If you want to have a root key on your responses you should use the Json adapter, instead of the default FlattenJson:
71+
If you want to have a root key on your responses you should use the Json adapter, instead of the default Attributes:
7272

7373
```ruby
7474
ActiveModel::Serializer.config.adapter = :json
@@ -138,7 +138,7 @@ The key can be customized using `meta_key` option.
138138
render json: @post, meta: { total: 10 }, meta_key: "custom_meta"
139139
```
140140

141-
`meta` will only be included in your response if you are using an Adapter that supports `root`, as JsonAPI and Json adapters, the default adapter (FlattenJson) doesn't have `root`.
141+
`meta` will only be included in your response if you are using an Adapter that supports `root`, as JsonAPI and Json adapters, the default adapter (Attributes) doesn't have `root`.
142142

143143
### Overriding association methods
144144

@@ -174,7 +174,7 @@ end
174174

175175
### Built in Adapters
176176

177-
#### FlattenJSON
177+
#### Attributes
178178

179179
It's the default adapter, it generates a json response without a root key.
180180
Doesn't follow any specifc convention.

docs/general/adapters.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
AMS does this through two components: **serializers** and **adapters**.
44
Serializers describe _which_ attributes and relationships should be serialized.
55
Adapters describe _how_ attributes and relationships should be serialized.
6-
You can use one of the built-in adapters (```FlattenJSON``` is the default one) or create one by yourself, but you won't need to implement an adapter unless you wish to use a new format or media type with AMS.
6+
You can use one of the built-in adapters (```Attributes``` is the default one) or create one by yourself, but you won't need to implement an adapter unless you wish to use a new format or media type with AMS.
77

88
## Built in Adapters
99

10-
### FlattenJSON - Default
10+
### Attributes - Default
1111

1212
It's the default adapter, it generates a json response without a root key.
1313
Doesn't follow any specifc convention.
@@ -44,7 +44,7 @@ or
4444
ActiveModel::Serializer.config.adapter = :json_api
4545
```
4646

47-
If you want to have a root key in your responses you should use the Json adapter, instead of the default FlattenJson:
47+
If you want to have a root key in your responses you should use the Json adapter, instead of the default Attributes:
4848

4949
```ruby
5050
ActiveModel::Serializer.config.adapter = :json

docs/howto/add_pagination_links.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,6 @@ ex.
107107
}
108108
```
109109

110-
### FlattenJSON adapter
110+
### Attributes adapter
111111

112112
This adapter does not allow us to use `meta` key, due to that it is not possible to add pagination links.

docs/howto/add_root_key.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# How to add root key
22

3-
Add the root key to your API is quite simple with AMS. The **Adapter** is what determines the format of your JSON response. The default adapter is the ```FlattenJSON``` which doesn't have the root key, so your response is something similar to:
3+
Add the root key to your API is quite simple with AMS. The **Adapter** is what determines the format of your JSON response. The default adapter is the ```Attributes``` which doesn't have the root key, so your response is something similar to:
44

55
```json
66
{

lib/active_model/serializer/adapter.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class Adapter
55
autoload :CacheCheck
66
require 'active_model/serializer/adapter/json'
77
require 'active_model/serializer/adapter/json_api'
8-
autoload :FlattenJson
8+
autoload :Attributes
99
autoload :Null
1010
autoload :FragmentCache
1111

@@ -19,7 +19,8 @@ def self.create(resource, options = {})
1919

2020
def self.adapter_class(adapter)
2121
adapter_name = adapter.to_s.classify.sub("API", "Api")
22-
"ActiveModel::Serializer::Adapter::#{adapter_name}".safe_constantize
22+
"ActiveModel::Serializer::Adapter::#{adapter_name}".safe_constantize ||
23+
"ActiveModel::Serializer::Adapter::#{adapter_name.pluralize}".safe_constantize
2324
end
2425

2526
attr_reader :serializer

lib/active_model/serializer/adapter/flatten_json.rb renamed to lib/active_model/serializer/adapter/attributes.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
module ActiveModel
22
class Serializer
33
class Adapter
4-
class FlattenJson < Adapter
4+
class Attributes < Adapter
55

66
def serializable_hash(options = nil)
77
options ||= {}
88
if serializer.respond_to?(:each)
9-
@result = serializer.map { |s| FlattenJson.new(s).serializable_hash(options) }
9+
@result = serializer.map { |s| Attributes.new(s).serializable_hash(options) }
1010
else
1111
@hash = {}
1212

@@ -47,7 +47,7 @@ def fragment_cache(cached_hash, non_cached_hash)
4747

4848
private
4949

50-
# no-op: FlattenJson adapter does not include meta data, because it does not support root.
50+
# no-op: Attributes adapter does not include meta data, because it does not support root.
5151
def include_meta(json)
5252
json
5353
end

lib/active_model/serializer/adapter/json.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class Adapter
66
class Json < Adapter
77

88
def serializable_hash(options = {})
9-
{ root => FlattenJson.new(serializer).serializable_hash(options) }
9+
{ root => Adapter::Attributes.new(serializer).serializable_hash(options) }
1010
end
1111

1212
private

lib/active_model/serializer/configuration.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module Configuration
66

77
included do |base|
88
base.config.array_serializer = ActiveModel::Serializer::ArraySerializer
9-
base.config.adapter = :flatten_json
9+
base.config.adapter = :attributes
1010
base.config.jsonapi_resource_type = :plural
1111
end
1212
end

test/adapter_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def test_adapter_class_for_unknown_adapter
3131

3232
def test_create_adapter
3333
adapter = ActiveModel::Serializer::Adapter.create(@serializer)
34-
assert_equal ActiveModel::Serializer::Adapter::FlattenJson, adapter.class
34+
assert_equal ActiveModel::Serializer::Adapter::Attributes, adapter.class
3535
end
3636

3737
def test_create_adapter_with_override

test/serializers/adapter_for_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def teardown
1111

1212
def test_returns_default_adapter
1313
adapter = ActiveModel::Serializer.adapter
14-
assert_equal ActiveModel::Serializer::Adapter::FlattenJson, adapter
14+
assert_equal ActiveModel::Serializer::Adapter::Attributes, adapter
1515
end
1616

1717
def test_overwrite_adapter_with_symbol

0 commit comments

Comments
 (0)