From 09a166b76b8cbd61ea7c9b39b5616006dde3686b Mon Sep 17 00:00:00 2001 From: Pol Miro Date: Sun, 24 Aug 2014 15:17:52 -0700 Subject: [PATCH 1/6] Document polymorphic associations in README.md --- README.md | 89 ++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 79 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index d6667503..ec9a0200 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,12 @@ -[![Build Status](https://api.travis-ci.org/rails-api/active_model_serializers.png?branch=0-9-stable)](https://travis-ci.org/rails-api/active_model_serializers) -[![Code Climate](https://codeclimate.com/github/rails-api/active_model_serializers.png)](https://codeclimate.com/github/rails-api/active_model_serializers) +[![Build Status](https://api.travis-ci.org/rails-api/active_model_serializers.png?branch=0-9-stable)](https://travis-ci.org/rails-api/active_model_serializers) +[![Code Climate](https://codeclimate.com/github/rails-api/active_model_serializers.png)](https://codeclimate.com/github/rails-api/active_model_serializers) # ActiveModel::Serializers ## Purpose -`ActiveModel::Serializers` encapsulates the JSON serialization of objects. -Objects that respond to read\_attribute\_for\_serialization +`ActiveModel::Serializers` encapsulates the JSON serialization of objects. +Objects that respond to read\_attribute\_for\_serialization (including `ActiveModel` and `ActiveRecord` objects) are supported. Serializers know about both a model and the `current_user`, so you can @@ -229,7 +229,7 @@ ActiveModel::Serializer.setup do |config| config.key_format = :lower_camel end -class BlogLowerCamelSerializer < ActiveModel::Serializer +class BlogLowerCamelSerializer < ActiveModel::Serializer format_keys :lower_camel end @@ -467,9 +467,6 @@ You may also use the `:serializer` option to specify a custom serializer class a Serializers are only concerned with multiplicity, and not ownership. `belongs_to` ActiveRecord associations can be included using `has_one` in your serializer. -NOTE: polymorphic was removed because was only supported for has\_one -associations and is in the TODO list of the project. - ## Embedding Associations By default, associations will be embedded inside the serialized object. So if @@ -646,8 +643,8 @@ The above would yield the following JSON document: } ``` -When side-loading data, your serializer cannot have the `{ root: false }` option, -as this would lead to invalid JSON. If you do not have a root key, the `include` +When side-loading data, your serializer cannot have the `{ root: false }` option, +as this would lead to invalid JSON. If you do not have a root key, the `include` instruction will be ignored You can also specify a different root for the embedded objects than the key @@ -714,6 +711,78 @@ data looking for information, is extremely useful. If you are mostly working with the data in simple scenarios and manually making Ajax requests, you probably just want to use the default embedded behavior. + +## Embedding Polymorphic Associations + +Because we need both the id and the type to be able to identify a polymorphic associated model, these are serialized in a slightly different format than common ones. + +When embedding entire objects: + +```ruby +class PostSerializer < ActiveModel::Serializer + attributes :id, :title + has_many :attachments, polymorphic: true +end +``` + +```json +{ + "post": { + "id": 1, + "title": "New post", + "attachments": [ + { + "type": "image" + "image": { + "id": 3 + "name": "logo" + "url": "http://images.com/logo.jpg" + } + }, + { + "type": "video" + "video": { + "id": 12 + "uid": "XCSSMDFWW" + "source": "youtube" + } + } + ] + } +} +``` + +When embedding ids: + +```ruby +class PostSerializer < ActiveModel::Serializer + embed :ids + + attributes :id, :title + has_many :attachments, polymorphic: true +end +``` + +```json +{ + "post": { + "id": 1, + "title": "New post", + "attachment_ids": [ + { + "type": "image" + "id": 12 + }, + { + "type": "video" + "id": 3 + } + ] + } +} +``` + + ## Customizing Scope In a serializer, `current_user` is the current authorization scope which the controller From 63589f75007680f85d4a59cd21ee24783ed33bc3 Mon Sep 17 00:00:00 2001 From: Pol Miro Date: Thu, 24 Oct 2013 18:30:51 -0700 Subject: [PATCH 2/6] Add tests for has_many polymorphic --- test/fixtures/poro.rb | 27 +++ .../serializer/has_many_polymorphic_test.rb | 189 ++++++++++++++++++ 2 files changed, 216 insertions(+) create mode 100644 test/unit/active_model/serializer/has_many_polymorphic_test.rb diff --git a/test/fixtures/poro.rb b/test/fixtures/poro.rb index a357c24e..2dd98642 100644 --- a/test/fixtures/poro.rb +++ b/test/fixtures/poro.rb @@ -38,6 +38,19 @@ end class WebLog < Model end +class Mail < Model + def attachments + @attachments ||= [Image.new(url: 'U1'), + Video.new(html: 'H1')] + end +end + +class Image < Model +end + +class Video < Model +end + ### ## Serializers ### @@ -73,3 +86,17 @@ end class WebLogLowerCamelSerializer < WebLogSerializer format_keys :lower_camel end + +class MailSerializer < ActiveModel::Serializer + attributes :body + + has_many :attachments, polymorphic: true +end + +class ImageSerializer < ActiveModel::Serializer + attributes :url +end + +class VideoSerializer < ActiveModel::Serializer + attributes :html +end diff --git a/test/unit/active_model/serializer/has_many_polymorphic_test.rb b/test/unit/active_model/serializer/has_many_polymorphic_test.rb new file mode 100644 index 00000000..e5180cfe --- /dev/null +++ b/test/unit/active_model/serializer/has_many_polymorphic_test.rb @@ -0,0 +1,189 @@ +require 'test_helper' + +module ActiveModel + class Serializer + class HasManyPolymorphicTest < ActiveModel::TestCase + def setup + @association = MailSerializer._associations[:attachments] + @old_association = @association.dup + + @mail = Mail.new({ body: 'Body 1' }) + @mail_serializer = MailSerializer.new(@mail) + end + + def teardown + MailSerializer._associations[:attachments] = @old_association + end + + def model_name(object) + object.class.to_s.demodulize.underscore.to_sym + end + + def test_associations_definition + assert_equal 1, MailSerializer._associations.length + assert_kind_of Association::HasMany, @association + assert_equal true, @association.polymorphic + assert_equal 'attachments', @association.name + end + + def test_associations_embedding_ids_serialization_using_serializable_hash + @association.embed = :ids + + assert_equal({ + body: 'Body 1', + 'attachment_ids' => @mail.attachments.map do |c| + { id: c.object_id, type: model_name(c) } + end + }, @mail_serializer.serializable_hash) + end + + def test_associations_embedding_ids_serialization_using_as_json + @association.embed = :ids + + assert_equal({ + 'mail' => { + :body => 'Body 1', + 'attachment_ids' => @mail.attachments.map do |c| + { id: c.object_id, type: model_name(c) } + end + } + }, @mail_serializer.as_json) + end + + def test_associations_embedding_ids_serialization_using_serializable_hash_and_key_from_options + @association.embed = :ids + @association.key = 'key' + + assert_equal({ + body: 'Body 1', + 'key' => @mail.attachments.map do |c| + { id: c.object_id, type: model_name(c) } + end + }, @mail_serializer.serializable_hash) + end + + def test_associations_embedding_objects_serialization_using_serializable_hash + @association.embed = :objects + + assert_equal({ + body: 'Body 1', + :attachments => [ + { type: :image, image: { url: 'U1' }}, + { type: :video, video: { html: 'H1' }} + ] + }, @mail_serializer.serializable_hash) + end + + def test_associations_embedding_objects_serialization_using_as_json + @association.embed = :objects + + assert_equal({ + 'mail' => { + body: 'Body 1', + attachments: [ + { type: :image, image: { url: 'U1' }}, + { type: :video, video: { html: 'H1' }} + ] + } + }, @mail_serializer.as_json) + end + + def test_associations_embedding_nil_objects_serialization_using_as_json + @association.embed = :objects + @mail.instance_eval do + def attachments + [nil] + end + end + + assert_equal({ + 'mail' => { + :body => 'Body 1', + :attachments => [nil] + } + }, @mail_serializer.as_json) + end + + def test_associations_embedding_objects_serialization_using_serializable_hash_and_root_from_options + @association.embed = :objects + @association.embedded_key = 'root' + + assert_equal({ + body: 'Body 1', + 'root' => [ + { type: :image, image: { url: 'U1' }}, + { type: :video, video: { html: 'H1' }} + ] + }, @mail_serializer.serializable_hash) + end + + def test_associations_embedding_ids_including_objects_serialization_using_serializable_hash + @association.embed = :ids + @association.embed_in_root = true + + assert_equal({ + body: 'Body 1', + 'attachment_ids' => @mail.attachments.map do |c| + { id: c.object_id, type: model_name(c) } + end + }, @mail_serializer.serializable_hash) + end + + def test_associations_embedding_ids_including_objects_serialization_using_as_json + @association.embed = :ids + @association.embed_in_root = true + + assert_equal({ + 'mail' => { + body: 'Body 1', + 'attachment_ids' => @mail.attachments.map do |c| + { id: c.object_id, type: model_name(c) } + end, + }, + attachments: [ + { type: :image, image: { url: 'U1' }}, + { type: :video, video: { html: 'H1' }} + ] + }, @mail_serializer.as_json) + end + + def test_associations_embedding_nothing_including_objects_serialization_using_as_json + @association.embed = nil + @association.embed_in_root = true + + assert_equal({ + 'mail' => { body: 'Body 1' }, + attachments: [ + { type: :image, image: { url: 'U1' }}, + { type: :video, video: { html: 'H1' }} + ] + }, @mail_serializer.as_json) + end + + def test_associations_using_a_given_serializer + @association.embed = :ids + @association.embed_in_root = true + @association.serializer_from_options = Class.new(ActiveModel::Serializer) do + def fake + 'fake' + end + + attributes :fake + end + + assert_equal({ + 'mail' => { + body: 'Body 1', + 'attachment_ids' => @mail.attachments.map do |c| + { id: c.object_id, type: model_name(c) } + end + }, + attachments: [ + { type: :image, image: { fake: 'fake' }}, + { type: :video, video: { fake: 'fake' }} + ] + }, @mail_serializer.as_json) + end + end + end +end From bb18fc62250b899253ad00117be7ff1ef3fce033 Mon Sep 17 00:00:00 2001 From: Pol Miro Date: Thu, 24 Oct 2013 23:50:00 -0800 Subject: [PATCH 3/6] Add tests for has_one polymorphic --- test/fixtures/poro.rb | 12 ++ .../serializer/has_one_polymorphic_test.rb | 196 ++++++++++++++++++ 2 files changed, 208 insertions(+) create mode 100644 test/unit/active_model/serializer/has_one_polymorphic_test.rb diff --git a/test/fixtures/poro.rb b/test/fixtures/poro.rb index 2dd98642..0ed2dafe 100644 --- a/test/fixtures/poro.rb +++ b/test/fixtures/poro.rb @@ -38,6 +38,12 @@ end class WebLog < Model end +class Interview < Model + def attachment + @attachment ||= Image.new(url: 'U1') + end +end + class Mail < Model def attachments @attachments ||= [Image.new(url: 'U1'), @@ -87,6 +93,12 @@ class WebLogLowerCamelSerializer < WebLogSerializer format_keys :lower_camel end +class InterviewSerializer < ActiveModel::Serializer + attributes :text + + has_one :attachment, polymorphic: true +end + class MailSerializer < ActiveModel::Serializer attributes :body diff --git a/test/unit/active_model/serializer/has_one_polymorphic_test.rb b/test/unit/active_model/serializer/has_one_polymorphic_test.rb new file mode 100644 index 00000000..cf5f8e7c --- /dev/null +++ b/test/unit/active_model/serializer/has_one_polymorphic_test.rb @@ -0,0 +1,196 @@ +require 'test_helper' + +module ActiveModel + class Serializer + class HasOnePolymorphicTest < ActiveModel::TestCase + def setup + @association = InterviewSerializer._associations[:attachment] + @old_association = @association.dup + + @interview = Interview.new({ text: 'Text 1' }) + @interview_serializer = InterviewSerializer.new(@interview) + end + + def teardown + InterviewSerializer._associations[:attachment] = @old_association + end + + def model_name(object) + object.class.to_s.demodulize.underscore.to_sym + end + + def test_associations_definition + assert_equal 1, InterviewSerializer._associations.length + assert_kind_of Association::HasOne, @association + assert_equal true, @association.polymorphic + assert_equal 'attachment', @association.name + end + + def test_associations_embedding_ids_serialization_using_serializable_hash + @association.embed = :ids + + assert_equal({ + text: 'Text 1', + 'attachment_id' => { + type: model_name(@interview.attachment), + id: @interview.attachment.object_id + } + }, @interview_serializer.serializable_hash) + end + + def test_associations_embedding_ids_serialization_using_as_json + @association.embed = :ids + + assert_equal({ + 'interview' => { + text: 'Text 1', + 'attachment_id' => { + type: model_name(@interview.attachment), + id: @interview.attachment.object_id + } + } + }, @interview_serializer.as_json) + end + + def test_associations_embedding_ids_serialization_using_serializable_hash_and_key_from_options + @association.embed = :ids + @association.key = 'key' + + assert_equal({ + text: 'Text 1', + 'key' => { + type: model_name(@interview.attachment), + id: @interview.attachment.object_id + } + }, @interview_serializer.serializable_hash) + end + + def test_associations_embedding_objects_serialization_using_serializable_hash + @association.embed = :objects + + assert_equal({ + text: 'Text 1', + attachment: { + type: model_name(@interview.attachment), + model_name(@interview.attachment) => { url: 'U1'} + } + }, @interview_serializer.serializable_hash) + end + + def test_associations_embedding_objects_serialization_using_as_json + @association.embed = :objects + + assert_equal({ + 'interview' => { + text: 'Text 1', + attachment: { + type: model_name(@interview.attachment), + model_name(@interview.attachment) => { url: 'U1'} + } + } + }, @interview_serializer.as_json) + end + + def test_associations_embedding_nil_ids_serialization_using_as_json + @association.embed = :ids + @interview.instance_eval do + def attachment + nil + end + end + + assert_equal({ + 'interview' => { text: 'Text 1', 'attachment_id' => nil } + }, @interview_serializer.as_json) + end + + def test_associations_embedding_nil_objects_serialization_using_as_json + @association.embed = :objects + @interview.instance_eval do + def attachment + nil + end + end + + assert_equal({ + 'interview' => { text: 'Text 1', attachment: nil } + }, @interview_serializer.as_json) + end + + def test_associations_embedding_objects_serialization_using_serializable_hash_and_root_from_options + @association.embed = :objects + @association.embedded_key = 'root' + + assert_equal({ + text: 'Text 1', + 'root' => { + type: model_name(@interview.attachment), + model_name(@interview.attachment) => { url: 'U1'} + } + }, @interview_serializer.serializable_hash) + end + + def test_associations_embedding_ids_including_objects_serialization_using_serializable_hash + @association.embed = :ids + @association.embed_in_root = true + + assert_equal({ + text: 'Text 1', + 'attachment_id' => { + type: model_name(@interview.attachment), + id: @interview.attachment.object_id + } + }, @interview_serializer.serializable_hash) + end + + def test_associations_embedding_ids_including_objects_serialization_using_as_json + @association.embed = :ids + @association.embed_in_root = true + + assert_equal({ + 'interview' => { + text: 'Text 1', + 'attachment_id' => { + type: model_name(@interview.attachment), + id: @interview.attachment.object_id + } + }, + "attachments" => [{ + type: model_name(@interview.attachment), + model_name(@interview.attachment) => { + url: 'U1' + } + }] + }, @interview_serializer.as_json) + end + + def test_associations_using_a_given_serializer + @association.embed = :ids + @association.embed_in_root = true + @association.serializer_from_options = Class.new(ActiveModel::Serializer) do + def name + 'fake' + end + + attributes :name + end + + assert_equal({ + 'interview' => { + text: 'Text 1', + 'attachment_id' => { + type: model_name(@interview.attachment), + id: @interview.attachment.object_id + } + }, + "attachments" => [{ + type: model_name(@interview.attachment), + model_name(@interview.attachment) => { + name: 'fake' + } + }] + }, @interview_serializer.as_json) + end + end + end +end From e8b983490e6f7a3d78b9ef1941f48a322461b47c Mon Sep 17 00:00:00 2001 From: Pol Miro Date: Sun, 24 Aug 2014 15:28:20 -0700 Subject: [PATCH 4/6] Separate associations into multiple files --- lib/active_model/serializer.rb | 2 +- .../{associations.rb => association.rb} | 50 +------------------ .../serializer/association/has_many.rb | 36 +++++++++++++ .../serializer/association/has_one.rb | 22 ++++++++ 4 files changed, 61 insertions(+), 49 deletions(-) rename lib/active_model/serializer/{associations.rb => association.rb} (62%) create mode 100644 lib/active_model/serializer/association/has_many.rb create mode 100644 lib/active_model/serializer/association/has_one.rb diff --git a/lib/active_model/serializer.rb b/lib/active_model/serializer.rb index e9edf41a..f9c7e0b8 100644 --- a/lib/active_model/serializer.rb +++ b/lib/active_model/serializer.rb @@ -1,6 +1,6 @@ require 'active_model/array_serializer' require 'active_model/serializable' -require 'active_model/serializer/associations' +require 'active_model/serializer/association' require 'active_model/serializer/config' require 'thread' diff --git a/lib/active_model/serializer/associations.rb b/lib/active_model/serializer/association.rb similarity index 62% rename from lib/active_model/serializer/associations.rb rename to lib/active_model/serializer/association.rb index 44d7c84f..cdfe9195 100644 --- a/lib/active_model/serializer/associations.rb +++ b/lib/active_model/serializer/association.rb @@ -1,4 +1,6 @@ require 'active_model/default_serializer' +require 'active_model/serializer/association/has_one' +require 'active_model/serializer/association/has_many' module ActiveModel class Serializer @@ -49,54 +51,6 @@ module ActiveModel def build_serializer(object, options = {}) serializer_class(object).new(object, options.merge(self.options)) end - - class HasOne < Association - def initialize(name, *args) - super - @root_key = @embedded_key.to_s.pluralize - @key ||= "#{name}_id" - end - - def serializer_class(object) - serializer_from_options || serializer_from_object(object) || default_serializer - end - - def build_serializer(object, options = {}) - options[:_wrap_in_array] = embed_in_root? - super - end - end - - class HasMany < Association - def initialize(name, *args) - super - @root_key = @embedded_key - @key ||= "#{name.to_s.singularize}_ids" - end - - def serializer_class(object) - if use_array_serializer? - ArraySerializer - else - serializer_from_options - end - end - - def options - if use_array_serializer? - { each_serializer: serializer_from_options }.merge! super - else - super - end - end - - private - - def use_array_serializer? - !serializer_from_options || - serializer_from_options && !(serializer_from_options <= ArraySerializer) - end - end end end end diff --git a/lib/active_model/serializer/association/has_many.rb b/lib/active_model/serializer/association/has_many.rb new file mode 100644 index 00000000..4480afc6 --- /dev/null +++ b/lib/active_model/serializer/association/has_many.rb @@ -0,0 +1,36 @@ +module ActiveModel + class Serializer + class Association + class HasMany < Association + def initialize(name, *args) + super + @root_key = @embedded_key + @key ||= "#{name.to_s.singularize}_ids" + end + + def serializer_class(object) + if use_array_serializer? + ArraySerializer + else + serializer_from_options + end + end + + def options + if use_array_serializer? + { each_serializer: serializer_from_options }.merge! super + else + super + end + end + + private + + def use_array_serializer? + !serializer_from_options || + serializer_from_options && !(serializer_from_options <= ArraySerializer) + end + end + end + end +end \ No newline at end of file diff --git a/lib/active_model/serializer/association/has_one.rb b/lib/active_model/serializer/association/has_one.rb new file mode 100644 index 00000000..4965e428 --- /dev/null +++ b/lib/active_model/serializer/association/has_one.rb @@ -0,0 +1,22 @@ +module ActiveModel + class Serializer + class Association + class HasOne < Association + def initialize(name, *args) + super + @root_key = @embedded_key.to_s.pluralize + @key ||= "#{name}_id" + end + + def serializer_class(object) + serializer_from_options || serializer_from_object(object) || default_serializer + end + + def build_serializer(object, options = {}) + options[:_wrap_in_array] = embed_in_root? + super + end + end + end + end +end \ No newline at end of file From 26d5fb74239bdc50cfabcd8a8d0ee3dee1409818 Mon Sep 17 00:00:00 2001 From: Pol Miro Date: Mon, 25 Aug 2014 23:26:15 -0700 Subject: [PATCH 5/6] Add support to polymorphic models --- lib/active_model/array_serializer.rb | 3 ++- lib/active_model/serializer.rb | 17 ++++++++++++++--- lib/active_model/serializer/association.rb | 4 +++- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/lib/active_model/array_serializer.rb b/lib/active_model/array_serializer.rb index 8c050770..fe985e8b 100644 --- a/lib/active_model/array_serializer.rb +++ b/lib/active_model/array_serializer.rb @@ -15,6 +15,7 @@ module ActiveModel @object = object @scope = options[:scope] @root = options.fetch(:root, self.class._root) + @polymorphic = options.fetch(:polymorphic, false) @meta_key = options[:meta_key] || :meta @meta = options[@meta_key] @each_serializer = options[:each_serializer] @@ -33,7 +34,7 @@ module ActiveModel def serializer_for(item) serializer_class = @each_serializer || Serializer.serializer_for(item) || DefaultSerializer - serializer_class.new(item, scope: scope, key_format: key_format, only: @only, except: @except) + serializer_class.new(item, scope: scope, key_format: key_format, only: @only, except: @except, polymorphic: @polymorphic) end def serializable_object diff --git a/lib/active_model/serializer.rb b/lib/active_model/serializer.rb index f9c7e0b8..abb1162d 100644 --- a/lib/active_model/serializer.rb +++ b/lib/active_model/serializer.rb @@ -130,6 +130,7 @@ end @object = object @scope = options[:scope] @root = options.fetch(:root, self.class._root) + @polymorphic = options.fetch(:polymorphic, false) @meta_key = options[:meta_key] || :meta @meta = options[@meta_key] @wrap_in_array = options[:_wrap_in_array] @@ -138,7 +139,7 @@ end @key_format = options[:key_format] @context = options[:context] end - attr_accessor :object, :scope, :root, :meta_key, :meta, :key_format, :context + attr_accessor :object, :scope, :root, :meta_key, :meta, :key_format, :context, :polymorphic def json_key key = if root == true || root.nil? @@ -225,9 +226,9 @@ end def serialize_ids(association) associated_data = send(association.name) if associated_data.respond_to?(:to_ary) - associated_data.map { |elem| elem.read_attribute_for_serialization(association.embed_key) } + associated_data.map { |elem| serialize_id(elem, association) } else - associated_data.read_attribute_for_serialization(association.embed_key) if associated_data + serialize_id(associated_data, association) if associated_data end end @@ -260,9 +261,19 @@ end hash = attributes hash.merge! associations hash = convert_keys(hash) if key_format.present? + hash = { :type => type_name(@object), type_name(@object) => hash } if @polymorphic @wrap_in_array ? [hash] : hash end alias_method :serializable_hash, :serializable_object + + def serialize_id(elem, association) + id = elem.read_attribute_for_serialization(association.embed_key) + association.polymorphic? ? { id: id, type: type_name(elem) } : id + end + + def type_name(elem) + elem.class.to_s.demodulize.underscore.to_sym + end end end diff --git a/lib/active_model/serializer/association.rb b/lib/active_model/serializer/association.rb index cdfe9195..f62c982f 100644 --- a/lib/active_model/serializer/association.rb +++ b/lib/active_model/serializer/association.rb @@ -15,6 +15,7 @@ module ActiveModel @name = name.to_s @options = options self.embed = options.fetch(:embed) { CONFIG.embed } + @polymorphic = options.fetch(:polymorphic, false) @embed_in_root = options.fetch(:embed_in_root) { options.fetch(:include) { CONFIG.embed_in_root } } @key_format = options.fetch(:key_format) { CONFIG.key_format } @embed_key = options[:embed_key] || :id @@ -27,13 +28,14 @@ module ActiveModel @serializer_from_options = serializer.is_a?(String) ? serializer.constantize : serializer end - attr_reader :name, :embed_ids, :embed_objects + attr_reader :name, :embed_ids, :embed_objects, :polymorphic attr_accessor :embed_in_root, :embed_key, :key, :embedded_key, :root_key, :serializer_from_options, :options, :key_format, :embed_in_root_key, :embed_namespace alias embed_ids? embed_ids alias embed_objects? embed_objects alias embed_in_root? embed_in_root alias embed_in_root_key? embed_in_root_key alias embed_namespace? embed_namespace + alias polymorphic? polymorphic def embed=(embed) @embed_ids = embed == :id || embed == :ids From 5c4121432c7894a05458e4a7eaf73ad4d790bd64 Mon Sep 17 00:00:00 2001 From: Gauthier Delacroix Date: Tue, 26 Aug 2014 17:17:42 +0200 Subject: [PATCH 6/6] Fix #0d31e72 regression https://github.com/rails-api/active_model_serializers/commit/0d31e72d2211b6bf7f0b0420139c4b370d6e986e --- lib/active_model_serializers.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/active_model_serializers.rb b/lib/active_model_serializers.rb index 01b2ed83..7066ebf4 100644 --- a/lib/active_model_serializers.rb +++ b/lib/active_model_serializers.rb @@ -9,7 +9,7 @@ begin require 'action_controller/serialization' require 'action_controller/serialization_test_case' - ActiveSupport.on_load(:after_initialize) do + ActiveSupport.on_load(:action_controller) do if ::ActionController::Serialization.enabled ActionController::Base.send(:include, ::ActionController::Serialization) ActionController::TestCase.send(:include, ::ActionController::SerializationAssertions)