Skip to content

Commit

Permalink
Clean up Serializer#build interface and Controller usage
Browse files Browse the repository at this point in the history
  • Loading branch information
bf4 committed Jun 12, 2015
1 parent f2958c1 commit 81eb1f0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
17 changes: 9 additions & 8 deletions lib/action_controller/serialization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,16 @@ def use_adapter?

[:_render_option_json, :_render_with_renderer_json].each do |renderer_method|
define_method renderer_method do |resource, options|
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)
serializable_resource = ActiveModel::Serializer::build(resource, options) do |builder|
if builder.serializer?
builder.serialization_scope ||= serialization_scope
builder.serialization_scope_name = _serialization_scope
builder.adapter
else
resource
end
end
super(serializable_resource, options)
end
end

Expand Down
9 changes: 7 additions & 2 deletions lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,15 @@ def self.inherited(base)
end

# Primary interface to building a serializer (with adapter)
# If no block is given, returns the adapter, ready for #as_json/#to_json
# Otherwise, yields the builder and returns the contents of the block
def self.build(resource, options = {})
builder = Builder.new(resource, options)
yield builder if block_given?
builder.adapter
if block_given?
yield builder
else
builder.adapter
end
end

def self.attributes(*attrs)
Expand Down
3 changes: 1 addition & 2 deletions test/serializers/meta_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@ def test_meta_is_present_on_arrays_with_root

def load_adapter(options)
options = options.merge(adapter: :json, serializer: AlternateBlogSerializer)
builder = ActiveModel::Serializer::Builder.new(@blog, options)
builder.adapter
ActiveModel::Serializer.build(@blog, options)
end
end
end
Expand Down

0 comments on commit 81eb1f0

Please sign in to comment.