From b6f9c5ee4331654fba4d8fb1254052e251eca7eb Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Tue, 2 Jul 2013 00:13:26 -0700 Subject: [PATCH] Implement Serializer's scope --- lib/active_model/serializer.rb | 3 ++- test/serializer/scope_test.rb | 35 ++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 test/serializer/scope_test.rb diff --git a/lib/active_model/serializer.rb b/lib/active_model/serializer.rb index 615c9704..d148860d 100644 --- a/lib/active_model/serializer.rb +++ b/lib/active_model/serializer.rb @@ -31,8 +31,9 @@ module ActiveModel @object = object @root = options[:root] || self.class._root @root = self.class.root_name if @root == true + @scope = options[:scope] end - attr_accessor :object, :root + attr_accessor :object, :root, :scope alias read_attribute_for_serialization send diff --git a/test/serializer/scope_test.rb b/test/serializer/scope_test.rb new file mode 100644 index 00000000..95e16ff8 --- /dev/null +++ b/test/serializer/scope_test.rb @@ -0,0 +1,35 @@ +require 'newbase/test_helper' +require 'newbase/active_model/serializer' + +module SerializerTest + module Scope + class Model + def initialize(hash={}) + @attributes = hash + end + + def read_attribute_for_serialization(name) + @attributes[name] + end + end + + class ModelSerializer < ActiveModel::Serializer + end + + class Test < ActiveModel::TestCase + def setup + @serializer = ModelSerializer.new(nil, scope: current_user) + end + + def test_scope + assert_equal('user', @serializer.scope) + end + + private + + def current_user + 'user' + end + end + end +end