rails-api / active_model_serializers

ActiveModel::Serializer implementation and Rails hooks
MIT License
5.33k stars 1.39k forks source link

Unable to use a serializer which is in a folder (i.e. namespace / versioning) #1701

Closed patrickgordon closed 8 years ago

patrickgordon commented 8 years ago

Any help would be fantastic.

Expected behavior vs actual behavior

Expected: render json: @contacts, each_serializer: ContactsSerializer should work normally

Actual: Get error message: superclass must be a Class (Module given)

Steps to reproduce

(e.g., detailed walkthrough, runnable script, example application)

controller in folder: app/controllers/api/v1/contacts_controller.rb serializer in folder: app/serializers/api/v1/contacts_serializer.rb

Code in Controller:

module API::V1
  class ContactsController < ApplicationController
    def index
      @contacts = Contact.order(created_at: :desc).all
      render json: @contacts, each_serializer: ContactsSerializer
    end
    ...
  end
end

Code in serializer:

module API
  module V1
    class ContactsSerializer < ActiveModelSerializers
      attributes :first_name, :last_name, :email, :coffee_preference, :readable_date_of_birth, :age, :favourite
      ...
    end
  end
end

Environment

ActiveModelSerializers Version 0.10:

Output of ruby -e "puts RUBY_DESCRIPTION": ruby 2.2.4p230 (2015-12-16 revision 53155) [x86_64-darwin15]

OS Type & Version: Mac OSX 10.11.3

Integrated application and version (e.g., Rails, Grape, etc): Rails 4.2.6

Backtrace

(e.g., provide any applicable backtraces from your application)

app/serializers/api/v1/contacts_serializer.rb:3:in `<module:V1>'
app/serializers/api/v1/contacts_serializer.rb:2:in `<module:API>'
app/serializers/api/v1/contacts_serializer.rb:1:in `<top (required)>'
activesupport (4.2.6) lib/active_support/dependencies.rb:457:in `load'
...
bf4 commented 8 years ago

Looks like it's related to using different module nesting, which affects Ruby's constant lookup scope.

module API::V1

and

module API
  module V1

and then possibly how Rails autoloading of undefined constants works

have you tried using the absolute namespace? each_serializer: ::API::V1::ContactsSerializer?

beauby commented 8 years ago

This:

class ContactsSerializer < ActiveModelSerializers

should be

class ContactsSerializer < ActiveModel::Serializer
patrickgordon commented 8 years ago

I chose to go with a different gem. Thanks anyway for your responses.

bf4 commented 8 years ago

@patrickgordon Thanks, would you mind sharing what you're using?

patrickgordon commented 8 years ago

@bf4 I ended up going with this gem https://github.com/fotinakis/jsonapi-serializers

Cheers!

bf4 commented 8 years ago

I'd recommend https://github.com/cerebris/jsonapi-resources if it's not too late

B mobile phone

On Apr 25, 2016, at 5:30 PM, Patrick notifications@github.com wrote:

@bf4 I ended up going with this gem https://github.com/fotinakis/jsonapi-serializers

Cheers!

— You are receiving this because you were mentioned. Reply to this email directly or view it on GitHub

tomharvey commented 7 years ago

For anyone looking to achieve this in AMS, @bf4 suggestion works fine, to be explicit, the code in the serializer in api/v1/contacts_serializer.rb should be changed to

module Api::V1
    class ContactsSerializer < ActiveModelSerializers
      attributes :first_name, :last_name, :email, :coffee_preference, :readable_date_of_birth, :age, :favourite
      ...
  end
end