rocket_pants
rocket_pants copied to clipboard
Not compatible with active_model_serializers 0.9.x
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.
Interesting - I'll take a look into how they handle the detection now and see how hard it would be to integrate into RP
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.
There is a potential integration point for this in ActiveModel::Serializer.serializer_for
, but I want to test compatibility etc before implementing it first.
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 :)
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.