module ActiveModelSerializers::LookupChain

Constants

BY_NAMESPACE

Uses the controller namespace of the resource to find the serializer

Example:

Api::V3::AuthorsController => Api::V3::AuthorSerializer
BY_PARENT_SERIALIZER

Allows for serializers to be defined in parent serializers

  • useful if a relationship only needs a different set of attributes than if it were rendered independently.

Example:

 class BlogSerializer < ActiveModel::Serializer
   class AuthorSerialier < ActiveModel::Serializer
   ...
   end

   belongs_to :author
   ...
 end

The belongs_to relationship would be rendered with
  BlogSerializer::AuthorSerialier
BY_RESOURCE

Standard appending of Serializer to the resource name.

Example:

Author => AuthorSerializer
BY_RESOURCE_NAMESPACE

Uses the namespace of the resource to find the serializer

Example:

British::Author => British::AuthorSerializer
DEFAULT

Public Instance Methods

namespace_for(klass) click to toggle source
# File lib/active_model_serializers/lookup_chain.rb, line 65
def namespace_for(klass)
  klass.name.deconstantize
end
resource_class_name(klass) click to toggle source
# File lib/active_model_serializers/lookup_chain.rb, line 69
def resource_class_name(klass)
  klass.name.demodulize
end
serializer_from(klass) click to toggle source
# File lib/active_model_serializers/lookup_chain.rb, line 77
def serializer_from(klass)
  name = resource_class_name(klass)
  serializer_from_resource_name(name)
end
serializer_from_resource_name(name) click to toggle source
# File lib/active_model_serializers/lookup_chain.rb, line 73
def serializer_from_resource_name(name)
  "#{name}Serializer"
end