Skip to content

Commit

Permalink
Nest scope under context option
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrian Mugnolo and Santiago Pastorino authored and spastorino committed Jan 6, 2014
1 parent 8bd2542 commit 0d8ef2b
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 85 deletions.
2 changes: 1 addition & 1 deletion lib/action_controller/serialization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def build_json_serializer(resource, options = {})
options = default_serializer_options.merge(options)

if serializer = options.fetch(:serializer, ActiveModel::Serializer.serializer_for(resource))
options[:scope] = serialization_scope unless options.has_key?(:scope)
options[:context] = { scope: serialization_scope }.merge!(options[:context] || {})
options[:resource_name] = controller_name if resource.respond_to?(:to_ary)

serializer.new(resource, options)
Expand Down
7 changes: 3 additions & 4 deletions lib/active_model/array_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@ class << self

def initialize(object, options={})
@object = object
@context = options[:context]
@scope = options[:scope]
@context = options[:context] || {}
@root = options.fetch(:root, self.class._root)
@meta_key = options[:meta_key] || :meta
@meta = options[@meta_key]
@each_serializer = options[:each_serializer]
@resource_name = options[:resource_name]
end
attr_accessor :object, :context, :scope, :root, :meta_key, :meta
attr_accessor :object, :context, :root, :meta_key, :meta

def json_key
if root.nil?
Expand All @@ -34,7 +33,7 @@ def json_key

def serializer_for(item)
serializer_class = @each_serializer || Serializer.serializer_for(item) || DefaultSerializer
serializer_class.new(item, context: context, scope: scope)
serializer_class.new(item, context: context)
end

def serializable_object
Expand Down
7 changes: 3 additions & 4 deletions lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,13 @@ def associate(klass, *attrs)

def initialize(object, options={})
@object = object
@context = options[:context]
@scope = options[:scope]
@context = options[:context] || {}
@root = options.fetch(:root, self.class._root)
@meta_key = options[:meta_key] || :meta
@meta = options[@meta_key]
@wrap_in_array = options[:_wrap_in_array]
end
attr_accessor :object, :context, :scope, :root, :meta_key, :meta
attr_accessor :object, :context, :root, :meta_key, :meta

def json_key
if root == true || root.nil?
Expand Down Expand Up @@ -167,7 +166,7 @@ def embedded_in_root_associations

def build_serializer(association)
object = send(association.name)
association.build_serializer(object, context: context, scope: scope)
association.build_serializer(object, context: context)
end

def serialize(association)
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/poro.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class UserSerializer < ActiveModel::Serializer
class ProfileSerializer < ActiveModel::Serializer
def description
description = object.read_attribute_for_serialization(:description)
scope ? "#{description} - #{scope}" : description
context[:scope] ? "#{description} - #{context[:scope]}" : description
end

attributes :name, :description
Expand Down
4 changes: 2 additions & 2 deletions test/integration/action_controller/serialization_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def test_render_using_implicit_serializer_and_scope
class DefaultOptionsForSerializerScopeTest < ActionController::TestCase
class MyController < ActionController::Base
def default_serializer_options
{ scope: current_admin }
{ context: { scope: current_admin } }
end

def render_using_scope_set_in_default_serializer_options
Expand Down Expand Up @@ -73,7 +73,7 @@ def test_render_using_scope_set_in_default_serializer_options
class ExplicitSerializerScopeTest < ActionController::TestCase
class MyController < ActionController::Base
def render_using_implicit_serializer_and_explicit_scope
render json: Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }), scope: current_admin
render json: Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }), context: { scope: current_admin }
end

private
Expand Down
19 changes: 19 additions & 0 deletions test/unit/active_model/array_serializer/context_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,24 @@ def test_context_using_an_object
assert_equal(2, serializer.context.b)
end
end

class ScopeTest < ActiveModel::TestCase
def test_array_serializer_pass_context_to_item_serializers
array = [Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })]
serializer = ArraySerializer.new(array, context: { scope: current_user })

expected = [{ name: 'Name 1', description: 'Description 1 - user' },
{ name: 'Name 2', description: 'Description 2 - user' }]

assert_equal expected, serializer.serializable_array
end

private

def current_user
'user'
end
end
end
end
24 changes: 0 additions & 24 deletions test/unit/active_model/array_serializer/scope_test.rb

This file was deleted.

27 changes: 27 additions & 0 deletions test/unit/active_model/serializer/context_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,32 @@ def test_context_using_an_object
assert_equal(2, serializer.context.b)
end
end

class ContextAssociationTest < ActiveModel::TestCase
def setup
@association = UserSerializer._associations[:profile]
@old_association = @association.dup
@user = User.new({ name: 'Name 1', email: 'mail@server.com', gender: 'M' })
@user_serializer = UserSerializer.new(@user, context: { admin: true })
end

def teardown
UserSerializer._associations[:profile] = @old_association
end

def test_context_passed_through
@association.serializer_class = Class.new(ActiveModel::Serializer) do
def name
context[:admin] ? 'Admin' : 'User'
end

attributes :name
end

assert_equal({
name: 'Name 1', email: 'mail@server.com', profile: { name: 'Admin' }
}, @user_serializer.serializable_hash)
end
end
end
end
49 changes: 0 additions & 49 deletions test/unit/active_model/serializer/scope_test.rb

This file was deleted.

0 comments on commit 0d8ef2b

Please sign in to comment.