Skip to content

Commit

Permalink
Rename FlattenJson to Attributes (allow plural adapter names)
Browse files Browse the repository at this point in the history
  • Loading branch information
bf4 committed Sep 4, 2015
1 parent f5ed923 commit fae5e0e
Show file tree
Hide file tree
Showing 13 changed files with 25 additions and 24 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ AMS does this through two components: **serializers** and **adapters**.
Serializers describe _which_ attributes and relationships should be serialized.
Adapters describe _how_ attributes and relationships should be serialized.

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

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

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

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

`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`.
`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`.

### Overriding association methods

Expand Down Expand Up @@ -174,7 +174,7 @@ end

### Built in Adapters

#### FlattenJSON
#### Attributes

It's the default adapter, it generates a json response without a root key.
Doesn't follow any specifc convention.
Expand Down
6 changes: 3 additions & 3 deletions docs/general/adapters.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
AMS does this through two components: **serializers** and **adapters**.
Serializers describe _which_ attributes and relationships should be serialized.
Adapters describe _how_ attributes and relationships should be serialized.
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.
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.

## Built in Adapters

### FlattenJSON - Default
### Attributes - Default

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

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

```ruby
ActiveModel::Serializer.config.adapter = :json
Expand Down
2 changes: 1 addition & 1 deletion docs/howto/add_pagination_links.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,6 @@ ex.
}
```

### FlattenJSON adapter
### Attributes adapter

This adapter does not allow us to use `meta` key, due to that it is not possible to add pagination links.
2 changes: 1 addition & 1 deletion docs/howto/add_root_key.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# How to add root key

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:
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:

```json
{
Expand Down
5 changes: 3 additions & 2 deletions lib/active_model/serializer/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Adapter
autoload :CacheCheck
require 'active_model/serializer/adapter/json'
require 'active_model/serializer/adapter/json_api'
autoload :FlattenJson
autoload :Attributes
autoload :Null
autoload :FragmentCache

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

def self.adapter_class(adapter)
adapter_name = adapter.to_s.classify.sub("API", "Api")
"ActiveModel::Serializer::Adapter::#{adapter_name}".safe_constantize
"ActiveModel::Serializer::Adapter::#{adapter_name}".safe_constantize ||
"ActiveModel::Serializer::Adapter::#{adapter_name.pluralize}".safe_constantize
end

attr_reader :serializer
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
module ActiveModel
class Serializer
class Adapter
class FlattenJson < Adapter
class Attributes < Adapter

def serializable_hash(options = nil)
options ||= {}
if serializer.respond_to?(:each)
@result = serializer.map { |s| FlattenJson.new(s).serializable_hash(options) }
@result = serializer.map { |s| Attributes.new(s).serializable_hash(options) }
else
@hash = {}

Expand Down Expand Up @@ -47,7 +47,7 @@ def fragment_cache(cached_hash, non_cached_hash)

private

# no-op: FlattenJson adapter does not include meta data, because it does not support root.
# no-op: Attributes adapter does not include meta data, because it does not support root.
def include_meta(json)
json
end
Expand Down
2 changes: 1 addition & 1 deletion lib/active_model/serializer/adapter/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Adapter
class Json < Adapter

def serializable_hash(options = {})
{ root => FlattenJson.new(serializer).serializable_hash(options) }
{ root => Adapter::Attributes.new(serializer).serializable_hash(options) }
end

private
Expand Down
2 changes: 1 addition & 1 deletion lib/active_model/serializer/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Configuration

included do |base|
base.config.array_serializer = ActiveModel::Serializer::ArraySerializer
base.config.adapter = :flatten_json
base.config.adapter = :attributes
base.config.jsonapi_resource_type = :plural
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/adapter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def test_adapter_class_for_unknown_adapter

def test_create_adapter
adapter = ActiveModel::Serializer::Adapter.create(@serializer)
assert_equal ActiveModel::Serializer::Adapter::FlattenJson, adapter.class
assert_equal ActiveModel::Serializer::Adapter::Attributes, adapter.class
end

def test_create_adapter_with_override
Expand Down
2 changes: 1 addition & 1 deletion test/serializers/adapter_for_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def teardown

def test_returns_default_adapter
adapter = ActiveModel::Serializer.adapter
assert_equal ActiveModel::Serializer::Adapter::FlattenJson, adapter
assert_equal ActiveModel::Serializer::Adapter::Attributes, adapter
end

def test_overwrite_adapter_with_symbol
Expand Down
2 changes: 1 addition & 1 deletion test/serializers/attribute_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def test_json_serializable_hash
def test_attribute_inheritance_with_key
inherited_klass = Class.new(AlternateBlogSerializer)
blog_serializer = inherited_klass.new(@blog)
adapter = ActiveModel::Serializer::Adapter::FlattenJson.new(blog_serializer)
adapter = ActiveModel::Serializer::Adapter::Attributes.new(blog_serializer)
assert_equal({:id=>1, :title=>"AMS Hints"}, adapter.serializable_hash)
end

Expand Down
2 changes: 1 addition & 1 deletion test/serializers/configuration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def test_array_serializer
end

def test_default_adapter
assert_equal :flatten_json, ActiveModel::Serializer.config.adapter
assert_equal :attributes, ActiveModel::Serializer.config.adapter
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions test/serializers/meta_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def test_meta_is_present_with_root
end

def test_meta_is_not_included_when_root_is_missing
# load_adapter uses FlattenJson Adapter
# load_adapter uses Attributes Adapter
adapter = load_adapter(meta: {total: 10})
expected = {
id: 1,
Expand Down Expand Up @@ -67,8 +67,8 @@ def test_meta_key_is_used_with_json_api

def test_meta_is_not_present_on_arrays_without_root
serializer = ArraySerializer.new([@blog], meta: {total: 10})
# FlattenJSON doesn't have support to root
adapter = ActiveModel::Serializer::Adapter::FlattenJson.new(serializer)
# Attributes doesn't have support to root
adapter = ActiveModel::Serializer::Adapter::Attributes.new(serializer)
expected = [{
id: 1,
name: "AMS Hints",
Expand Down Expand Up @@ -113,7 +113,7 @@ def test_meta_is_present_on_arrays_with_root
private

def load_adapter(options)
options = options.merge(adapter: :flatten_json, serializer: AlternateBlogSerializer)
options = options.merge(adapter: :attributes, serializer: AlternateBlogSerializer)
ActiveModel::SerializableResource.new(@blog, options)
end
end
Expand Down

0 comments on commit fae5e0e

Please sign in to comment.