-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow overriding the adapter with render option
Make it easy to use multiple adapters in an app. use "adapter: false" to not use ams make a test override config.adapter
- Loading branch information
Showing
8 changed files
with
99 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,29 +6,32 @@ module Serialization | |
|
||
include ActionController::Renderers | ||
|
||
ADAPTER_OPTION_KEYS = [:include, :root] | ||
ADAPTER_OPTION_KEYS = [:include, :root, :adapter] | ||
|
||
def get_serializer(resource, options) | ||
@_serializer ||= options.delete(:serializer) | ||
def get_serializer(resource) | ||
This comment has been minimized.
Sorry, something went wrong. |
||
@_serializer ||= @_serializer_opts.delete(:serializer) | ||
@_serializer ||= ActiveModel::Serializer.serializer_for(resource) | ||
|
||
if options.key?(:each_serializer) | ||
options[:serializer] = options.delete(:each_serializer) | ||
if @_serializer_opts.key?(:each_serializer) | ||
@_serializer_opts[:serializer] = @_serializer_opts.delete(:each_serializer) | ||
end | ||
|
||
@_serializer | ||
end | ||
|
||
def use_adapter? | ||
!(@_adapter_opts.key?(:adapter) && !@_adapter_opts[:adapter]) | ||
end | ||
|
||
[:_render_option_json, :_render_with_renderer_json].each do |renderer_method| | ||
define_method renderer_method do |resource, options| | ||
|
||
adapter_opts, serializer_opts = | ||
@_adapter_opts, @_serializer_opts = | ||
This comment has been minimized.
Sorry, something went wrong.
jejacks0n
|
||
options.partition { |k, _| ADAPTER_OPTION_KEYS.include? k }.map { |h| Hash[h] } | ||
|
||
if (serializer = get_serializer(resource, serializer_opts)) | ||
if use_adapter? && (serializer = get_serializer(resource)) | ||
# omg hax | ||
object = serializer.new(resource, serializer_opts) | ||
adapter = ActiveModel::Serializer.adapter.new(object, adapter_opts) | ||
object = serializer.new(resource, @_serializer_opts) | ||
adapter = ActiveModel::Serializer::Adapter.create(object, @_adapter_opts) | ||
super(adapter, options) | ||
else | ||
super(resource, options) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
require 'test_helper' | ||
|
||
module ActionController | ||
module Serialization | ||
class AdapterSelectorTest < ActionController::TestCase | ||
class MyController < ActionController::Base | ||
def render_using_default_adapter | ||
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }) | ||
render json: @profile | ||
end | ||
|
||
def render_using_adapter_override | ||
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }) | ||
render json: @profile, adapter: :json_api | ||
end | ||
|
||
def render_skipping_adapter | ||
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }) | ||
render json: @profile, adapter: false | ||
end | ||
end | ||
|
||
tests MyController | ||
|
||
def test_render_using_default_adapter | ||
get :render_using_default_adapter | ||
assert_equal '{"name":"Name 1","description":"Description 1"}', response.body | ||
end | ||
|
||
def test_render_using_adapter_override | ||
get :render_using_adapter_override | ||
assert_equal '{"profiles":{"name":"Name 1","description":"Description 1"}}', response.body | ||
end | ||
|
||
def test_render_skipping_adapter | ||
get :render_skipping_adapter | ||
assert_equal '{"attributes":{"name":"Name 1","description":"Description 1","comments":"Comments 1"}}', response.body | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
I like how this can now be overridden via the controller. This allows me to version my serializers based on controller module namespace or any other logic within the controller. Thanks for that.