Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,20 @@ end
If you find that your project is already relying on the old rails to_json
change `render :json` to `render json: @your_object.to_json`.

# Custom MIME Types

If you're dealing in a custom MIME Type (say, `application/vnd.api+json` or
similar) and you want `ActiveModel::Serializers` to respond with the correct
Content-Type header, just pass the `Mime::Type` object to
`ActiveModel::Serializer.register_mime_type`. If you're registering custom MIME
Types in a Rails initializer, you'll want to make sure the type is registered
first, then send it to `ActiveModel::Serializers`.

```ruby
Mime::Type.register('application/flergle+json', :flergle)
ActiveModel::Serializer.register_mime_type(Mime::FLERGLE)
```

# Attributes and Associations

Once you have a serializer, you can specify which attributes and associations
Expand Down
13 changes: 13 additions & 0 deletions lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,19 @@ def build_json(controller, resource, options)

serializer.new(resource, options)
end

def register_mime_type(mime_type)
ActionController.add_renderer(mime_type.symbol) do |resource, options|
serialized = ActiveModel::Serializer.build_json(self, resource, options)

if serialized
self.content_type ||= mime_type
serialized.to_json
else
_render_option_json(resource, options)
end
end
end
end

attr_reader :object, :options
Expand Down
13 changes: 13 additions & 0 deletions test/serialization_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ def render_json_empty_array
render json: []
end

def render_custom_mime_type_empty_array
render flergle: []
end

def render_json_array_with_custom_array_serializer
render json: [], serializer: CustomArraySerializer
end
Expand Down Expand Up @@ -373,6 +377,15 @@ def test_render_json_empty_array
assert_equal '{"test":[]}', @response.body
end

def test_render_custom_mime_type_empty_array
Mime::Type.register('application/flergle+json', :flergle)
ActiveModel::Serializer.register_mime_type(Mime::FLERGLE)

get :render_custom_mime_type_empty_array
assert_equal '{"test":[]}', @response.body
assert_equal 'application/flergle+json', @response.content_type
end

def test_render_json_empty_array_checking_default_root
get :render_json_empty_array, check_default_root: true
assert_equal '{"awesome":[]}', @response.body
Expand Down