-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for single record serialization
This will handle serialization of single records as used for example in show actions using the postres array serializer. To be able to do this, the record is wrapped in a relation using its primary key for lookup. It's also possible to use this feature with a relation by passing the `root: 'singular_name'` and `single_record: true` options to render or the serializer, but you have to ensure that the relation only returns a single record, for example by using `.limit(1)`. The single record serialization format does use a singular root key name for the main resource and does not wrap the object in an array: Collection: `{"notes":[{"id":1,"tag_ids":[1]}],"tags":[...]}` Single Record: `{"note":{"id":1,"tag_ids":[1]},"tags":[...]}` This adds a patch to ActiveModel::Serializer.build_json which adds the required wrapping in a relation. Note that this method will load and instantiate the record and it's serializer once and then reload it using the primary key in a relation. To avoid this you can skip the find call in the controller and replace it with a relation and pass `:root` and `:single_record` along with the singular relation in the call to `render`.
- Loading branch information
1 parent
f302397
commit 52fb8d1
Showing
6 changed files
with
108 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
module PostgresExt::Serializers::ActiveModel | ||
module Serializer | ||
def self.prepended(base) | ||
class << base | ||
prepend ClassMethods | ||
end | ||
end | ||
|
||
module ClassMethods | ||
# Wrap ActiveModel::Serializer.build_json | ||
# to send single records through ArraySerializer | ||
# enabling database serialization. | ||
def build_json(controller, resource, options) | ||
serializer_instance = super | ||
return serializer_instance unless serializer_instance | ||
|
||
default_options = controller.send(:default_serializer_options) || {} | ||
options = default_options.merge(options || {}) | ||
|
||
if ActiveRecord::Base === resource && options[:root] != false && serializer_instance.root_name != false | ||
options[:root] ||= serializer_instance.root_name | ||
options[:each_serializer] = serializer_instance.class | ||
options[:single_record] = options.fetch(:single_record, true) | ||
options.delete(:serializer) # Reset to default ArraySerializer. | ||
|
||
# Wrap Record in a Relation. | ||
klass = resource.class | ||
primary_key = klass.primary_key | ||
resource = klass.where(primary_key => resource.send(primary_key)).limit(1) | ||
|
||
super(controller, resource, options) | ||
else | ||
serializer_instance | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters