From 5d0add3f2d5d7be23c6518534bb5a0a0275e7e52 Mon Sep 17 00:00:00 2001 From: Mikael Henriksson Date: Sat, 26 Oct 2013 22:41:45 +0200 Subject: [PATCH 01/13] Fix #425 global root configuration not inherited --- lib/active_model/serializer.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/active_model/serializer.rb b/lib/active_model/serializer.rb index b37ef7cdf..1e85d089b 100644 --- a/lib/active_model/serializer.rb +++ b/lib/active_model/serializer.rb @@ -8,7 +8,7 @@ module ActiveModel class Serializer include Serializable - + class_attribute :_root @mutex = Mutex.new class << self @@ -59,7 +59,7 @@ def serializer_for(resource) end end - attr_accessor :_root, :_attributes, :_associations + attr_accessor :_attributes, :_associations alias root _root= alias root= _root= @@ -112,7 +112,7 @@ def initialize(object, options={}) def root=(root) @root = root - @root = self.class._root if @root.nil? + @root = _root if @root.nil? @root = self.class.root_name if @root == true || @root.nil? end From 45fa2253e2cad362775a10df98d3b3bf742c6ccd Mon Sep 17 00:00:00 2001 From: Mike Krisher Date: Thu, 31 Oct 2013 20:06:32 -0500 Subject: [PATCH 02/13] Adds method and helpers to convert keys during serialization This commit adds a convert_hash method during the serialization process. It can be overwritten to support converting keys to any format. Two helper methods are included, one for converting the serialized hash keys to camelcase and another to convert them to upcase. --- lib/active_model/serializer.rb | 22 ++++++++++- test/fixtures/poro.rb | 7 ++++ .../serializer/attributes_test.rb | 38 +++++++++++++++++++ 3 files changed, 65 insertions(+), 2 deletions(-) diff --git a/lib/active_model/serializer.rb b/lib/active_model/serializer.rb index b3d55c1fd..c5d663481 100644 --- a/lib/active_model/serializer.rb +++ b/lib/active_model/serializer.rb @@ -107,7 +107,7 @@ def initialize(object, options={}) @meta_key = options[:meta_key] || :meta @meta = options[@meta_key] end - attr_accessor :object, :scope, :meta_key, :meta, :root + attr_accessor :object, :scope, :meta_key, :meta, :root, :convert_type def json_key if root == true || root.nil? @@ -180,8 +180,26 @@ def serialize_ids(association) def serializable_hash(options={}) return nil if object.nil? hash = attributes - hash.merge! associations + convert_keys hash.merge!(associations) end alias_method :serializable_object, :serializable_hash + + def convert_keys(hash) + hash.inject({}) { |h, (k, v)| h[apply_conversion(k)] = v; h } + end + + def apply_conversion(key) + return key.to_s.camelize(:lower).to_sym if convert_type == 'camelcase' + return key.to_s.upcase.to_sym if convert_type == 'upcase' + key + end + + def camelize_keys! + @convert_type = "camelcase" + end + + def upcase_keys! + @convert_type = "upcase" + end end end diff --git a/test/fixtures/poro.rb b/test/fixtures/poro.rb index a43954b53..982b43530 100644 --- a/test/fixtures/poro.rb +++ b/test/fixtures/poro.rb @@ -25,6 +25,9 @@ def profile class Profile < Model end +class Commenter < Model +end + class Post < Model def comments @comments ||= [Comment.new(content: 'C1'), @@ -53,6 +56,10 @@ def description attributes :name, :description end +class CommenterSerializer < ActiveModel::Serializer + attributes :first_name, :last_name, :company +end + class PostSerializer < ActiveModel::Serializer attributes :title, :body diff --git a/test/unit/active_model/serializer/attributes_test.rb b/test/unit/active_model/serializer/attributes_test.rb index 0914747e8..3a460d4c7 100644 --- a/test/unit/active_model/serializer/attributes_test.rb +++ b/test/unit/active_model/serializer/attributes_test.rb @@ -25,5 +25,43 @@ def test_attributes_serialization_using_as_json }, @profile_serializer.as_json) end end + + class HashKeyTest < ActiveModel::TestCase + def setup + @commenter = Commenter.new({ first_name: 'Steve', last_name: 'Jobs', company: 'Apple' }) + @commenter_serializer = CommenterSerializer.new(@commenter) + end + + def test_attributes_serialization_using_camelcase_key_conversion + @commenter_serializer.convert_type = 'camelcase' + assert_equal({ + firstName: 'Steve', lastName: 'Jobs', company: 'Apple' + }, @commenter_serializer.serializable_hash) + end + + def test_attributes_serialization_using_upcase_key_conversion + @commenter_serializer.convert_type = 'upcase' + assert_equal({ + FIRST_NAME: 'Steve', LAST_NAME: 'Jobs', COMPANY: 'Apple' + }, @commenter_serializer.serializable_hash) + end + end + + class HelpersTest < ActiveModel::TestCase + def setup + @commenter = Commenter.new({ first_name: 'Steve', last_name: 'Wozniak', company: 'Apple' }) + @commenter_serializer = CommenterSerializer.new(@commenter) + end + + def test_attributes_serialization_using_camelize_keys_helper + @commenter_serializer.camelize_keys! + assert_equal("camelcase", @commenter_serializer.convert_type) + end + + def test_attributes_serialization_using_upcase_keys_helper + @commenter_serializer.upcase_keys! + assert_equal("upcase", @commenter_serializer.convert_type) + end + end end end From 80909bc799e99688c3d67f9ea3bdd375c20caad3 Mon Sep 17 00:00:00 2001 From: Mike Krisher Date: Thu, 14 Nov 2013 20:04:32 -0600 Subject: [PATCH 03/13] Removes commenter poro, updates post poro to include snakecase attrs This commit removes the commenter poro and updates the post poro to include snakecase attrs (created_at and updated_at) to test converting keys to various formats such as camelizing. --- test/fixtures/poro.rb | 9 +------ .../active_model/serializer/filter_test.rb | 2 +- .../active_model/serializer/has_many_test.rb | 24 +++++++++---------- 3 files changed, 14 insertions(+), 21 deletions(-) diff --git a/test/fixtures/poro.rb b/test/fixtures/poro.rb index 982b43530..fb550e2c0 100644 --- a/test/fixtures/poro.rb +++ b/test/fixtures/poro.rb @@ -25,9 +25,6 @@ def profile class Profile < Model end -class Commenter < Model -end - class Post < Model def comments @comments ||= [Comment.new(content: 'C1'), @@ -56,12 +53,8 @@ def description attributes :name, :description end -class CommenterSerializer < ActiveModel::Serializer - attributes :first_name, :last_name, :company -end - class PostSerializer < ActiveModel::Serializer - attributes :title, :body + attributes :title, :body, :created_at, :updated_at has_many :comments end diff --git a/test/unit/active_model/serializer/filter_test.rb b/test/unit/active_model/serializer/filter_test.rb index bf33b5e74..2df5829fd 100644 --- a/test/unit/active_model/serializer/filter_test.rb +++ b/test/unit/active_model/serializer/filter_test.rb @@ -41,7 +41,7 @@ def teardown def test_filtered_associations_serialization assert_equal({ - 'post' => { title: 'Title 1' } + 'post' => { title: 'Title 1', created_at: nil, updated_at: nil } }, @post_serializer.as_json) end end diff --git a/test/unit/active_model/serializer/has_many_test.rb b/test/unit/active_model/serializer/has_many_test.rb index fc191dcb3..0a684ec97 100644 --- a/test/unit/active_model/serializer/has_many_test.rb +++ b/test/unit/active_model/serializer/has_many_test.rb @@ -25,7 +25,7 @@ def test_associations_embedding_ids_serialization_using_serializable_hash @association.embed = :ids assert_equal({ - title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id } + title: 'Title 1', body: 'Body 1', created_at: nil, updated_at: nil, 'comment_ids' => @post.comments.map { |c| c.object_id } }, @post_serializer.serializable_hash) end @@ -33,7 +33,7 @@ def test_associations_embedding_ids_serialization_using_as_json @association.embed = :ids assert_equal({ - 'post' => { title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id } } + 'post' => { title: 'Title 1', body: 'Body 1', created_at: nil, updated_at: nil, 'comment_ids' => @post.comments.map { |c| c.object_id } } }, @post_serializer.as_json) end @@ -42,7 +42,7 @@ def test_associations_embedding_ids_serialization_using_serializable_hash_and_ke @association.key = 'key' assert_equal({ - title: 'Title 1', body: 'Body 1', 'key' => @post.comments.map { |c| c.object_id } + title: 'Title 1', body: 'Body 1', created_at: nil, updated_at: nil, 'key' => @post.comments.map { |c| c.object_id } }, @post_serializer.serializable_hash) end @@ -50,7 +50,7 @@ def test_associations_embedding_objects_serialization_using_serializable_hash @association.embed = :objects assert_equal({ - title: 'Title 1', body: 'Body 1', comments: [{ content: 'C1' }, { content: 'C2' }] + title: 'Title 1', body: 'Body 1', created_at: nil, updated_at: nil, comments: [{ content: 'C1' }, { content: 'C2' }] }, @post_serializer.serializable_hash) end @@ -58,7 +58,7 @@ def test_associations_embedding_objects_serialization_using_as_json @association.embed = :objects assert_equal({ - 'post' => { title: 'Title 1', body: 'Body 1', comments: [{ content: 'C1' }, { content: 'C2' }] } + 'post' => { title: 'Title 1', body: 'Body 1', created_at: nil, updated_at: nil, comments: [{ content: 'C1' }, { content: 'C2' }] } }, @post_serializer.as_json) end @@ -71,7 +71,7 @@ def comments end assert_equal({ - 'post' => { title: 'Title 1', body: 'Body 1', comments: [nil] } + 'post' => { title: 'Title 1', body: 'Body 1', created_at: nil, updated_at: nil, comments: [nil] } }, @post_serializer.as_json) end @@ -80,7 +80,7 @@ def test_associations_embedding_objects_serialization_using_serializable_hash_an @association.embedded_key = 'root' assert_equal({ - title: 'Title 1', body: 'Body 1', 'root' => [{ content: 'C1' }, { content: 'C2' }] + title: 'Title 1', body: 'Body 1', created_at: nil, updated_at: nil, 'root' => [{ content: 'C1' }, { content: 'C2' }] }, @post_serializer.serializable_hash) end @@ -89,7 +89,7 @@ def test_associations_embedding_ids_including_objects_serialization_using_serial @association.embed_in_root = true assert_equal({ - title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id } + title: 'Title 1', body: 'Body 1', created_at: nil, updated_at: nil, 'comment_ids' => @post.comments.map { |c| c.object_id } }, @post_serializer.serializable_hash) end @@ -98,7 +98,7 @@ def test_associations_embedding_ids_including_objects_serialization_using_as_jso @association.embed_in_root = true assert_equal({ - 'post' => { title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id } }, + 'post' => { title: 'Title 1', body: 'Body 1', created_at: nil, updated_at: nil, 'comment_ids' => @post.comments.map { |c| c.object_id } }, comments: [{ content: 'C1' }, { content: 'C2' }] }, @post_serializer.as_json) end @@ -108,7 +108,7 @@ def test_associations_embedding_nothing_including_objects_serialization_using_as @association.embed_in_root = true assert_equal({ - 'post' => { title: 'Title 1', body: 'Body 1' }, + 'post' => { title: 'Title 1', body: 'Body 1', created_at: nil, updated_at: nil }, comments: [{ content: 'C1' }, { content: 'C2' }] }, @post_serializer.as_json) end @@ -125,7 +125,7 @@ def content end assert_equal({ - 'post' => { title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id } }, + 'post' => { title: 'Title 1', body: 'Body 1', created_at: nil, updated_at: nil, 'comment_ids' => @post.comments.map { |c| c.object_id } }, comments: [{ content: 'fake' }, { content: 'fake' }] }, @post_serializer.as_json) end @@ -140,7 +140,7 @@ def serializable_object end assert_equal({ - 'post' => { title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id } }, + 'post' => { title: 'Title 1', body: 'Body 1', created_at: nil, updated_at: nil, 'comment_ids' => @post.comments.map { |c| c.object_id } }, comments: { my_content: ['fake'] } }, @post_serializer.as_json) end From 0f98065384537750c7acde0d73e33398f102f3b2 Mon Sep 17 00:00:00 2001 From: Mike Krisher Date: Thu, 14 Nov 2013 20:06:20 -0600 Subject: [PATCH 04/13] Updates tests to use Post poro vs. Commenter This commit updates the tests to use the Post poro rather than introducing a new Commenter poro. --- .../serializer/attributes_test.rb | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/test/unit/active_model/serializer/attributes_test.rb b/test/unit/active_model/serializer/attributes_test.rb index 3a460d4c7..2a8b8d06d 100644 --- a/test/unit/active_model/serializer/attributes_test.rb +++ b/test/unit/active_model/serializer/attributes_test.rb @@ -28,39 +28,39 @@ def test_attributes_serialization_using_as_json class HashKeyTest < ActiveModel::TestCase def setup - @commenter = Commenter.new({ first_name: 'Steve', last_name: 'Jobs', company: 'Apple' }) - @commenter_serializer = CommenterSerializer.new(@commenter) + @post = Post.new({ title: 'test', body: 'lorem ipsum', created_at: Time.now, updated_at: Time.now }) + @post_serializer = PostSerializer.new(@post) end def test_attributes_serialization_using_camelcase_key_conversion - @commenter_serializer.convert_type = 'camelcase' - assert_equal({ - firstName: 'Steve', lastName: 'Jobs', company: 'Apple' - }, @commenter_serializer.serializable_hash) + @post_serializer.convert_type = 'camelcase' + assert_match({ + title: 'test', body: 'lorem ipsum', createdAt: Time.now, updatedAt: Time.now, :comments=>[{:content=>"C1"}, {:content=>"C2"}] + }.to_s, @post_serializer.serializable_hash.to_s) end def test_attributes_serialization_using_upcase_key_conversion - @commenter_serializer.convert_type = 'upcase' - assert_equal({ - FIRST_NAME: 'Steve', LAST_NAME: 'Jobs', COMPANY: 'Apple' - }, @commenter_serializer.serializable_hash) + @post_serializer.convert_type = 'upcase' + assert_match({ + TITLE: 'test', BODY: 'lorem ipsum', CREATED_AT: Time.now, UPDATED_AT: Time.now, :COMMENTS=>[{:content=>"C1"}, {:content=>"C2"}] + }.to_s, @post_serializer.serializable_hash.to_s) end end class HelpersTest < ActiveModel::TestCase def setup - @commenter = Commenter.new({ first_name: 'Steve', last_name: 'Wozniak', company: 'Apple' }) - @commenter_serializer = CommenterSerializer.new(@commenter) + @post = Post.new({ title: 'test', body: 'lorem ipsum', created_at: Time.now, updated_at: Time.now }) + @post_serializer = PostSerializer.new(@post) end def test_attributes_serialization_using_camelize_keys_helper - @commenter_serializer.camelize_keys! - assert_equal("camelcase", @commenter_serializer.convert_type) + @post_serializer.camelize_keys! + assert_equal("camelcase", @post_serializer.convert_type) end def test_attributes_serialization_using_upcase_keys_helper - @commenter_serializer.upcase_keys! - assert_equal("upcase", @commenter_serializer.convert_type) + @post_serializer.upcase_keys! + assert_equal("upcase", @post_serializer.convert_type) end end end From 1c5091f27072d8f0cf24a4efa4af2711f8222e64 Mon Sep 17 00:00:00 2001 From: Mike Krisher Date: Thu, 14 Nov 2013 20:37:20 -0600 Subject: [PATCH 05/13] Updates serialize to pass convert_type to association serializer This commit updates the test to expect the same key conversion for associations as the parent. This required passing the convert type from the parent to the associations via the serializer method. If it is included in the options, it is set during initialize. --- lib/active_model/serializer.rb | 13 +++++++------ lib/active_model/serializer/associations.rb | 4 ++-- .../unit/active_model/serializer/attributes_test.rb | 2 +- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/lib/active_model/serializer.rb b/lib/active_model/serializer.rb index c5d663481..362bf0c96 100644 --- a/lib/active_model/serializer.rb +++ b/lib/active_model/serializer.rb @@ -101,11 +101,12 @@ def associate(klass, *attrs) end def initialize(object, options={}) - @object = object - @scope = options[:scope] - @root = options.fetch(:root, self.class._root) - @meta_key = options[:meta_key] || :meta - @meta = options[@meta_key] + @object = object + @scope = options[:scope] + @root = options.fetch(:root, self.class._root) + @meta_key = options[:meta_key] || :meta + @meta = options[@meta_key] + @convert_type = options[:convert_type] end attr_accessor :object, :scope, :meta_key, :meta, :root, :convert_type @@ -162,7 +163,7 @@ def serialize(association) if associated_data.respond_to?(:to_ary) && !(association.serializer_class && association.serializer_class <= ArraySerializer) - associated_data.map { |elem| association.build_serializer(elem).serializable_hash } + associated_data.map { |elem| association.build_serializer(elem, @convert_type).serializable_hash } else association.build_serializer(associated_data).serializable_object end diff --git a/lib/active_model/serializer/associations.rb b/lib/active_model/serializer/associations.rb index 2a5787c9f..0771d1a76 100644 --- a/lib/active_model/serializer/associations.rb +++ b/lib/active_model/serializer/associations.rb @@ -37,9 +37,9 @@ def embed=(embed) @embed_objects = embed == :object || embed == :objects end - def build_serializer(object) + def build_serializer(object, convert_type = nil) @serializer_class ||= Serializer.serializer_for(object) || DefaultSerializer - @serializer_class.new(object, @options) + @serializer_class.new(object, @options.merge(:convert_type => convert_type)) end class HasOne < Association diff --git a/test/unit/active_model/serializer/attributes_test.rb b/test/unit/active_model/serializer/attributes_test.rb index 2a8b8d06d..b3d47be22 100644 --- a/test/unit/active_model/serializer/attributes_test.rb +++ b/test/unit/active_model/serializer/attributes_test.rb @@ -42,7 +42,7 @@ def test_attributes_serialization_using_camelcase_key_conversion def test_attributes_serialization_using_upcase_key_conversion @post_serializer.convert_type = 'upcase' assert_match({ - TITLE: 'test', BODY: 'lorem ipsum', CREATED_AT: Time.now, UPDATED_AT: Time.now, :COMMENTS=>[{:content=>"C1"}, {:content=>"C2"}] + TITLE: 'test', BODY: 'lorem ipsum', CREATED_AT: Time.now, UPDATED_AT: Time.now, :COMMENTS=>[{:CONTENT=>"C1"}, {:CONTENT=>"C2"}] }.to_s, @post_serializer.serializable_hash.to_s) end end From 288be40c93c36fe97f18503ae2612e3f3fb887e7 Mon Sep 17 00:00:00 2001 From: Mike Krisher Date: Thu, 14 Nov 2013 20:46:26 -0600 Subject: [PATCH 06/13] Removes unnecessary to_sym This commit removes the to_sym when converting keys. Tests were updated. --- lib/active_model/serializer.rb | 4 ++-- test/unit/active_model/serializer/attributes_test.rb | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/active_model/serializer.rb b/lib/active_model/serializer.rb index 362bf0c96..983bff9df 100644 --- a/lib/active_model/serializer.rb +++ b/lib/active_model/serializer.rb @@ -190,8 +190,8 @@ def convert_keys(hash) end def apply_conversion(key) - return key.to_s.camelize(:lower).to_sym if convert_type == 'camelcase' - return key.to_s.upcase.to_sym if convert_type == 'upcase' + return key.to_s.camelize(:lower) if convert_type == 'camelcase' + return key.to_s.upcase if convert_type == 'upcase' key end diff --git a/test/unit/active_model/serializer/attributes_test.rb b/test/unit/active_model/serializer/attributes_test.rb index b3d47be22..52f7b0299 100644 --- a/test/unit/active_model/serializer/attributes_test.rb +++ b/test/unit/active_model/serializer/attributes_test.rb @@ -35,14 +35,14 @@ def setup def test_attributes_serialization_using_camelcase_key_conversion @post_serializer.convert_type = 'camelcase' assert_match({ - title: 'test', body: 'lorem ipsum', createdAt: Time.now, updatedAt: Time.now, :comments=>[{:content=>"C1"}, {:content=>"C2"}] + 'title' => 'test', 'body' => 'lorem ipsum', 'createdAt' => Time.now, 'updatedAt' => Time.now, 'comments' => [{ 'content' => 'C1'}, { 'content' => 'C2'}] }.to_s, @post_serializer.serializable_hash.to_s) end def test_attributes_serialization_using_upcase_key_conversion @post_serializer.convert_type = 'upcase' assert_match({ - TITLE: 'test', BODY: 'lorem ipsum', CREATED_AT: Time.now, UPDATED_AT: Time.now, :COMMENTS=>[{:CONTENT=>"C1"}, {:CONTENT=>"C2"}] + 'TITLE' => 'test', 'BODY' => 'lorem ipsum', 'CREATED_AT' => Time.now, 'UPDATED_AT' => Time.now, 'COMMENTS' => [{'CONTENT' => 'C1'}, { 'CONTENT' => 'C2'}] }.to_s, @post_serializer.serializable_hash.to_s) end end From cc5ae4a6011d4e42f83e156c5d5d986a2ef5acbb Mon Sep 17 00:00:00 2001 From: Mike Krisher Date: Thu, 14 Nov 2013 22:24:25 -0600 Subject: [PATCH 07/13] Converts root key case same as model case This commit applies the conversion to the root key the same as the model convert type. --- lib/active_model/serializable.rb | 4 +++- test/unit/active_model/serializer/root_test.rb | 13 +++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/active_model/serializable.rb b/lib/active_model/serializable.rb index c5adb6845..93a33e26f 100644 --- a/lib/active_model/serializable.rb +++ b/lib/active_model/serializable.rb @@ -1,7 +1,9 @@ module ActiveModel module Serializable def as_json(options={}) - if root = options.fetch(:root, json_key) + root = options.fetch(:root, json_key) + root = apply_conversion(root) if @convert_type + if root hash = { root => serializable_object } hash.merge!(serializable_data) hash diff --git a/test/unit/active_model/serializer/root_test.rb b/test/unit/active_model/serializer/root_test.rb index 6cf2c546f..6b45cf6af 100644 --- a/test/unit/active_model/serializer/root_test.rb +++ b/test/unit/active_model/serializer/root_test.rb @@ -6,7 +6,7 @@ class RootAsOptionTest < ActiveModel::TestCase def setup @old_root = ProfileSerializer._root @profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }) - @serializer = ProfileSerializer.new(@profile, root: :initialize) + @serializer = ProfileSerializer.new(@profile, root: :new_posts) ProfileSerializer._root = true end @@ -22,12 +22,21 @@ def test_root_is_not_displayed_using_serializable_hash def test_root_using_as_json assert_equal({ - initialize: { + new_posts: { name: 'Name 1', description: 'Description 1' } }, @serializer.as_json) end + def test_root_applied_conversion_using_as_json + @serializer.camelize_keys! + assert_equal({ + 'newPosts' => { + 'name' => 'Name 1', 'description' => 'Description 1' + } + }, @serializer.as_json) + end + def test_root_from_serializer_name @serializer = ProfileSerializer.new(@profile) From 39b2a2bdf4b6253508aa27d71531a92b2f1b58d1 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Thu, 28 Nov 2013 15:47:30 -0300 Subject: [PATCH 08/13] Update Rubinius to 2.2.1 --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index fd881a3a9..6f2bde4e1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,7 +3,7 @@ rvm: - 1.9.3 - 2.0.0 - jruby-19mode - - rbx-19mode + - rbx-2.2.1 gemfile: - Gemfile - Gemfile.rails3 From ddaf09a099e7b9bf8e815a4bbad6100d40026760 Mon Sep 17 00:00:00 2001 From: Arthur Neves Date: Tue, 3 Dec 2013 22:53:37 -0500 Subject: [PATCH 09/13] Alow rbx to fail --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 6f2bde4e1..425edee59 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,6 +10,7 @@ gemfile: - Gemfile.edge matrix: allow_failures: + - rvm: rbx-2.2.1 - gemfile: Gemfile.edge notifications: email: false From 3a9475a16879d1811a67c71116f74ab0fdf5cf42 Mon Sep 17 00:00:00 2001 From: Arthur Neves Date: Tue, 3 Dec 2013 22:55:29 -0500 Subject: [PATCH 10/13] Ruby-head and fast_finish --- .travis.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.travis.yml b/.travis.yml index 425edee59..a34cf8095 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,6 +2,7 @@ language: ruby rvm: - 1.9.3 - 2.0.0 + - ruby-head - jruby-19mode - rbx-2.2.1 gemfile: @@ -11,7 +12,9 @@ gemfile: matrix: allow_failures: - rvm: rbx-2.2.1 + - rvm: ruby-head - gemfile: Gemfile.edge + fast_finish: true notifications: email: false campfire: From ac0ce8dcf94341ff2e34046759b22aa52ee8a469 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Fri, 6 Dec 2013 15:26:38 -0200 Subject: [PATCH 11/13] Use jruby and rbx in Travis --- .travis.yml | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index a34cf8095..1c6de7a71 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,16 +3,17 @@ rvm: - 1.9.3 - 2.0.0 - ruby-head - - jruby-19mode - - rbx-2.2.1 + - jruby + - rbx gemfile: - Gemfile - Gemfile.rails3 - Gemfile.edge matrix: allow_failures: - - rvm: rbx-2.2.1 - rvm: ruby-head + - rvm: jruby + - rvm: rbx - gemfile: Gemfile.edge fast_finish: true notifications: From 2faa571a9cfac95f992449fe0417a1d7774b84fc Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Fri, 6 Dec 2013 15:33:08 -0200 Subject: [PATCH 12/13] Do not allo ruby-head failures --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 1c6de7a71..0a0b1a99f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,7 +11,6 @@ gemfile: - Gemfile.edge matrix: allow_failures: - - rvm: ruby-head - rvm: jruby - rvm: rbx - gemfile: Gemfile.edge From 2877f40d818338febff9faa5d3cb5adee3a3010d Mon Sep 17 00:00:00 2001 From: Mikael Henriksson Date: Fri, 13 Dec 2013 17:16:01 +0100 Subject: [PATCH 13/13] Fix various serialization problems --- lib/active_model/serializer.rb | 2 +- lib/active_model/serializer/associations.rb | 5 +++-- test/unit/active_model/serializer/has_many_test.rb | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/active_model/serializer.rb b/lib/active_model/serializer.rb index 3b9733656..9e5009ff4 100644 --- a/lib/active_model/serializer.rb +++ b/lib/active_model/serializer.rb @@ -109,7 +109,7 @@ def initialize(object, options={}) @convert_type = options[:convert_type] @options = options.reject{|k,v| [:scope, :root, :meta_key, :meta].include?(k) } end - attr_accessor :object, :scope, :meta_key, :meta, :root, :convert_type, : options + attr_accessor :object, :scope, :meta_key, :meta, :root, :convert_type, :options def json_key if root == true || root.nil? diff --git a/lib/active_model/serializer/associations.rb b/lib/active_model/serializer/associations.rb index 76574dd78..3e6431c3e 100644 --- a/lib/active_model/serializer/associations.rb +++ b/lib/active_model/serializer/associations.rb @@ -34,8 +34,9 @@ def embed=(embed) @embed_objects = embed == :object || embed == :objects end - def build_serializer(object, options = {}) - @serializer_class.new(object, options.merge(@options)) + def build_serializer(object, convert_type = nil) + @serializer_class ||= Serializer.serializer_for(object) || DefaultSerializer + @serializer_class.new(object, @options.merge(:convert_type => convert_type)) end private diff --git a/test/unit/active_model/serializer/has_many_test.rb b/test/unit/active_model/serializer/has_many_test.rb index 88b6b6a9a..6f6372b6d 100644 --- a/test/unit/active_model/serializer/has_many_test.rb +++ b/test/unit/active_model/serializer/has_many_test.rb @@ -153,7 +153,7 @@ def serializable_object end assert_equal({ - 'post' => { title: 'Title 1', body: 'Body 1', comments: { my_content: ['fake'] } } + 'post' => { title: 'Title 1', body: 'Body 1', created_at: nil, updated_at: nil, comments: { my_content: ['fake'] } } }, @post_serializer.as_json) end end