Skip to content

Commit

Permalink
Encapsulate serialization in ActiveModel::Serializer::Builder
Browse files Browse the repository at this point in the history
Usage: ActiveModel::Serializer.build(resource, options)
  • Loading branch information
bf4 committed Jun 12, 2015
1 parent 460150f commit 5b0a4c3
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 41 deletions.
50 changes: 25 additions & 25 deletions lib/action_controller/serialization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Serialization

include ActionController::Renderers

ADAPTER_OPTION_KEYS = [:include, :fields, :root, :adapter]
ADAPTER_OPTION_KEYS = ActiveModel::Serializer::Builder::ADAPTER_OPTION_KEYS

included do
class_attribute :_serialization_scope
Expand All @@ -18,46 +18,46 @@ def serialization_scope
respond_to?(_serialization_scope, true)
end

# Deprecated
def get_serializer(resource)
@_serializer ||= @_serializer_opts.delete(:serializer)
@_serializer ||= ActiveModel::Serializer.serializer_for(resource)

if @_serializer_opts.key?(:each_serializer)
@_serializer_opts[:serializer] = @_serializer_opts.delete(:each_serializer)
end

@_serializer
fail ActiveModel::Serializer::Error, "ActionController::Serialization##{__method__} has been removed. "\
"Please use ActiveSupport::Serializer#build instead"
end

# Deprecated
def use_adapter?
!(@_adapter_opts.key?(:adapter) && !@_adapter_opts[:adapter])
fail ActiveModel::Serializer::Error, "ActionController::Serialization##{__method__} has been removed. "\
"Please use ActiveSupport::Serializer#build instead"
end

[:_render_option_json, :_render_with_renderer_json].each do |renderer_method|
define_method renderer_method do |resource, options|
@_adapter_opts, @_serializer_opts =
options.partition { |k, _| ADAPTER_OPTION_KEYS.include? k }.map { |h| Hash[h] }

if use_adapter? && (serializer = get_serializer(resource))

@_serializer_opts[:scope] ||= serialization_scope
@_serializer_opts[:scope_name] = _serialization_scope

# omg hax
object = serializer.new(resource, @_serializer_opts)
adapter = ActiveModel::Serializer::Adapter.create(object, @_adapter_opts)
builder = ActiveModel::Serializer::Builder.new(resource, options)
if builder.serializer?
builder.serialization_scope ||= serialization_scope
builder.serialization_scope_name = _serialization_scope
adapter = builder.adapter
super(adapter, options)
else
super(resource, options)
end
end
end

# Tries to rescue the exception by looking up and calling a registered handler.
# TODO: Either Decorate 'exception' and define #handle_error where it is serialized
# For example:
# class ExceptionModel
# include ActiveModel::Serialization
# def initialize(exception)
# # etc
# end
# def handle_error(exception)
# exception_model = ActiveModel::Serializer.build_exception_model({ errors: ['Internal Server Error'] })
# render json: exception_model, status: :internal_server_error
# end
# OR remove method as it doesn't do anything right now.
def rescue_with_handler(exception)
@_serializer = nil
@_serializer_opts = nil
@_adapter_opts = nil

super(exception)
end

Expand Down
9 changes: 9 additions & 0 deletions lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

module ActiveModel
class Serializer
Error = Class.new(StandardError)
extend ActiveSupport::Autoload
autoload :Configuration
autoload :ArraySerializer
autoload :Adapter
autoload :Builder
include Configuration

class << self
Expand All @@ -28,6 +30,13 @@ def self.inherited(base)
base._urls = []
end

# Primary interface to building a serializer (with adapter)
def self.build(resource, options = {})
builder = Builder.new(resource, options)
yield builder if block_given?
builder.adapter
end

def self.attributes(*attrs)
attrs = attrs.first if attrs.first.class == Array
@_attributes.concat attrs
Expand Down
69 changes: 69 additions & 0 deletions lib/active_model/serializer/builder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
require "set"
module ActiveModel
class Serializer
class Builder
extend ActiveSupport::Autoload

ADAPTER_OPTION_KEYS = Set.new([:include, :fields, :root, :adapter])

def initialize(resource, options = {})
@resource = resource
@adapter_opts, @serializer_opts =
options.partition { |k, _| ADAPTER_OPTION_KEYS.include? k }.map { |h| Hash[h] }
end

def serialization_scope=(scope)
serializer_opts[:scope] = scope
end

def serialization_scope
serializer_opts[:scope]
end

def serialization_scope_name=(scope_name)
serializer_opts[:scope_name] = scope_name
end

def adapter
@adapter ||= ActiveModel::Serializer::Adapter.create(serializer_instance, adapter_opts)
end
alias_method :adapter_instance, :adapter

def serializer_instance
@serializer_instance ||= serializer.new(resource, serializer_opts)
end

# Get serializer either explicitly :serializer or implicitly from resource
# Remove :serializer key from serializer_opts
# Replace :serializer key with :each_serializer if present
def serializer
@serializer ||=
begin
@serializer = serializer_opts.delete(:serializer)
@serializer ||= ActiveModel::Serializer.serializer_for(resource)

if serializer_opts.key?(:each_serializer)
serializer_opts[:serializer] = serializer_opts.delete(:each_serializer)
end
@serializer
end
end
alias_method :serializer_class, :serializer

# True when no explicit adapter given, or explicit appear is truthy (non-nil)
# False when explicit adapter is falsy (nil or false)
def use_adapter?
!(adapter_opts.key?(:adapter) && !adapter_opts[:adapter])
end

def serializer?
use_adapter? && !!(serializer)
end

private

attr_reader :resource, :adapter_opts, :serializer_opts

end
end
end
6 changes: 3 additions & 3 deletions test/action_controller/rescue_from_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ class MyController < ActionController::Base
rescue_from Exception, with: :handle_error

def render_using_raise_error_serializer
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
render json: [@profile], serializer: RaiseErrorSerializer
profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
render json: [profile], serializer: RaiseErrorSerializer
end

def handle_error(exception)
Expand All @@ -25,7 +25,7 @@ def test_rescue_from
errors: ['Internal Server Error']
}.to_json

assert_equal expected, @response.body
assert_equal expected, response.body
end
end
end
Expand Down
19 changes: 15 additions & 4 deletions test/action_controller/serialization_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,7 @@ def render_fragment_changed_object_with_relationship

private
def generate_cached_serializer(obj)
serializer_class = ActiveModel::Serializer.serializer_for(obj)
serializer = serializer_class.new(obj)
adapter = ActiveModel::Serializer.adapter.new(serializer)
adapter.to_json
ActiveModel::Serializer.build(obj).to_json
end

def with_adapter(adapter)
Expand Down Expand Up @@ -405,6 +402,20 @@ def test_cache_expiration_on_update
assert_equal 'application/json', @response.content_type
assert_equal expected.to_json, @response.body
end

def test_fail_calling_use_adapter_on_controller_instance
controller = MyController.new
assert_raises(ActiveModel::Serializer::Error) {
controller.use_adapter?
}
end

def test_fail_calling_get_serializer__on_controller_instance
controller = MyController.new
assert_raises(ActiveModel::Serializer::Error) {
controller.get_serializer(Class.new)
}
end
end
end
end
5 changes: 1 addition & 4 deletions test/serializers/cache_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,7 @@ def test_fragment_fetch_with_virtual_associations

private
def render_object_with_cache(obj)
serializer_class = ActiveModel::Serializer.serializer_for(obj)
serializer = serializer_class.new(obj)
adapter = ActiveModel::Serializer.adapter.new(serializer)
adapter.serializable_hash
ActiveModel::Serializer.build(obj).serializable_hash
end
end
end
Expand Down
8 changes: 3 additions & 5 deletions test/serializers/meta_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,9 @@ def test_meta_is_present_on_arrays_with_root
private

def load_adapter(options)
adapter_opts, serializer_opts =
options.partition { |k, _| ActionController::Serialization::ADAPTER_OPTION_KEYS.include? k }.map { |h| Hash[h] }

serializer = AlternateBlogSerializer.new(@blog, serializer_opts)
ActiveModel::Serializer::Adapter::Json.new(serializer, adapter_opts)
options = options.merge(adapter: :json, serializer: AlternateBlogSerializer)
builder = ActiveModel::Serializer::Builder.new(@blog, options)
builder.adapter
end
end
end
Expand Down

0 comments on commit 5b0a4c3

Please sign in to comment.