hookercookerman / active_model_version_serializers

Versioning For ActiveModel::Serializer
MIT License
7 stars 4 forks source link

AMS root element configuration not respected #3

Open ghost opened 11 years ago

ghost commented 11 years ago

There is an issue when root element is configured this way :

ActiveSupport.on_load(:active_model_serializers) do
  # Disable for all serializers (except ArraySerializer)
  ActiveModel::Serializer.root = false
  # Disable for ArraySerializer
  ActiveModel::ArraySerializer.root = false
end

Actually the gem is doing that :

    def self.version(version, superclass = ActiveModel::Serializer, &block)
      base_class = self
      vklass = Class.new(superclass) do
        self.root(base_class._root)
        alias_method base_class._name.to_sym, :object

        singleton_class.class_eval do
          define_method(:to_s) do
            "(#{base_class.name} VERSION: #{version})"
          end
          alias inspect to_s
        end

It sets root (or not) directly based on parameters passed to the serializer :

[1] poc_api_app(#<Class>) »  superclass
=> ActiveModel::Serializer < Object
[2] poc_api_app(#<Class>) »  superclass._root?
=> false
[3] poc_api_app(#<Class>) »  base_class
=> UserSerializer < ActiveModel::VersionSerializer
[4] poc_api_app(#<Class>) »  base_class._root?
=> true

You can notice that it doesn't use my configuration and still includes the root element because it doesn't check if superclass._root? is false.

natebird commented 11 years ago

+1