diff --git a/Gemfile b/Gemfile index c19f7552..df30fcd0 100644 --- a/Gemfile +++ b/Gemfile @@ -4,3 +4,4 @@ source 'http://rubygems.org' gemspec gem "pry" +gem "simplecov", :require => false diff --git a/lib/action_controller/serialization.rb b/lib/action_controller/serialization.rb index ebcc9c20..583309fa 100644 --- a/lib/action_controller/serialization.rb +++ b/lib/action_controller/serialization.rb @@ -42,7 +42,8 @@ module ActionController 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 diff --git a/lib/active_model/serializer.rb b/lib/active_model/serializer.rb index 1ba94bec..17b53088 100644 --- a/lib/active_model/serializer.rb +++ b/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 @@ module ActiveModel # # 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 @@ module ActiveModel 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 @@ module ActiveModel 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 @@ module ActiveModel 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 diff --git a/test/association_test.rb b/test/association_test.rb index b8687b33..31b6175c 100644 --- a/test/association_test.rb +++ b/test/association_test.rb @@ -43,7 +43,7 @@ class AssociationTest < ActiveModel::TestCase 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 @@ class AssociationTest < ActiveModel::TestCase 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 @@ class AssociationTest < ActiveModel::TestCase 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 @@ class AssociationTest < ActiveModel::TestCase 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 @@ class AssociationTest < ActiveModel::TestCase def test_when_it_is_not_excluded post_serializer = @post_serializer_class.new( - @post, nil, :exclude => [] + @post, :exclude => [] ) json = post_serializer.as_json diff --git a/test/serialization_test.rb b/test/serialization_test.rb index c807fcc5..e930f476 100644 --- a/test/serialization_test.rb +++ b/test/serialization_test.rb @@ -15,12 +15,12 @@ class RenderJsonTest < ActionController::TestCase 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 diff --git a/test/serializer_test.rb b/test/serializer_test.rb index bc9a18bd..c5cf5b86 100644 --- a/test/serializer_test.rb +++ b/test/serializer_test.rb @@ -52,7 +52,7 @@ class SerializerTest < ActiveModel::TestCase 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 @@ class SerializerTest < ActiveModel::TestCase 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 @@ class SerializerTest < ActiveModel::TestCase 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 @@ class SerializerTest < ActiveModel::TestCase 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 @@ class SerializerTest < ActiveModel::TestCase 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 @@ class SerializerTest < ActiveModel::TestCase 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 @@ class SerializerTest < ActiveModel::TestCase 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 @@ class SerializerTest < ActiveModel::TestCase 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 @@ class SerializerTest < ActiveModel::TestCase 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 @@ class SerializerTest < ActiveModel::TestCase 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 @@ class SerializerTest < ActiveModel::TestCase 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 @@ class SerializerTest < ActiveModel::TestCase 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 @@ class SerializerTest < ActiveModel::TestCase 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 @@ class SerializerTest < ActiveModel::TestCase post.author = User.new(:id => 1) - serializer = serializer_class.new(post, nil) + serializer = serializer_class.new(post) assert_equal({ :post => { @@ -354,7 +354,7 @@ class SerializerTest < ActiveModel::TestCase 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 @@ class SerializerTest < ActiveModel::TestCase 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 @@ class SerializerTest < ActiveModel::TestCase 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 @@ class SerializerTest < ActiveModel::TestCase 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 @@ class SerializerTest < ActiveModel::TestCase attribute :password end - serializer = serializer_class.new(User.new, nil) + serializer = serializer_class.new(User.new) assert_equal({ :user => { @@ -575,7 +575,7 @@ class SerializerTest < ActiveModel::TestCase 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 @@ class SerializerTest < ActiveModel::TestCase 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,13 +647,13 @@ class SerializerTest < ActiveModel::TestCase :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 => { @@ -661,13 +661,13 @@ class SerializerTest < ActiveModel::TestCase :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 @@ class SerializerTest < ActiveModel::TestCase 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 @@ class SerializerTest < ActiveModel::TestCase 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] } diff --git a/test/test_helper.rb b/test/test_helper.rb index 689d8bfa..ceede0cc 100644 --- a/test/test_helper.rb +++ b/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"