Skip to content

Grape Support #1258

@bf4

Description

@bf4

https://github.com/jrhe/grape-active_model_serializers/blob/ca9c8faaa99727300ff21419aa20471ac27eed42/lib/grape-active_model_serializers/formatter.rb#L15-L25

        def fetch_serializer(resource, env)
          endpoint = env['api.endpoint']
          options = build_options_from_endpoint(endpoint)

          serializer = options.fetch(:serializer, ActiveModel::Serializer.serializer_for(resource))
          return nil unless serializer

          options[:scope] = endpoint unless options.key?(:scope)
          # ensure we have an root to fallback on
          options[:resource_name] = default_root(endpoint) if resource.respond_to?(:to_ary)
          serializer.new(resource, options.merge(other_options(env)))
        end

which uses serializer = ActiveModel::Serializer.serializer_for(resource)).new(resource, options) which is (somewhat intentionally) not supported in 0.10.

I'd be in favor of adding a grape serializer something or other (grape users can help here).

In the meantime, this may work

Grape::Formatter::ActiveModelSerializers.module_eval do # might be instance_eval
      class << self
        def fetch_serializer(resource, env)
          endpoint = env['api.endpoint']
          options = build_options_from_endpoint(endpoint)

           serializer = options.fetch(:serializer, ActiveModel::Serializer.serializer_for(resource))
-          return nil unless serializer


          options[:scope] = endpoint unless options.key?(:scope)
          # ensure we have an root to fallback on
          options[:resource_name] = default_root(endpoint) if resource.respond_to?(:to_ary)
+        serialization_options = options.merge(adapter: :json, serializer: merge!(other_options(env))).serializer) 
+          ActiveModel::SerializableResource.new(resource, serialization_options) # assuming you want json, I don't know which adapter to use

-          serializer.new(resource, options.merge(other_options(env)))
        end
  end
end

Related issues

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions