Skip to content

Commit a66f53c

Browse files
committed
Improvements on media type configs
By default, AMS responds to media type application/json and application/vnd.api+json using the FlattenJson and JsonApi adapters respectively. If you want to add a different media type, override the defaults or even create your own, you can do it in your initializer.
1 parent e89a815 commit a66f53c

File tree

5 files changed

+31
-16
lines changed

5 files changed

+31
-16
lines changed

README.md

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,36 @@ end
5151

5252
Generally speaking, you as a user of AMS will write (or generate) these
5353
serializer classes. If you want to use a different adapter, such as a JsonApi, you can
54-
change this in an initializer:
54+
change this in your render, or in an initializer:
5555

5656
```ruby
57+
#class
5758
ActiveModel::Serializer.config.adapter = ActiveModel::Serializer::Adapter::JsonApi
58-
```
5959

60+
#symbol
61+
ActiveModel::Serializer.config.adapter = :json_api
62+
```
6063
or
6164

6265
```ruby
63-
ActiveModel::Serializer.config.adapter = :json_api
66+
render json: @posts, adapter: :json_api
67+
```
68+
69+
You can use `Accept` HTTP header-field to select a specific adapter by enabling it.
70+
71+
```ruby
72+
ActiveModel::Serializer.config.enabled_adapters_by_media_type = true
73+
```
74+
By default, AMS responds to media type `application/json` and `application/vnd.api+json` using the `FlattenJson` and `JsonApi` adapters respectively. If you want to add a different media type, override the defaults or even create your own, you can do it in your initializer.
75+
76+
To override media type `application/json` to respond with `Json` instead of `FlattenJson` adapter:
77+
```ruby
78+
ActiveModel::Serializer.config.custom_media_type_adapters = {"application/json" => :json}
79+
```
80+
81+
If you have a custom adapter (`xml`, as example) you can create your own media type:
82+
```ruby
83+
ActiveModel::Serializer.config.custom_media_type_adapters = {"application/xml" => :xml}
6484
```
6585

6686
You won't need to implement an adapter unless you wish to use a new format or

lib/action_controller/serialization.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def get_serializer(resource, options = {})
2626
options[:adapter] = false
2727
end
2828

29-
unless options[:adapter]
29+
if ActiveModel::Serializer.enabled_adapters_by_media_type && !options.fetch(:adapter, nil)
3030
adapter = ActiveModel::Serializer::Adapter.by_request(request)
3131
options[:adapter] ||= adapter if adapter
3232
end

lib/active_model/serializer.rb

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -104,20 +104,12 @@ def self.adapter
104104
adapter_class
105105
end
106106

107-
def self.custom_media_type_adapters=(media_type_adapters)
108-
config.custom_media_type_adapters ||= {}
109-
110-
unless media_type_adatets.is_a? Array
111-
media_types_adapters = [media_types_adapters]
112-
end
113-
114-
media_types_adapters.each do |media_type_adapter|
115-
config.custom_media_type_adapters.merge!(media_type_adapter)
116-
end
107+
def self.custom_media_type_adapters
108+
config.custom_media_type_adapters
117109
end
118110

119-
def self.custom_media_type_adapters
120-
config.custom_media_type_adapters ||= {}
111+
def self.enabled_adapters_by_media_type
112+
config.enabled_adapters_by_media_type
121113
end
122114

123115
def self.root_name

lib/active_model/serializer/configuration.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ module Configuration
77
included do |base|
88
base.config.array_serializer = ActiveModel::Serializer::ArraySerializer
99
base.config.adapter = :flatten_json
10+
base.config.enabled_adapters_by_media_type = true
11+
base.config.custom_media_type_adapters = {}
1012
end
1113
end
1214
end

test/action_controller/adapter_selector_test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ def test_render_skipping_adapter
5555
end
5656

5757
def test_render_using_adapter_selected_by_accept_header_field
58+
ActiveModel::Serializer.config.enabled_adapters_by_media_type = true
5859
request.headers['Accept'] = "application/vnd.api+json"
5960

6061
get :render_selecting_adapter_by_accept_header_field

0 commit comments

Comments
 (0)