New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Passing serializer options for associations #331
Comments
I'd like to see how real world code would look like before that. |
I'm including some details depending on params. Looks like this: class FooSerializer < ActiveModel::Serializer
has_one :slow_to_serialize
def include_slow_to_serialize?
@options[:with_slow_to_serialize]
end
end Which works fine when I'm serializing an array of Foos. But when I have Bar: class BarSerializer < ActiveModel::Serializer
has_one :foo
end and I serialize an array of Bars, it would be nice to be able to control including slow_to_serialize. I created a second serializer inheriting from FooSerializer that has include_slow_to_serialize? always set to true, but it would be nice to be able to control it in BarSerializer's has_one. |
But that's not related with And you can already do what you say, just pass with_slow_to_serialize option to the serializer when you create it. |
OK, maybe I wasn't clear. I want to From what I understand, I have these options to do it right now:
Ideally, I'd like to change Does this make sense? |
+1 Maybe I'm going about this wrong but I find that I sometimes get in a recursive situation so I would like to do something like the following: class UserSerializer < ActiveModel::Serializer
attributes :id, :name
has_many :friends, :serializer => UserSerializer, :dont_embed_friends => true
def include_associations!
include! :friends unless @options[:dont_embed_friends]
end
end |
+1 |
1 similar comment
+1 |
+1 ! Such feature is a must for serializers. It'll make handling circular references much easier. Currently I have to create a lot of serializer classes with repeated code / huge inheritance to make this work, when there are a lot of many-to-many associations. |
+1 |
I would be great to have this feature available. Otherwise I similar cases I end up building unneccessary inherited serializers to refine some serialization options. |
Until they support passing options this way, there is a way to do it, but not as pretty: instead of using an association:
you have to define it as an attribute, and then call the serialization yourself:
Hope that makes sense and helps for now. |
Well. I piggy backed on the fact that association serializer is instantiated with the same options as the base serializer class BaseSerializer < ActiveModel::Serializer
has_one :association
def initialize(*args)
super
options[:extended_mode] = true
end
end
class AssociationSerializer < ActiveModel::Serializer
attribute :attr1
def include_attr1
options[:extended_mode]
end
end |
+1 for it.. I think there also must be a way to configure which attributes/methods do I need given a serializer (mostly to avoid circular references and/or improve speed) |
+1 |
+1 for options to serializers |
Can you guys check if master code allows you to do what you want?. |
I don't see an option to do this right now... a simple example would be:
Then call
or
|
You can get this functionality by modifying the initialize function in a base serializer... Should be easy to add to the code if desired:
|
I think this should all be hidden behind the scope object. I made a pull request that passes the scope object through to association serializers: #433 |
I've upgraded to the latest master and it seems to work so my vote is to close this issue. |
Yep this should be working. In any case comment back and I will reopen. |
So class ApplicationController < ActionController::Base
def default_serializer_options
{ url_options: url_options }.merge(super)
end
end
class ApplicationSerializer < ActiveModel::Serializer
include Rails.application.routes.url_helpers
def self.links(&block)
@links = block if block_given?
@links || Proc.new {}
end
def url_options
options[:url_options]
end
def link(rel, opts = {})
links[rel] = opts[:href]
end
def filter(keys)
self.instance_eval(&self.class.links)
keys << :links unless links.empty?
keys
end
private
def links
@links ||= {}
end
end
class BeerSerializer < ApplicationSerializer
attributes :id, :name
links do
link :self, href: beer_url(object)
end
end I'm guessing I should open a new Issue for this? |
You can access options using |
@spastorino In the example code I posted, note that I do use As far as I can tell (via association.build_serializer(object, options.merge(scope: scope)).serializable_object However, that might be a bit heavy-handed, as @lastobelus indicated in his above comment. |
To clarify - I'm talking about the options passed to an instance of an associated serializer, not the options you give when declaring the serializer via a class BrewerySerializer < ApplicationSerializer
attributes :id, :name, :website
has_many :beers, random_option: :foo
links do
link :self, href: brewery_url(object) #=> /brewery/123
link :beers, href: brewery_beers_url(object) #=> /brewery/123/beers
end
end A |
@2468ben Do you know if anything has changed here? I think it would be useful to be able to specify per-association serializer options. Here is some pseudocode (highlighted) that illustrates how it might work: https://gist.github.com/mmun/3646b88d4cb2f13a256b#file-2-blog_serializer-rb-L5-L9 |
I've committed this ^^^ to allow passing options for now but is not yet defined if that's the way it should be using it. We may move to something like contexts and we would allow passing scope and context only to associations. Then you would be able to do the dance passing whatever you like inside context all the way down. |
@spastorino Wow, awesome response time. I look forward to seeing how this progresses. I can try helping if needed. |
@mmun @spastorino That'll help out, thank you. I haven't migrated to 0.9 because the README mentioned that there would still be heavy changes (including caching) in the near future for . Are you still doing a gut renovation for 1.0, or is the current approach (minus caching) going to stay pretty close? I'm happy to wait. |
We have a roadmap here https://www.pivotaltracker.com/s/projects/978898 |
I've reverted the commit that allowed you to pass options around. I think that's a bad idea. |
:( I was using that |
One use-case for passing things around to associated serializers would be API versioning. We use this to include the version, and can then adjust the serialization output based on the client. If such info could be passed onto associated serializers (via options/scope/context/something) one could easily build basic api versioning into the serializer.
Note that we are not on master, but an older branch, so exact syntax may differ. |
Would love to see options return. The recommendation by blakewatters in this thread seems great for versioning though: |
Hi, sorry to resurface this discussion, but I'm not sure if this feature was finally provided, and if so, how to use it. Or else any workarounds. Any updates on this? |
afaict everyone has stopped using ams and gone back to jbuilder :( too bad, i like the ams interface way better Jesse Sanford
|
Not sure what poll you're referring to. Can you cite your source @oliverjesse? We for one are still using AMS... I'd also like the 411 on how to use this feature, if it exists. |
You can use the options to pass along the info to the association. An example would be: class PlantSerializer < ActiveModel::Serializer
has_one :plant_type
def initialize(object, options={})
super
# This options[:parent] will get passed through to the plant_type
options[:parent] = object.parent
end
end
class PlantTypeSerializer < ActiveModel::Serializer
def my_method
# I am able to access the previously set data using @options
if @options[:parent]
# do something special based on @options[:parent]
end
end
end There is probably an elegant solution, but this is how I am solving the problem for now. |
Is there any way to pass serializer options to associations?
I looked at the code, and it looks like it was planned, but is not "connected" yet.
ActiveModel::Serializer::Association takes the third argument for serializer_options, but I don't see it used anywhere. Perhaps getting them from options[:serializer_options] would be a good idea?
Would you accept a pull request with that change?
The text was updated successfully, but these errors were encountered: