Browse files

remove scope as a separate concept and pass it in

as an option.
  • Loading branch information...
1 parent 4ad9c64 commit 671fc14888e9639554ecf295a5834e0c7235d2e4 @wycats wycats committed Jan 11, 2012
View
1 Gemfile
@@ -4,3 +4,4 @@ source 'http://rubygems.org'
gemspec
gem "pry"
+gem "simplecov", :require => false
View
3 lib/action_controller/serialization.rb
@@ -42,7 +42,8 @@ def _render_option_json(json, options)
end
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
super
end
View
31 lib/active_model/serializer.rb
@@ -5,18 +5,18 @@ module ActiveModel
# Active Model Array Serializer
#
# 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
- attr_reader :object, :scope
+ attr_reader :object
- def initialize(object, scope, options={})
- @object, @scope, @options = object, scope, options
+ def initialize(object, options={})
+ @object, @options = object, options
end
def serializable_array
@object.map do |item|
if item.respond_to?(:active_model_serializer) && (serializer = item.active_model_serializer)
- serializer.new(item, scope, @options)
+ serializer.new(item, @options)
else
item
end
@@ -40,12 +40,13 @@ def as_json(*args)
#
# Provides a basic serializer implementation that allows you to easily
# 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:
#
- # 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.
# For example, some attributes maybe only be returned if +current_user+ is the
@@ -127,10 +128,6 @@ def associated_object
option(:value) || source_serializer.send(name)
end
- def scope
- option(:scope) || source_serializer.scope
- end
-
def embed_ids?
option(:embed, source_serializer._embed) == :ids
end
@@ -147,9 +144,9 @@ def embed_in_root?
def find_serializable(object)
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)
- ams.new(object, scope, source_serializer.options)
+ ams.new(object, source_serializer.options)
else
object
end
@@ -343,10 +340,10 @@ def inherited(klass) #:nodoc:
end
end
- attr_reader :object, :scope, :options
+ attr_reader :object, :options
- def initialize(object, scope, options={})
- @object, @scope, @options = object, scope, options
+ def initialize(object, options={})
+ @object, @options = object, options
end
# Returns a json representation of the serializable
View
10 test/association_test.rb
@@ -43,7 +43,7 @@ def setup
attributes :title, :body
end
- @post_serializer = @post_serializer_class.new(@post, nil)
+ @post_serializer = @post_serializer_class.new(@post)
@hash = {}
@root_hash = {}
@@ -321,7 +321,7 @@ def setup
def test_when_it_is_included
post_serializer = @post_serializer_class.new(
- @post, nil, :include => [:comments]
+ @post, :include => [:comments]
)
json = post_serializer.as_json
@@ -340,7 +340,7 @@ def test_when_it_is_included
def test_when_it_is_not_included
post_serializer = @post_serializer_class.new(
- @post, nil, :include => []
+ @post, :include => []
)
json = post_serializer.as_json
@@ -356,7 +356,7 @@ def test_when_it_is_not_included
def test_when_it_is_excluded
post_serializer = @post_serializer_class.new(
- @post, nil, :exclude => [:comments]
+ @post, :exclude => [:comments]
)
json = post_serializer.as_json
@@ -372,7 +372,7 @@ def test_when_it_is_excluded
def test_when_it_is_not_excluded
post_serializer = @post_serializer_class.new(
- @post, nil, :exclude => []
+ @post, :exclude => []
)
json = post_serializer.as_json
View
6 test/serialization_test.rb
@@ -15,12 +15,12 @@ def to_json(options = {})
end
class JsonSerializer
- def initialize(object, scope, options={})
- @object, @scope, @options = object, scope, options
+ def initialize(object, options={})
+ @object, @options = object, options
end
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
end
View
56 test/serializer_test.rb
@@ -52,7 +52,7 @@ class UserSerializer < ActiveModel::Serializer
attributes :first_name, :last_name
def serializable_hash
- attributes.merge(:ok => true).merge(scope)
+ attributes.merge(:ok => true).merge(options[:scope])
end
end
@@ -107,7 +107,7 @@ def test_attributes
def test_attributes_method
user = User.new
- user_serializer = UserSerializer.new(user, {})
+ user_serializer = UserSerializer.new(user, :scope => {})
hash = user_serializer.as_json
@@ -118,7 +118,7 @@ def test_attributes_method
def test_serializer_receives_scope
user = User.new
- user_serializer = UserSerializer.new(user, {:scope => true})
+ user_serializer = UserSerializer.new(user, :scope => {:scope => true})
hash = user_serializer.as_json
@@ -135,7 +135,7 @@ def test_serializer_receives_scope
def test_pretty_accessors
user = User.new
user.superuser = true
- user_serializer = MyUserSerializer.new(user, nil)
+ user_serializer = MyUserSerializer.new(user)
hash = user_serializer.as_json
@@ -153,7 +153,7 @@ def test_has_many
comments = [Comment.new(:title => "Comment1"), Comment.new(:title => "Comment2")]
post.comments = comments
- post_serializer = PostSerializer.new(post, user)
+ post_serializer = PostSerializer.new(post, :scope => user)
assert_equal({
:post => {
@@ -184,7 +184,7 @@ def test_has_one
blog = Blog.new
blog.author = user
- json = BlogSerializer.new(blog, user).as_json
+ json = BlogSerializer.new(blog, :scope => user).as_json
assert_equal({
:blog => {
:author => {
@@ -212,7 +212,7 @@ def person
blog = Blog.new
blog.author = user
- json = blog_serializer.new(blog, user).as_json
+ json = blog_serializer.new(blog, :scope => user).as_json
assert_equal({
:person => {
:first_name => "Jose"
@@ -232,7 +232,7 @@ def test_associations_with_nil_association
user = User.new
blog = Blog.new
- json = BlogSerializer.new(blog, user).as_json
+ json = BlogSerializer.new(blog, :scope => user).as_json
assert_equal({
:blog => { :author => nil }
}, json)
@@ -241,7 +241,7 @@ def test_associations_with_nil_association
root :blog
end
- json = serializer.new(blog, user).as_json
+ json = serializer.new(blog, :scope => user).as_json
assert_equal({ :blog => { :author => nil } }, json)
end
@@ -253,7 +253,7 @@ def test_custom_root
root :my_blog
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
def test_false_root
@@ -264,11 +264,11 @@ def test_false_root
root false
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
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
def test_embed_ids
@@ -283,7 +283,7 @@ def test_embed_ids
comments = [Comment.new(:title => "Comment1", :id => 1), Comment.new(:title => "Comment2", :id => 2)]
post.comments = comments
- serializer = serializer.new(post, nil)
+ serializer = serializer.new(post)
assert_equal({
:post => {
@@ -307,7 +307,7 @@ def test_embed_ids_include_true
comments = [Comment.new(:title => "Comment1", :id => 1), Comment.new(:title => "Comment2", :id => 2)]
post.comments = comments
- serializer = serializer_class.new(post, nil)
+ serializer = serializer_class.new(post)
assert_equal({
:post => {
@@ -325,7 +325,7 @@ def test_embed_ids_include_true
post.author = User.new(:id => 1)
- serializer = serializer_class.new(post, nil)
+ serializer = serializer_class.new(post)
assert_equal({
:post => {
@@ -354,7 +354,7 @@ def test_embed_objects
comments = [Comment.new(:title => "Comment1", :id => 1), Comment.new(:title => "Comment2", :id => 2)]
post.comments = comments
- serializer = serializer.new(post, nil)
+ serializer = serializer.new(post)
assert_equal({
:post => {
@@ -389,7 +389,7 @@ def test_array_serializer
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 => [
{ :title => "Comment1" },
@@ -417,7 +417,7 @@ def test_associations_with_as
custom_blog.public_posts = posts
custom_blog.public_user = user
- serializer = CustomBlogSerializer.new(custom_blog, :scope => true)
+ serializer = CustomBlogSerializer.new(custom_blog, :scope => { :scope => true })
assert_equal({
:custom_blog => {
@@ -454,7 +454,7 @@ def test_implicity_detection_for_association_serializers
custom_blog.public_posts = posts
custom_blog.public_user = user
- serializer = implicit_serializer.new(custom_blog, :scope => true)
+ serializer = implicit_serializer.new(custom_blog, :scope => { :scope => true })
assert_equal({
:custom_blog => {
@@ -480,7 +480,7 @@ def test_attribute_key
attribute :password
end
- serializer = serializer_class.new(User.new, nil)
+ serializer = serializer_class.new(User.new)
assert_equal({
:user => {
@@ -575,7 +575,7 @@ def test_embed_id_for_has_one
author = author_class.new(:id => 5)
post.author = author
- hash = serializer_class.new(post, nil)
+ hash = serializer_class.new(post)
assert_equal({
:post => {
@@ -608,7 +608,7 @@ def test_embed_objects_for_has_one
author = author_class.new(:id => 5, :name => "Tom Dale")
post.author = author
- hash = serializer_class.new(post, nil)
+ hash = serializer_class.new(post)
assert_equal({
:post => {
@@ -647,27 +647,27 @@ def test_root_provided_in_options
:body => "It's a new post!",
: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({
:title => "New Post",
:body => "It's a new post!",
: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({
:blog_post => {
:title => "New Post",
:body => "It's a new post!",
: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({
:title => "New Post",
:body => "It's a new post!",
:author => { :id => 5, :name => "Tom Dale" }
- }, serializer_class.new(post, nil).as_json(:root => false))
+ }, serializer_class.new(post).as_json(:root => false))
end
def test_serializer_has_access_to_root_object
@@ -699,7 +699,7 @@ def test_serializer_has_access_to_root_object
author = author_class.new(:id => 5, :name => "Tom Dale")
post.author = author
- expected = serializer_class.new(post, nil).as_json
+ expected = serializer_class.new(post).as_json
assert_equal expected, hash_object
end
@@ -747,7 +747,7 @@ def test_embed_with_include_inserts_at_root
comment1.tags = [tag1, tag3]
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({
:posts => [
{ :title => "New Post", :body => "NEW POST", :id => 1, :comments => [1,2] }
View
9 test/test_helper.rb
@@ -1,5 +1,14 @@
require "rubygems"
require "bundler/setup"
+
+unless ENV["TRAVIS"]
+ require 'simplecov'
+ SimpleCov.start do
+ add_group "lib", "lib"
+ add_group "spec", "spec"
+ end
+end
+
require "pry"
require "active_model_serializers"

0 comments on commit 671fc14

Please sign in to comment.