rocket_pants
rocket_pants copied to clipboard
Unable to pass options to serializer when using ActiveModelSerializers
ActiveModelSerializer allows for options to be passed to the serializer from the controller. RocketPants does not seem to support this at this time. If it does, could you please point me to instructions on how to do it?
It seems like the fix for this would be to update this line (https://github.com/Sutto/rocket_pants/blob/bddc27ada67a11ff34fa8065fe2b1b2627bd28ff/lib/rocket_pants/controller/respondable.rb#L10) to pass the options
variable to #serializable_hash
.
SerializerWrapper = Struct.new(:serializer, :object) do
def serializable_hash(options = {})
instance = serializer.new(object, options)
if instance.respond_to?(:serializable_hash)
instance.serializable_hash
else
instance.as_json options
end
end
end
I would be happy to make this change and submit a PR if that is the proper fix for this issue.
@rbhitchcock I'm looking for this exact functionality myself. Have you tried to monkeypatch this and see if your concept works?
@SirRawlins Yes, I am using the following initializer...
# frozen_string_literal: true
module RocketPants
module Respondable
extend ActiveSupport::Concern
SerializerWrapper.class_eval do
# We need to pass serialiation options to our serializer, so let's modify the method
# definition. Issue opened on Github: (https://github.com/Sutto/rocket_pants/issues/141)
def serializable_hash(options = {})
instance = serializer.new(object, options)
if instance.respond_to?(:serializable_hash)
instance.serializable_hash options
else
instance.as_json options
end
end
end
end
end
Sweet. Thanks Blake - I will drop that in and see how I get on. On Feb 23, 2016 9:29 PM, "Blake Hitchcock" [email protected] wrote:
@SirRawlins https://github.com/SirRawlins Yes, I am using the following initializer...
frozen_string_literal: truemodule RocketPants
module Respondable extend ActiveSupport::Concern
SerializerWrapper.class_eval do # We need to pass serialiation options to our serializer, so let's modify the method # definition. Issue opened on Github: (https://github.com/Sutto/rocket_pants/issues/141) def serializable_hash(options = {}) instance = serializer.new(object, options) if instance.respond_to?(:serializable_hash) instance.serializable_hash options else instance.as_json options end end end
endend
— Reply to this email directly or view it on GitHub https://github.com/Sutto/rocket_pants/issues/141#issuecomment-187919796.