Add "scope" method

This commit is contained in:
twinturbo 2012-07-14 14:54:23 +02:00
parent 3e87c6414d
commit 7936e3efba
3 changed files with 13 additions and 3 deletions

View File

@ -126,12 +126,12 @@ class PostSerializer < ActiveModel::Serializer
# only let the user see comments he created.
def comments
post.comments.where(:created_by => options[:scope])
post.comments.where(:created_by => scope)
end
end
```
In a serializer, `options[:scope]` is the current authorization scope (usually
In a serializer, `scope` is the current authorization scope (usually
`current_user`), which the controller gives to the serializer when you call
`render :json`

View File

@ -103,7 +103,7 @@ module ActiveModel
# end
#
# def author?
# post.author == options[:scope]
# post.author == scope
# end
# end
#
@ -520,6 +520,11 @@ module ActiveModel
hash
end
# Returns options[:scope]
def scope
@options[:scope]
end
alias :read_attribute_for_serialization :send
# Use ActiveSupport::Notifications to send events to external systems.

View File

@ -94,6 +94,11 @@ class SerializerTest < ActiveModel::TestCase
has_many :comments, :serializer => CommentSerializer
end
def test_scope_works_correct
serializer = ActiveModel::Serializer.new :foo, :scope => :bar
asser_equal serializer.scope, :bar
end
def test_attributes
user = User.new
user_serializer = DefaultUserSerializer.new(user, {})