Skip to content

Commit c6f8d0f

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

File tree

15 files changed

+36
-35
lines changed

15 files changed

+36
-35
lines changed

.rubocop.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ AllCops:
1010

1111
Style/IndentationConsistency:
1212
Exclude:
13-
- lib/active_model/serializer/adapter/flatten_json.rb
13+
- lib/active_model/serializer/adapter/attributes.rb
1414
- lib/active_model/serializer/adapter/fragment_cache.rb
1515
- lib/active_model/serializer/adapter/json.rb
1616
- lib/active_model/serializer/adapter/json/fragment_cache.rb
@@ -21,7 +21,7 @@ Style/IndentationConsistency:
2121

2222
Style/IndentationWidth:
2323
Exclude:
24-
- lib/active_model/serializer/adapter/flatten_json.rb
24+
- lib/active_model/serializer/adapter/attributes.rb
2525
- lib/active_model/serializer/adapter/fragment_cache.rb
2626
- lib/active_model/serializer/adapter/json.rb
2727
- lib/active_model/serializer/adapter/json/fragment_cache.rb
@@ -32,7 +32,7 @@ Style/IndentationWidth:
3232

3333
Style/AccessModifierIndentation:
3434
Exclude:
35-
- lib/active_model/serializer/adapter/flatten_json.rb
35+
- lib/active_model/serializer/adapter/attributes.rb
3636
- lib/active_model/serializer/adapter/fragment_cache.rb
3737
- lib/active_model/serializer/adapter/json.rb
3838
- lib/active_model/serializer/adapter/json/fragment_cache.rb

README.md

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

15-
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).
15+
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).
1616
Check how to change the adapter in the sections bellow.
1717

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

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

7171
```ruby
7272
ActiveModel::Serializer.config.adapter = :json
@@ -137,7 +137,7 @@ render json: @post, meta: { total: 10 }, meta_key: "custom_meta"
137137
```
138138

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

142142
### Using a serializer without `render`
143143

@@ -190,7 +190,7 @@ end
190190

191191
### Built in Adapters
192192

193-
#### FlattenJSON
193+
#### Attributes
194194

195195
It's the default adapter, it generates a json response without a root key.
196196
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.
@@ -51,7 +51,7 @@ ActiveModel::Serializer.config.adapter = :json_api
5151
```
5252

5353
If you want to have a root key for each resource in your responses, you should use the Json or
54-
JsonApi adapters instead of the default FlattenJson:
54+
JsonApi adapters instead of the default Attributes:
5555

5656
```ruby
5757
ActiveModel::Serializer.config.adapter = :json

docs/general/configuration_options.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ The following configuration options can be set on `ActiveModel::Serializer.confi
44

55
## General
66

7-
- `adapter`: The [adapter](adapters.md) to use. Possible values: `:flatten_json, :json, :json_api`. Default: `:flatten_json`.
7+
- `adapter`: The [adapter](adapters.md) to use. Possible values: `:attributes, :json, :json_api`. Default: `:attributes`.
88

99
## JSON API
1010

docs/howto/add_pagination_links.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,6 @@ ex.
110110
}
111111
```
112112

113-
### FlattenJSON adapter
113+
### Attributes adapter
114114

115115
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: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ class Adapter
55
ADAPTER_MAP = {}
66
private_constant :ADAPTER_MAP if defined?(private_constant)
77
extend ActiveSupport::Autoload
8+
autoload :Attributes
9+
autoload :Null
810
autoload :FragmentCache
911
autoload :Json
1012
autoload :JsonApi
11-
autoload :Null
12-
autoload :FlattenJson
1313
autoload :CachedSerializer
1414

1515
def self.create(resource, options = {})
@@ -74,7 +74,8 @@ def lookup(adapter)
7474
# @api private
7575
def find_by_name(adapter_name)
7676
adapter_name = adapter_name.to_s.classify.tr('API', 'Api')
77-
ActiveModel::Serializer::Adapter.const_get(adapter_name.to_sym) or # rubocop:disable Style/AndOr
77+
"ActiveModel::Serializer::Adapter::#{adapter_name}".safe_constantize ||
78+
"ActiveModel::Serializer::Adapter::#{adapter_name.pluralize}".safe_constantize or # rubocop:disable Style/AndOr
7879
fail UnknownAdapterError
7980
end
8081
private :find_by_name

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,8 +1,8 @@
1-
class ActiveModel::Serializer::Adapter::FlattenJson < ActiveModel::Serializer::Adapter::Json
1+
class ActiveModel::Serializer::Adapter::Attributes < ActiveModel::Serializer::Adapter
22
def serializable_hash(options = nil)
33
options ||= {}
44
if serializer.respond_to?(:each)
5-
result = serializer.map { |s| FlattenJson.new(s).serializable_hash(options) }
5+
result = serializer.map { |s| Attributes.new(s).serializable_hash(options) }
66
else
77
hash = {}
88

@@ -43,7 +43,7 @@ def fragment_cache(cached_hash, non_cached_hash)
4343

4444
private
4545

46-
# no-op: FlattenJson adapter does not include meta data, because it does not support root.
46+
# no-op: Attributes adapter does not include meta data, because it does not support root.
4747
def include_meta(json)
4848
json
4949
end

lib/active_model/serializer/adapter/json.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class ActiveModel::Serializer::Adapter::Json < ActiveModel::Serializer::Adapter
44

55
def serializable_hash(options = nil)
66
options ||= {}
7-
{ root => FlattenJson.new(serializer).serializable_hash(options) }
7+
{ root => Attributes.new(serializer).serializable_hash(options) }
88
end
99

1010
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

0 commit comments

Comments
 (0)