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

Not compatible with active_model_serializers 0.9.x #123

Open
icem opened this issue May 5, 2015 · 5 comments
Open

Not compatible with active_model_serializers 0.9.x #123

icem opened this issue May 5, 2015 · 5 comments
Milestone

Comments

@icem
Copy link

icem commented May 5, 2015

According to active_model_serializers changelog they removed method Model#active_model_serializer. The problem that rocket_pants uses this method if it's present to get serializer from model:

serializer = object.active_model_serializer if object.respond_to?(:active_model_serializer) && serializer.nil?

The workaround to specify serializer explicitly on expose call.

@Sutto
Copy link
Owner

Sutto commented May 19, 2015

Interesting - I'll take a look into how they handle the detection now and see how hard it would be to integrate into RP

@Sutto Sutto added this to the v1.12 milestone May 19, 2015
@icem
Copy link
Author

icem commented May 19, 2015

For people on rails 4.2 looking for easy workaround while issue not fixed:
I added this to initializers folder:

# Extracted from old active_model_serializers 0.8
# This patch fixes rocket_pants 1.11.0 compatibility with active_model_serializers 0.9
module ActiveModel::SerializerSupport
  extend ActiveSupport::Concern

  module ClassMethods #:nodoc:
    if "".respond_to?(:safe_constantize)
      def active_model_serializer
        "#{self.name}Serializer".safe_constantize
      end
    else
      def active_model_serializer
        begin
          "#{self.name}Serializer".constantize
        rescue NameError => e
          raise unless e.message =~ /uninitialized constant/
        end
      end
    end
  end

  # Returns a model serializer for this object considering its namespace.
  def active_model_serializer
    self.class.active_model_serializer
  end

  alias :read_attribute_for_serialization :send
end

module ActiveModel::ArraySerializerSupport
  def active_model_serializer
    ActiveModel::ArraySerializer
  end
end

Array.send(:include, ActiveModel::ArraySerializerSupport)
Set.send(:include, ActiveModel::ArraySerializerSupport)

{
    :active_record => 'ActiveRecord::Relation',
    :mongoid => 'Mongoid::Criteria'
}.each do |orm, rel_class|
  ActiveSupport.on_load(orm) do
    include ActiveModel::SerializerSupport
    rel_class.constantize.send(:include, ActiveModel::ArraySerializerSupport)
  end
end

Be sure to remove this code when issue will be fixed.

@Sutto
Copy link
Owner

Sutto commented May 20, 2015

There is a potential integration point for this in ActiveModel::Serializer.serializer_for, but I want to test compatibility etc before implementing it first.

@Sutto
Copy link
Owner

Sutto commented May 20, 2015

For a start, given there is a work around for this - that is to say, you can manually set :serializer or define your own active_model_serializer method, I'm going to leave this out of 1.12 to avoid accidentally breaking stuff.

I'll retag this to 1.13, and take a look for that in the near future.

Again - patches / specs welcome :)

@Sutto Sutto modified the milestones: v1.13, v1.12 May 20, 2015
@SirRawlins
Copy link

Just a note, but I'm experimenting with bringing RP up to spec with AMS 0.10.x which has even further interface changes above and beyond 0.9.x

I'll let you know how I get on.

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

3 participants