I'm a little bummed no one responded to this.  Here's some new info: 
ActiveModelSerializers already mixes in `get_serializer` to controllers. 
<https://github.com/rails-api/active_model_serializers/blob/f67fd976ecbe41305c484e4689003758e8631998/lib/action_controller/serialization.rb#L21-L30>

In AMS PR #954 
<https://github.com/rails-api/active_model_serializers/pull/954> I've moved 
the serialization logic there from _render_with_renderer_json 
<https://github.com/rails-api/active_model_serializers/blob/728b939521321f0bbe6680d4d070a4b63a1ca404/lib/action_controller/serialization.rb#L22-L49>


- def get_serializer(resource)- @_serializer ||= @_serializer_opts.delete(
:serializer)- @_serializer ||= ActiveModel::Serializer
.serializer_for(resource)-- if @_serializer_opts.key?(:each_serializer)- 
@_serializer_opts[:serializer] = @_serializer_opts.delete(:each_serializer)+ 
def get_serializer(resource, options = {})+ serializable_resource = 
ActiveModel::Serializer::build(resource, options) do |builder|+ if 
builder.serializer?+ builder.serialization_scope ||= serialization_scope+ 
builder.serialization_scope_name = _serialization_scope+ builder.adapter+ 
else+ resource+ end end-- @_serializer end
 [:_render_option_json, :_render_with_renderer_json].each do |
renderer_method| define_method renderer_method do |resource, options|- 
@_adapter_opts, @_serializer_opts =- options.partition { |k, _| 
ADAPTER_OPTION_KEYS.include? k }.map { |h| Hash[h] }-- if use_adapter? && 
(serializer = get_serializer(resource))-- @_serializer_opts[:scope] ||= 
serialization_scope- @_serializer_opts[:scope_name] = _serialization_scope-- 
# omg hax- object = serializer.new(resource, @_serializer_opts)- adapter = 
ActiveModel::Serializer::Adapter.create(object, @_adapter_opts)- super(adapter, 
options)- else- super(resource, options)- end+ serializable_resource = 
get_serializer(resource, options)+ super(serializable_resource, options) end 
end

Thanks for any eyeballs on this.  I want to help!

-Benjamin


On Thursday, June 11, 2015 at 12:34:55 AM UTC-5, Benjamin Fleischer wrote:
>
> I know y'all on core are busy.. I thought this was a reasonable proposal 
> to improve the usability or Rails, especially as rails-api gets merged in 
> https://github.com/rails/rails/pull/19832
>
> Besides JBuilder and https://github.com/rails-api/active_model_serializers, 
> I know of  https://github.com/cerebris/jsonapi-resources ( which 
> basically forces you to use their render call that wraps the model in a 
> 'resource serializer' ) and 
> https://github.com/RestPack/restpack_serializer ( which requires you wrap 
> your model in its serializer in the controller yourself ).  I think they'd 
> all benefit from having a nice place in the request lifecycle where the how 
> the rendered resource is serialized can be configured.
>
> Anyhow, basically justing bumping to see if there's interest in a PR
>

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.

Reply via email to