Skip to content

Instantly share code, notes, and snippets.

@bf4
Created October 1, 2015 20:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bf4/0a82e1bc07f3ab4d654c to your computer and use it in GitHub Desktop.
Save bf4/0a82e1bc07f3ab4d654c to your computer and use it in GitHub Desktop.
activemodel::serializers architecture draft

I'd like to narrow the Serializer interface rather than broaden it. Every method we add has the potential to interfere with an attribute on the model.

Also, with respect to this change, couldn't you just use the key option?

class FooSerializer < AM::Serializer attribute :name, key: title end FooSerializer.new(foo).attributes FooSerializer._attributes_keys FooSerializer._attributes

If you want to select which attributes to use, it really needs to be when the serializer is instantiated, because you don't have control of its rendering from the controller.

I'm also against merging this until the fields instance method is removed, and until some documentation is added explaining how to use it, with an example.

I'd also rather use the rails convention of only and except as the names and add code and tests to ensure it doesn't process non-existant attributes

Can we maybe re-use some of the naming from https://github.com/rails-api/active_model_serializers/blob/1388ae82f26133d62f4e6cdfc40d1c659e121935/lib/active_model/serializer/adapter/json_api.rb#L91-L113 ?

Nested Associations Usage

rails-api/active_model_serializers#1127 (comment)

I'd like to keep the Attribute adapter slim, and would prefer to avoid using inheritance through modules to avoid inheritance through classes

rails-api/active_model_serializers#1153

@meltingice are we discussing Array.new or Article.none?

If Array.new, AMS does nothing to resources that have no serializer, and there's no serializer for the Array primitive (0.10.0 master, 0.10.0.rc2, 0.9.3, 0.8.3.

The option for manually specifying the serializer is serializer, not each_serializer. Naming :boom: As I commented there:

Get serializer either explicitly :serializer or implicitly from resource

Remove :serializer key from serializer_opts

Replace :serializer key with :each_serializer if present

Without serializer, it couldn't know the name of the instance variable. The best it could do would be given no serializer is found for the argument to json in the controller,

class ArticleController < ApplicationController def index @articles = [] render json: @articles, adapter: :json #=> { articles: [] }, where the resource root comes from the current controller. end end If Article.none (an empty collection), then it could find the serializer implicitly.

rails-api/active_model_serializers#1098

Whereas

Having SerializableResource.serialize return an instance of SerializableResource is not intuitive. It makes more sense for SerializableResource.new to return a serializable_resource A SerializableResource takes a Ruby model and presents it with a serialization interface serializable_hash, as_json, to_json that is usually consumed by the Renderer in Rails The upcoming deserialization feature takes a JSON document and returns a Ruby model The code for SerializableResource.serialize and SerializableResource.deserialize would get complicated quite quickly as they're really different responsibilities Therefore

It may be more intuitive to have a SerializableResource that takes a resource and a DeserializableDocument that takes a JSON document. It might even make sense for the DeserializableDocument to be a pure function since there may not need to be an intermediate state, as there is with the SerializableResource. Also, we might start a convention of calling subclass of an ActiveModel::Serializer an e.g. PostSerialization

Relevant PRS

#1083 #950 #950 (comment) #950 (comment) ) joaomdmoura@5bc262f ? Joao renamed PostSerializer to PostSerialization #954 #954 (comment) #954 (comment) #954 (comment) #889 (comment) Some (lightly amended) discussion of what SerializableResource does (from the slack team)

I learned about these naming issues when trying to test my serializers. (Which is part of I wanted to address in #967 and #996 ) rails-api/active_model_serializers#878 (comment) rails/rails#21496

Consider refactoring override implementation _render_with_renderer_json xml rails-api/active_model_serializers#1115

This is also a feature of the JSON API

http://jsonapi.org/format/#fetching-filtering http://jsonapi.org/recommendations/#filtering

Not sure I understand. Fields is different.

http://jsonapi.org/format/#fetching-sparse-fieldsets http://jsonapi.org/examples/

rails-api/active_model_serializers#1038

I saw to_json_with_active_support_encoder and looks like rollbar/rollbar-gem#271 and mentioned it's a bug in ActiveSupport (I see you're on 4.2.3 and 4.2.4), so I found

rails/rails#20775 (comment) Introduced in rails/rails#10278 Fixed in master rails/rails#10278 4-2 patches rails/rails@24094c3 rails/rails@49c6134 in 4-2-stable unreleased https://github.com/rails/rails/blob/4-2-stable/activesupport/lib/active_support/core_ext/object/json.rb#L29 rails/rails@d6142c7 rails/rails#20791 fix in another project: instructure/paul_bunyan@627f939 and rails/rails#21580

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment