remove scope as a separate concept and pass it in

as an option.
This commit is contained in:
Yehuda Katz 2012-01-11 21:16:02 -07:00
parent 4ad9c64e46
commit 671fc14888
7 changed files with 62 additions and 54 deletions

View File

@ -4,3 +4,4 @@ source 'http://rubygems.org'
gemspec gemspec
gem "pry" gem "pry"
gem "simplecov", :require => false

View File

@ -42,7 +42,8 @@ module ActionController
end end
if json.respond_to?(:active_model_serializer) && (serializer = json.active_model_serializer) if json.respond_to?(:active_model_serializer) && (serializer = json.active_model_serializer)
json = serializer.new(json, serialization_scope, options) options[:scope] = serialization_scope
json = serializer.new(json, options)
end end
super super
end end

View File

@ -5,18 +5,18 @@ module ActiveModel
# Active Model Array Serializer # Active Model Array Serializer
# #
# It serializes an array checking if each element that implements # It serializes an array checking if each element that implements
# the +active_model_serializer+ method passing down the current scope. # the +active_model_serializer+ method.
class ArraySerializer class ArraySerializer
attr_reader :object, :scope attr_reader :object
def initialize(object, scope, options={}) def initialize(object, options={})
@object, @scope, @options = object, scope, options @object, @options = object, options
end end
def serializable_array def serializable_array
@object.map do |item| @object.map do |item|
if item.respond_to?(:active_model_serializer) && (serializer = item.active_model_serializer) if item.respond_to?(:active_model_serializer) && (serializer = item.active_model_serializer)
serializer.new(item, scope, @options) serializer.new(item, @options)
else else
item item
end end
@ -40,12 +40,13 @@ module ActiveModel
# #
# Provides a basic serializer implementation that allows you to easily # Provides a basic serializer implementation that allows you to easily
# control how a given object is going to be serialized. On initialization, # control how a given object is going to be serialized. On initialization,
# it expects to object as arguments, a resource and a scope. For example, # it expects to object as arguments, a resource and options. For example,
# one may do in a controller: # one may do in a controller:
# #
# PostSerializer.new(@post, current_user).to_json # PostSerializer.new(@post, :scope => current_user).to_json
# #
# The object to be serialized is the +@post+ and the scope is +current_user+. # The object to be serialized is the +@post+ and the current user is passed
# in for authorization purposes.
# #
# We use the scope to check if a given attribute should be serialized or not. # We use the scope to check if a given attribute should be serialized or not.
# For example, some attributes maybe only be returned if +current_user+ is the # For example, some attributes maybe only be returned if +current_user+ is the
@ -127,10 +128,6 @@ module ActiveModel
option(:value) || source_serializer.send(name) option(:value) || source_serializer.send(name)
end end
def scope
option(:scope) || source_serializer.scope
end
def embed_ids? def embed_ids?
option(:embed, source_serializer._embed) == :ids option(:embed, source_serializer._embed) == :ids
end end
@ -147,9 +144,9 @@ module ActiveModel
def find_serializable(object) def find_serializable(object)
if target_serializer if target_serializer
target_serializer.new(object, scope, source_serializer.options) target_serializer.new(object, source_serializer.options)
elsif object.respond_to?(:active_model_serializer) && (ams = object.active_model_serializer) elsif object.respond_to?(:active_model_serializer) && (ams = object.active_model_serializer)
ams.new(object, scope, source_serializer.options) ams.new(object, source_serializer.options)
else else
object object
end end
@ -343,10 +340,10 @@ module ActiveModel
end end
end end
attr_reader :object, :scope, :options attr_reader :object, :options
def initialize(object, scope, options={}) def initialize(object, options={})
@object, @scope, @options = object, scope, options @object, @options = object, options
end end
# Returns a json representation of the serializable # Returns a json representation of the serializable

View File

@ -43,7 +43,7 @@ class AssociationTest < ActiveModel::TestCase
attributes :title, :body attributes :title, :body
end end
@post_serializer = @post_serializer_class.new(@post, nil) @post_serializer = @post_serializer_class.new(@post)
@hash = {} @hash = {}
@root_hash = {} @root_hash = {}
@ -321,7 +321,7 @@ class AssociationTest < ActiveModel::TestCase
def test_when_it_is_included def test_when_it_is_included
post_serializer = @post_serializer_class.new( post_serializer = @post_serializer_class.new(
@post, nil, :include => [:comments] @post, :include => [:comments]
) )
json = post_serializer.as_json json = post_serializer.as_json
@ -340,7 +340,7 @@ class AssociationTest < ActiveModel::TestCase
def test_when_it_is_not_included def test_when_it_is_not_included
post_serializer = @post_serializer_class.new( post_serializer = @post_serializer_class.new(
@post, nil, :include => [] @post, :include => []
) )
json = post_serializer.as_json json = post_serializer.as_json
@ -356,7 +356,7 @@ class AssociationTest < ActiveModel::TestCase
def test_when_it_is_excluded def test_when_it_is_excluded
post_serializer = @post_serializer_class.new( post_serializer = @post_serializer_class.new(
@post, nil, :exclude => [:comments] @post, :exclude => [:comments]
) )
json = post_serializer.as_json json = post_serializer.as_json
@ -372,7 +372,7 @@ class AssociationTest < ActiveModel::TestCase
def test_when_it_is_not_excluded def test_when_it_is_not_excluded
post_serializer = @post_serializer_class.new( post_serializer = @post_serializer_class.new(
@post, nil, :exclude => [] @post, :exclude => []
) )
json = post_serializer.as_json json = post_serializer.as_json

View File

@ -15,12 +15,12 @@ class RenderJsonTest < ActionController::TestCase
end end
class JsonSerializer class JsonSerializer
def initialize(object, scope, options={}) def initialize(object, options={})
@object, @scope, @options = object, scope, options @object, @options = object, options
end end
def as_json(*) def as_json(*)
hash = { :object => serializable_hash, :scope => @scope.as_json } hash = { :object => serializable_hash, :scope => @options[:scope].as_json }
hash.merge!(:options => true) if @options[:options] hash.merge!(:options => true) if @options[:options]
hash hash
end end

View File

@ -52,7 +52,7 @@ class SerializerTest < ActiveModel::TestCase
attributes :first_name, :last_name attributes :first_name, :last_name
def serializable_hash def serializable_hash
attributes.merge(:ok => true).merge(scope) attributes.merge(:ok => true).merge(options[:scope])
end end
end end
@ -107,7 +107,7 @@ class SerializerTest < ActiveModel::TestCase
def test_attributes_method def test_attributes_method
user = User.new user = User.new
user_serializer = UserSerializer.new(user, {}) user_serializer = UserSerializer.new(user, :scope => {})
hash = user_serializer.as_json hash = user_serializer.as_json
@ -118,7 +118,7 @@ class SerializerTest < ActiveModel::TestCase
def test_serializer_receives_scope def test_serializer_receives_scope
user = User.new user = User.new
user_serializer = UserSerializer.new(user, {:scope => true}) user_serializer = UserSerializer.new(user, :scope => {:scope => true})
hash = user_serializer.as_json hash = user_serializer.as_json
@ -135,7 +135,7 @@ class SerializerTest < ActiveModel::TestCase
def test_pretty_accessors def test_pretty_accessors
user = User.new user = User.new
user.superuser = true user.superuser = true
user_serializer = MyUserSerializer.new(user, nil) user_serializer = MyUserSerializer.new(user)
hash = user_serializer.as_json hash = user_serializer.as_json
@ -153,7 +153,7 @@ class SerializerTest < ActiveModel::TestCase
comments = [Comment.new(:title => "Comment1"), Comment.new(:title => "Comment2")] comments = [Comment.new(:title => "Comment1"), Comment.new(:title => "Comment2")]
post.comments = comments post.comments = comments
post_serializer = PostSerializer.new(post, user) post_serializer = PostSerializer.new(post, :scope => user)
assert_equal({ assert_equal({
:post => { :post => {
@ -184,7 +184,7 @@ class SerializerTest < ActiveModel::TestCase
blog = Blog.new blog = Blog.new
blog.author = user blog.author = user
json = BlogSerializer.new(blog, user).as_json json = BlogSerializer.new(blog, :scope => user).as_json
assert_equal({ assert_equal({
:blog => { :blog => {
:author => { :author => {
@ -212,7 +212,7 @@ class SerializerTest < ActiveModel::TestCase
blog = Blog.new blog = Blog.new
blog.author = user blog.author = user
json = blog_serializer.new(blog, user).as_json json = blog_serializer.new(blog, :scope => user).as_json
assert_equal({ assert_equal({
:person => { :person => {
:first_name => "Jose" :first_name => "Jose"
@ -232,7 +232,7 @@ class SerializerTest < ActiveModel::TestCase
user = User.new user = User.new
blog = Blog.new blog = Blog.new
json = BlogSerializer.new(blog, user).as_json json = BlogSerializer.new(blog, :scope => user).as_json
assert_equal({ assert_equal({
:blog => { :author => nil } :blog => { :author => nil }
}, json) }, json)
@ -241,7 +241,7 @@ class SerializerTest < ActiveModel::TestCase
root :blog root :blog
end end
json = serializer.new(blog, user).as_json json = serializer.new(blog, :scope => user).as_json
assert_equal({ :blog => { :author => nil } }, json) assert_equal({ :blog => { :author => nil } }, json)
end end
@ -253,7 +253,7 @@ class SerializerTest < ActiveModel::TestCase
root :my_blog root :my_blog
end end
assert_equal({ :my_blog => { :author => nil } }, serializer.new(blog, user).as_json) assert_equal({ :my_blog => { :author => nil } }, serializer.new(blog, :scope => user).as_json)
end end
def test_false_root def test_false_root
@ -264,11 +264,11 @@ class SerializerTest < ActiveModel::TestCase
root false root false
end end
assert_equal({ :author => nil }, serializer.new(blog, user).as_json) assert_equal({ :author => nil }, serializer.new(blog, :scope => user).as_json)
# test inherited false root # test inherited false root
serializer = Class.new(serializer) serializer = Class.new(serializer)
assert_equal({ :author => nil }, serializer.new(blog, user).as_json) assert_equal({ :author => nil }, serializer.new(blog, :scope => user).as_json)
end end
def test_embed_ids def test_embed_ids
@ -283,7 +283,7 @@ class SerializerTest < ActiveModel::TestCase
comments = [Comment.new(:title => "Comment1", :id => 1), Comment.new(:title => "Comment2", :id => 2)] comments = [Comment.new(:title => "Comment1", :id => 1), Comment.new(:title => "Comment2", :id => 2)]
post.comments = comments post.comments = comments
serializer = serializer.new(post, nil) serializer = serializer.new(post)
assert_equal({ assert_equal({
:post => { :post => {
@ -307,7 +307,7 @@ class SerializerTest < ActiveModel::TestCase
comments = [Comment.new(:title => "Comment1", :id => 1), Comment.new(:title => "Comment2", :id => 2)] comments = [Comment.new(:title => "Comment1", :id => 1), Comment.new(:title => "Comment2", :id => 2)]
post.comments = comments post.comments = comments
serializer = serializer_class.new(post, nil) serializer = serializer_class.new(post)
assert_equal({ assert_equal({
:post => { :post => {
@ -325,7 +325,7 @@ class SerializerTest < ActiveModel::TestCase
post.author = User.new(:id => 1) post.author = User.new(:id => 1)
serializer = serializer_class.new(post, nil) serializer = serializer_class.new(post)
assert_equal({ assert_equal({
:post => { :post => {
@ -354,7 +354,7 @@ class SerializerTest < ActiveModel::TestCase
comments = [Comment.new(:title => "Comment1", :id => 1), Comment.new(:title => "Comment2", :id => 2)] comments = [Comment.new(:title => "Comment1", :id => 1), Comment.new(:title => "Comment2", :id => 2)]
post.comments = comments post.comments = comments
serializer = serializer.new(post, nil) serializer = serializer.new(post)
assert_equal({ assert_equal({
:post => { :post => {
@ -389,7 +389,7 @@ class SerializerTest < ActiveModel::TestCase
array = [ comment1, comment2 ] array = [ comment1, comment2 ]
serializer = array.active_model_serializer.new(array, nil, :root => :comments) serializer = array.active_model_serializer.new(array, :root => :comments)
assert_equal({ :comments => [ assert_equal({ :comments => [
{ :title => "Comment1" }, { :title => "Comment1" },
@ -417,7 +417,7 @@ class SerializerTest < ActiveModel::TestCase
custom_blog.public_posts = posts custom_blog.public_posts = posts
custom_blog.public_user = user custom_blog.public_user = user
serializer = CustomBlogSerializer.new(custom_blog, :scope => true) serializer = CustomBlogSerializer.new(custom_blog, :scope => { :scope => true })
assert_equal({ assert_equal({
:custom_blog => { :custom_blog => {
@ -454,7 +454,7 @@ class SerializerTest < ActiveModel::TestCase
custom_blog.public_posts = posts custom_blog.public_posts = posts
custom_blog.public_user = user custom_blog.public_user = user
serializer = implicit_serializer.new(custom_blog, :scope => true) serializer = implicit_serializer.new(custom_blog, :scope => { :scope => true })
assert_equal({ assert_equal({
:custom_blog => { :custom_blog => {
@ -480,7 +480,7 @@ class SerializerTest < ActiveModel::TestCase
attribute :password attribute :password
end end
serializer = serializer_class.new(User.new, nil) serializer = serializer_class.new(User.new)
assert_equal({ assert_equal({
:user => { :user => {
@ -575,7 +575,7 @@ class SerializerTest < ActiveModel::TestCase
author = author_class.new(:id => 5) author = author_class.new(:id => 5)
post.author = author post.author = author
hash = serializer_class.new(post, nil) hash = serializer_class.new(post)
assert_equal({ assert_equal({
:post => { :post => {
@ -608,7 +608,7 @@ class SerializerTest < ActiveModel::TestCase
author = author_class.new(:id => 5, :name => "Tom Dale") author = author_class.new(:id => 5, :name => "Tom Dale")
post.author = author post.author = author
hash = serializer_class.new(post, nil) hash = serializer_class.new(post)
assert_equal({ assert_equal({
:post => { :post => {
@ -647,13 +647,13 @@ class SerializerTest < ActiveModel::TestCase
:body => "It's a new post!", :body => "It's a new post!",
:author => { :id => 5, :name => "Tom Dale" } :author => { :id => 5, :name => "Tom Dale" }
} }
}, serializer_class.new(post, nil, :root => :blog_post).as_json) }, serializer_class.new(post, :root => :blog_post).as_json)
assert_equal({ assert_equal({
:title => "New Post", :title => "New Post",
:body => "It's a new post!", :body => "It's a new post!",
:author => { :id => 5, :name => "Tom Dale" } :author => { :id => 5, :name => "Tom Dale" }
}, serializer_class.new(post, nil, :root => false).as_json) }, serializer_class.new(post, :root => false).as_json)
assert_equal({ assert_equal({
:blog_post => { :blog_post => {
@ -661,13 +661,13 @@ class SerializerTest < ActiveModel::TestCase
:body => "It's a new post!", :body => "It's a new post!",
:author => { :id => 5, :name => "Tom Dale" } :author => { :id => 5, :name => "Tom Dale" }
} }
}, serializer_class.new(post, nil).as_json(:root => :blog_post)) }, serializer_class.new(post).as_json(:root => :blog_post))
assert_equal({ assert_equal({
:title => "New Post", :title => "New Post",
:body => "It's a new post!", :body => "It's a new post!",
:author => { :id => 5, :name => "Tom Dale" } :author => { :id => 5, :name => "Tom Dale" }
}, serializer_class.new(post, nil).as_json(:root => false)) }, serializer_class.new(post).as_json(:root => false))
end end
def test_serializer_has_access_to_root_object def test_serializer_has_access_to_root_object
@ -699,7 +699,7 @@ class SerializerTest < ActiveModel::TestCase
author = author_class.new(:id => 5, :name => "Tom Dale") author = author_class.new(:id => 5, :name => "Tom Dale")
post.author = author post.author = author
expected = serializer_class.new(post, nil).as_json expected = serializer_class.new(post).as_json
assert_equal expected, hash_object assert_equal expected, hash_object
end end
@ -747,7 +747,7 @@ class SerializerTest < ActiveModel::TestCase
comment1.tags = [tag1, tag3] comment1.tags = [tag1, tag3]
comment2.tags = [tag1, tag2] comment2.tags = [tag1, tag2]
actual = ActiveModel::ArraySerializer.new([post], nil, :root => :posts).as_json actual = ActiveModel::ArraySerializer.new([post], :root => :posts).as_json
assert_equal({ assert_equal({
:posts => [ :posts => [
{ :title => "New Post", :body => "NEW POST", :id => 1, :comments => [1,2] } { :title => "New Post", :body => "NEW POST", :id => 1, :comments => [1,2] }

View File

@ -1,5 +1,14 @@
require "rubygems" require "rubygems"
require "bundler/setup" require "bundler/setup"
unless ENV["TRAVIS"]
require 'simplecov'
SimpleCov.start do
add_group "lib", "lib"
add_group "spec", "spec"
end
end
require "pry" require "pry"
require "active_model_serializers" require "active_model_serializers"