Sutto / rocket_pants

API building tools on top of ActionController. Also, an awesome name.
MIT License
981 stars 130 forks source link

Unable to pass options to serializer when using ActiveModelSerializers #141

Open rbhitchcock opened 8 years ago

rbhitchcock commented 8 years ago

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.

SirRawlins commented 8 years ago

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

rbhitchcock commented 8 years ago

@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 commented 8 years ago

Sweet. Thanks Blake - I will drop that in and see how I get on. On Feb 23, 2016 9:29 PM, "Blake Hitchcock" notifications@github.com 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.