Skip to content
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

Unable to pass options to serializer when using ActiveModelSerializers #141

Open
rbhitchcock opened this issue Feb 11, 2016 · 3 comments
Open

Comments

@rbhitchcock
Copy link

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 (

) 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.

@SirRawlins
Copy link

@rbhitchcock I'm looking for this exact functionality myself. Have you tried to monkeypatch this and see if your concept works?

@rbhitchcock
Copy link
Author

@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

@SirRawlins
Copy link

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
#141 (comment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants