This enables the deferral of the resolution of the serializer class to prevent NameError exceptions due to reference cycles between serializer classes.
We do not know the type for computed attributes, so we pick nil.
Perhaps at some point we might add a :type option for attributes (or
not), but in any case it's important to not crash when there are
computed attributes.
To achieve this, we make the following change when sideloading: Instead
of serializing associations and discarding duplicate *hashes*, we
memorize the *objects* (records) that we have already serialized, and
only serialize those that are new.
This change is mostly transparent, and brings down serialization time
from 3.1 seconds to 1.0 seconds on my set of sample data.
There is one change in the behavior: If you sideload the same object
multiple times, and it yields different hashes, like so:
embed :ids, include: true
has_many :comments
has_many :recent_comments, root: comments, serializer: CommentShortSerializer
then previously, it would be included multiple times, whereas now, the
first hash wins. (I haven't actually tested this.) I don't know that
either option is preferable. It's not covered by the test suite, and I
think it's an edge case that is OK to ignore entirely.
ActiveRecord::Base and ActionController::Base"
The whole idea of having a lazy hook is that it can be executed
in the context of different targets. The moment you hardcode the class,
the hooks can no longer run in the proper context.
This reverts commit 506e2ac9ad.
Take this serializer:
class TodoSerializer < ActiveModel::Serializer
root :todo, :include => true
has_one :reference, :polymorphic => true
end
A nil reference would generate this JSON:
{
"todo": { "reference": null },
"nil_classes": []
}
This commit prevents the `nil_classes` key from being added when
serializing and including nil polymoprhic associations.
include! only works when the source serializer has a root set. The
as_json method sets up some state for the include! method. If a child
association has associations with `:include => true` or `root foo,
:include => true` would cause an undefined method error for `NilClass`.
This is entirely unhelpful for the end user.
This commit raise an error when this situation occurs. It makes it clear
that it's not a problem with AMS but the serialization graph.