mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
Implement Serializer's scope
This commit is contained in:
parent
c3f857d1b6
commit
b6f9c5ee43
@ -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
|
||||
|
||||
|
||||
35
test/serializer/scope_test.rb
Normal file
35
test/serializer/scope_test.rb
Normal file
@ -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
|
||||
Loading…
Reference in New Issue
Block a user