Git Product home page Git Product logo

grape-active_model_serializers's People

Contributors

andrusha avatar blackxored avatar dandehavilland avatar danielochoa avatar dblock avatar drn avatar giaogiaocat avatar ignat-z avatar johnallen3d avatar jrhe avatar jwkoelewijn avatar knao124 avatar kpassapk avatar nicolasleger avatar olleolleolle avatar purem avatar radanskoric avatar sbounmy avatar schickling avatar sethherr avatar siong1987 avatar sonxurxo avatar syntaxterr0r avatar tevanoff avatar tfe avatar unpublishedworks avatar xn avatar zph avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

grape-active_model_serializers's Issues

Breaks my project by invoking AR somewhere

I don't know but I've been tracing and the behavior comes from this gem. It fails on database cleaner because somehow it's AR used, which I'm not (all Mongoid), this shouldn't be specific to this project and will give more information as I continue to figure this out. Do you have any clues on this?

how to use helpers to conditionally include an attribute?

I'm confused on how to go about what I'm trying to do. Seems like it should be easy.

I'm building an API that doesn't have users, but it does have applications that connect to it via OAuth.

I have a grape endpoint:

desc "Get widgets"
get '/' do
  @widgets = Widget.all
  @widgets
end

I have a serializer:

class WidgetSerializer < ActiveModel::Serializer
  attributes :name, :color
end

I have a helper:

module ApplicationHelpers
  def current_application
    # returns an AuthorizedApplication object based on the current authentication
  end
end

What I want to do is have my WidgetSerializer conditionally include a secret_number attribute in the JSON only for applications with the proper role (e.g. current_application.has_role?(:fullaccess))

# this doesn't work, obviously
class WidgetSerializer < ActiveModel::Serializer
  attributes :name, :color
  attributes :secret_number if current_application.has_role?(:fullaccess)
end

What am I missing? I've tried a bunch of things online and none seem to work. What's the correct way to do this with grape-active_model_serializers?

Thanks for your help...

Namespaces with associations

Hi, I have a bunch of serializers which have associations. Unfortunately I just realised when one of them links to a serializer below it in the directory structure(alpahbetically organised by module), I get an error saying Uninitialized Constant in production.

E.g.

Articles
--ArticleSerializer
Likes
--LikeSerializer
  belongs_to :user, serializer: Users::UserSerializer
  belongs_to :article, serializer: Articles::ArticleSerializer
Users
--UserSerializer

belongs_to :user, serializer: Users::UserSerializer does not work because the UserSerializer is defined after it. However, if i rename the module of Users to ABC so it looks like

ABC
--UserSerializer
Articles
--ArticleSerializer
Likes
--LikeSerializer
  belongs_to :user, serializer: ABC::UserSerializer
  belongs_to :article, serializer: Articles::ArticleSerializer

it works! Is this a bug for module processing with associations?

Is this gem on rubygems.org?

This gem is not on rubygems.org yet, is it?

The only way I got it to install was with this in my Gemfile:

gem 'grape-active_model_serializers', :git => 'git://github.com/jrhe/grape-active_model_serializers.git'

Changing adapter in AMS is not affected to output

active_model_serializers-0.10.0
grape-active_model_serializers (1.3.2)

I'd like to have :json as the default adapter for AMS, so I've initialized it:

ActiveModelSerializers.config.adapter = :json

But nothing was changed and I've started looking into source.
As I've found grape-AMS using serializer.new here to create serialized_resource. But this approach does not allow us to apply adapter, include meta etc. For that we should user ActiveModelSerializers::SerializableResource as here.

No access to grape helpers

One of the key things about ActiveModelSerializers is it's access to your ActionController helpers.

It would be great if this plug could also give access to grapes helpers so you can use them to determine how to serialise content.

For instance sometimes I need to check if object == current_user to determine whether sensitive details should be returned.

rails 4.2.0rc1 can't modify frozen ActiveRecord::LazyAttributeHash

When using grape active model serializers with grape API I updated from Rails 4.2.0.beta4 to 4.2.0rc1 and suddenly all actions that return a destroyed object (product.destroy)result in
can't modify frozen ActiveRecord::LazyAttributeHash
The fix was to specify a serializer or return something other than the object which was frozen after destroy.

Exception: RuntimeError: can't modify frozen ActiveRecord::LazyAttributeHash
  0: /Users/brian/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.0.rc1/lib/active_record/attribute_set/builder.rb:79:in `materialize'
  1: /Users/brian/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.0.rc1/lib/active_record/attribute_set/builder.rb:23:in `select'
  2: /Users/brian/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.0.rc1/lib/active_record/attribute_set.rb:70:in `initialized_attributes'
  3: /Users/brian/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.0.rc1/lib/active_record/attribute_set.rb:18:in `to_hash'
  4: /Users/brian/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.0.rc1/lib/active_record/attribute_methods.rb:284:in `attributes'
  5: /Users/brian/.rvm/gems/ruby-2.1.5/gems/activemodel-4.2.0.rc1/lib/active_model/serialization.rb:100:in `serializable_hash'
  6: /Users/brian/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.0.rc1/lib/active_record/serialization.rb:17:in `serializable_hash'
  7: /Users/brian/.rvm/gems/ruby-2.1.5/gems/activemodel-4.2.0.rc1/lib/active_model/serializers/json.rb:99:in `as_json'
  8: /Users/brian/.rvm/gems/ruby-2.1.5/gems/activesupport-4.2.0.rc1/lib/active_support/json/encoding.rb:34:in `encode'
  9: /Users/brian/.rvm/gems/ruby-2.1.5/gems/activesupport-4.2.0.rc1/lib/active_support/json/encoding.rb:21:in `encode'
 10: /Users/brian/.rvm/gems/ruby-2.1.5/gems/activesupport-4.2.0.rc1/lib/active_support/core_ext/object/json.rb:37:in `to_json_with_active_support_encoder'
 11: /Users/brian/.rvm/gems/ruby-2.1.5/gems/grape-0.9.0/lib/grape/formatter/json.rb:6:in `call'
 12: /Users/brian/.rvm/gems/ruby-2.1.5/gems/grape-active_model_serializers-1.2.1/lib/grape-active_model_serializers/formatter.rb:11:in `call'
 13: /Users/brian/.rvm/gems/ruby-2.1.5/gems/grape-0.9.0/lib/grape/middleware/formatter.rb:33:in `block in after'
 14: /Users/brian/.rvm/gems/ruby-2.1.5/gems/grape-0.9.0/lib/grape/middleware/formatter.rb:32:in `collect'                                                                                                                           [100/5640]
 15: /Users/brian/.rvm/gems/ruby-2.1.5/gems/grape-0.9.0/lib/grape/middleware/formatter.rb:32:in `after'
 16: /Users/brian/.rvm/gems/ruby-2.1.5/gems/grape-0.9.0/lib/grape/middleware/base.rb:25:in `call!'
 17: /Users/brian/.rvm/gems/ruby-2.1.5/gems/grape-0.9.0/lib/grape/middleware/base.rb:18:in `call'
 18: /Users/brian/.rvm/gems/ruby-2.1.5/gems/grape-0.9.0/lib/grape/middleware/base.rb:24:in `call!'
 19: /Users/brian/.rvm/gems/ruby-2.1.5/gems/grape-0.9.0/lib/grape/middleware/base.rb:18:in `call'
 20: /Users/brian/.rvm/gems/ruby-2.1.5/gems/grape-0.9.0/lib/grape/middleware/base.rb:24:in `call!'
 21: /Users/brian/.rvm/gems/ruby-2.1.5/gems/grape-0.9.0/lib/grape/middleware/base.rb:18:in `call'
 22: /Users/brian/.rvm/gems/ruby-2.1.5/gems/grape-0.9.0/lib/grape/middleware/error.rb:27:in `block in call!'
 23: /Users/brian/.rvm/gems/ruby-2.1.5/gems/grape-0.9.0/lib/grape/middleware/error.rb:26:in `catch'
 24: /Users/brian/.rvm/gems/ruby-2.1.5/gems/grape-0.9.0/lib/grape/middleware/error.rb:26:in `call!'
 25: /Users/brian/.rvm/gems/ruby-2.1.5/gems/grape-0.9.0/lib/grape/middleware/base.rb:18:in `call'
 26: /Users/brian/.rvm/gems/ruby-2.1.5/gems/rack-1.6.0.beta2/lib/rack/head.rb:13:in `call'
 27: /Users/brian/.rvm/gems/ruby-2.1.5/gems/rack-1.6.0.beta2/lib/rack/builder.rb:153:in `call'
 28: /Users/brian/.rvm/gems/ruby-2.1.5/gems/rollbar-1.1.0/lib/rollbar/middleware/rack/builder.rb:8:in `call_with_rollbar'
 29: /Users/brian/.rvm/gems/ruby-2.1.5/gems/grape-0.9.0/lib/grape/endpoint.rb:172:in `call!'
 30: /Users/brian/.rvm/gems/ruby-2.1.5/gems/grape-0.9.0/lib/grape/endpoint.rb:160:in `call'
 31: /Users/brian/.rvm/gems/ruby-2.1.5/gems/rack-mount-0.8.3/lib/rack/mount/route_set.rb:152:in `block in call'
 32: /Users/brian/.rvm/gems/ruby-2.1.5/gems/rack-mount-0.8.3/lib/rack/mount/code_generation.rb:96:in `block in recognize'
 33: /Users/brian/.rvm/gems/ruby-2.1.5/gems/rack-mount-0.8.3/lib/rack/mount/code_generation.rb:68:in `optimized_each'
 34: /Users/brian/.rvm/gems/ruby-2.1.5/gems/rack-mount-0.8.3/lib/rack/mount/code_generation.rb:95:in `recognize'
 35: /Users/brian/.rvm/gems/ruby-2.1.5/gems/rack-mount-0.8.3/lib/rack/mount/route_set.rb:141:in `call'
 36: /Users/brian/.rvm/gems/ruby-2.1.5/gems/grape-0.9.0/lib/grape/api.rb:128:in `call'
 37: /Users/brian/.rvm/gems/ruby-2.1.5/gems/grape-0.9.0/lib/grape/api.rb:43:in `call!'
 38: /Users/brian/.rvm/gems/ruby-2.1.5/gems/grape-0.9.0/lib/grape/api.rb:39:in `call'
 39: /Users/brian/.rvm/gems/ruby-2.1.5/gems/actionpack-4.2.0.rc1/lib/action_dispatch/routing/mapper.rb:51:in `serve'
 40: /Users/brian/.rvm/gems/ruby-2.1.5/gems/actionpack-4.2.0.rc1/lib/action_dispatch/journey/router.rb:43:in `block in serve'
 41: /Users/brian/.rvm/gems/ruby-2.1.5/gems/actionpack-4.2.0.rc1/lib/action_dispatch/journey/router.rb:30:in `each'
 42: /Users/brian/.rvm/gems/ruby-2.1.5/gems/actionpack-4.2.0.rc1/lib/action_dispatch/journey/router.rb:30:in `serve'
 43: /Users/brian/.rvm/gems/ruby-2.1.5/gems/actionpack-4.2.0.rc1/lib/action_dispatch/routing/route_set.rb:800:in `call'
 44: /Users/brian/.rvm/gems/ruby-2.1.5/gems/rack-pjax-0.8.0/lib/rack/pjax.rb:12:in `call'
 45: /Users/brian/.rvm/gems/ruby-2.1.5/gems/omniauth-1.2.2/lib/omniauth/strategy.rb:404:in `call_app!'
 46: /Users/brian/.rvm/gems/ruby-2.1.5/gems/omniauth-1.2.2/lib/omniauth/strategy.rb:262:in `mock_call!'
 47: /Users/brian/.rvm/gems/ruby-2.1.5/gems/omniauth-1.2.2/lib/omniauth/strategy.rb:181:in `call!'
 48: /Users/brian/.rvm/gems/ruby-2.1.5/gems/omniauth-1.2.2/lib/omniauth/strategy.rb:164:in `call'
 49: /Users/brian/.rvm/gems/ruby-2.1.5/gems/omniauth-1.2.2/lib/omniauth/builder.rb:59:in `call'
 50: /Users/brian/.rvm/gems/ruby-2.1.5/gems/rollbar-1.1.0/lib/rollbar/middleware/rails/rollbar_request_store.rb:13:in `call'
 51: /Users/brian/.rvm/gems/ruby-2.1.5/gems/warden-1.2.3/lib/warden/manager.rb:35:in `block in call'
 52: /Users/brian/.rvm/gems/ruby-2.1.5/gems/warden-1.2.3/lib/warden/manager.rb:34:in `catch'
 53: /Users/brian/.rvm/gems/ruby-2.1.5/gems/warden-1.2.3/lib/warden/manager.rb:34:in `call'
 54: /Users/brian/.rvm/gems/ruby-2.1.5/gems/rack-1.6.0.beta2/lib/rack/etag.rb:24:in `call'
 55: /Users/brian/.rvm/gems/ruby-2.1.5/gems/rack-1.6.0.beta2/lib/rack/conditionalget.rb:38:in `call'
 56: /Users/brian/.rvm/gems/ruby-2.1.5/gems/rack-1.6.0.beta2/lib/rack/head.rb:13:in `call'
 57: /Users/brian/.rvm/gems/ruby-2.1.5/gems/remotipart-1.2.1/lib/remotipart/middleware.rb:27:in `call'
 58: /Users/brian/.rvm/gems/ruby-2.1.5/gems/actionpack-4.2.0.rc1/lib/action_dispatch/middleware/params_parser.rb:27:in `call'
 59: /Users/brian/.rvm/gems/ruby-2.1.5/gems/actionpack-4.2.0.rc1/lib/action_dispatch/middleware/flash.rb:260:in `call'
 60: /Users/brian/.rvm/gems/ruby-2.1.5/gems/rack-1.6.0.beta2/lib/rack/session/abstract/id.rb:225:in `context'
 61: /Users/brian/.rvm/gems/ruby-2.1.5/gems/rack-1.6.0.beta2/lib/rack/session/abstract/id.rb:220:in `call'
 62: /Users/brian/.rvm/gems/ruby-2.1.5/gems/actionpack-4.2.0.rc1/lib/action_dispatch/middleware/cookies.rb:560:in `call'
 63: /Users/brian/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.0.rc1/lib/active_record/query_cache.rb:36:in `call'
 64: /Users/brian/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.0.rc1/lib/active_record/connection_adapters/abstract/connection_pool.rb:647:in `call'
 64: /Users/brian/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.0.rc1/lib/active_record/connection_adapters/abstract/connection_pool.rb:647:in `call'                                                                                   [50/5640]
 65: /Users/brian/.rvm/gems/ruby-2.1.5/gems/actionpack-4.2.0.rc1/lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
 66: /Users/brian/.rvm/gems/ruby-2.1.5/gems/activesupport-4.2.0.rc1/lib/active_support/callbacks.rb:88:in `call'
 67: /Users/brian/.rvm/gems/ruby-2.1.5/gems/activesupport-4.2.0.rc1/lib/active_support/callbacks.rb:88:in `_run_callbacks'
 68: /Users/brian/.rvm/gems/ruby-2.1.5/gems/activesupport-4.2.0.rc1/lib/active_support/callbacks.rb:734:in `_run_call_callbacks'
 69: /Users/brian/.rvm/gems/ruby-2.1.5/gems/activesupport-4.2.0.rc1/lib/active_support/callbacks.rb:81:in `run_callbacks'
 70: /Users/brian/.rvm/gems/ruby-2.1.5/gems/actionpack-4.2.0.rc1/lib/action_dispatch/middleware/callbacks.rb:27:in `call'
 71: /Users/brian/.rvm/gems/ruby-2.1.5/gems/actionpack-4.2.0.rc1/lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
 72: /Users/brian/.rvm/gems/ruby-2.1.5/gems/actionpack-4.2.0.rc1/lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
 73: /Users/brian/.rvm/gems/ruby-2.1.5/gems/rollbar-1.1.0/lib/rollbar/middleware/rails/show_exceptions.rb:19:in `call_with_rollbar'
 74: /Users/brian/.rvm/gems/ruby-2.1.5/gems/actionpack-4.2.0.rc1/lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
 75: /Users/brian/.rvm/gems/ruby-2.1.5/gems/railties-4.2.0.rc1/lib/rails/rack/logger.rb:38:in `call_app'

Optional parameters for serializer

Hi,

I'm in a position where I need to pass additional parameters to the serializer. I pull a collection of locations, and for each item, I need to show the distance from that requests IP to the Location object. The problem is the only way I can see to do this is something like:

locations.each do |location|
    location.source_coords = [lat, lng]
end
locations

The problem is that it's not the Location objects responsibility to deal with this, it's more the responsibility of the serializer. How can I pass the param to the serializer?

Thanks

Test failures about Hash#deep_dup missing

This Issue is to mark the presence of a CI failure like

https://travis-ci.com/olleolleolle/grape-active_model_serializers/jobs/173305202

#render
  with meta key
    includes meta key and content (FAILED - 1)
  with a custom meta_key
    includes the custom meta key name (FAILED - 2)
    ignores a lonely meta_key (FAILED - 3)
  junk keys
    ignores junk keys (FAILED - 4)
    ignores empty meta_key (FAILED - 5)
    ignores empty meta (FAILED - 6)
  1) #render with meta key includes meta key and content
     Failure/Error:
       ::ActiveModelSerializers::Adapter.create(
         serializer, options
       ).to_json
     
     NoMethodError:
       undefined method `deep_dup' for {}:Hash
     # /home/travis/.rvm/gems/ruby-2.5.0/gems/active_model_serializers-0.10.8/lib/active_model/serializer.rb:350:in `block in associations'
     # /home/travis/.rvm/gems/ruby-2.5.0/gems/active_model_serializers-0.10.8/lib/active_model/serializer.rb:406:in `each'
     # /home/travis/.rvm/gems/ruby-2.5.0/gems/active_model_serializers-0.10.8/lib/active_model/serializer.rb:406:in `each_with_object'
     # /home/travis/.rvm/gems/ruby-2.5.0/gems/active_model_serializers-0.10.8/lib/active_model/serializer.rb:406:in `associations_hash'
     # /home/travis/.rvm/gems/ruby-2.5.0/gems/active_model_serializers-0.10.8/lib/active_model/serializer.rb:367:in `serializable_hash'
     # /home/travis/.rvm/gems/ruby-2.5.0/gems/active_model_serializers-0.10.8/lib/active_model_serializers/adapter/attributes.rb:7:in `serializable_hash'
     # /home/travis/.rvm/gems/ruby-2.5.0/gems/active_model_serializers-0.10.8/lib/active_model_serializers/adapter/json.rb:6:in `serializable_hash'
     # /home/travis/.rvm/gems/ruby-2.5.0/gems/active_model_serializers-0.10.8/lib/active_model_serializers/adapter/base.rb:59:in `as_json'
     # /home/travis/.rvm/gems/ruby-2.5.0/gems/activesupport-5.2.2/lib/active_support/json/encoding.rb:35:in `encode'
     # /home/travis/.rvm/gems/ruby-2.5.0/gems/activesupport-5.2.2/lib/active_support/json/encoding.rb:22:in `encode'
     # /home/travis/.rvm/gems/ruby-2.5.0/gems/activesupport-5.2.2/lib/active_support/core_ext/object/json.rb:41:in `to_json'
     # ./lib/grape-active_model_serializers/formatter.rb:12:in `call'
     # /home/travis/.rvm/gems/ruby-2.5.0/gems/grape-0.9.0/lib/grape/middleware/formatter.rb:33:in `block in after'
     # /home/travis/.rvm/gems/ruby-2.5.0/gems/grape-0.9.0/lib/grape/middleware/formatter.rb:32:in `collect'
     # /home/travis/.rvm/gems/ruby-2.5.0/gems/grape-0.9.0/lib/grape/middleware/formatter.rb:32:in `after'
     # /home/travis/.rvm/gems/ruby-2.5.0/gems/grape-0.9.0/lib/grape/middleware/base.rb:25:in `call!'
     # /home/travis/.rvm/gems/ruby-2.5.0/gems/grape-0.9.0/lib/grape/middleware/base.rb:18:in `call'
     # /home/travis/.rvm/gems/ruby-2.5.0/gems/grape-0.9.0/lib/grape/middleware/error.rb:27:in `block in call!'
     # /home/travis/.rvm/gems/ruby-2.5.0/gems/grape-0.9.0/lib/grape/middleware/error.rb:26:in `catch'
     # /home/travis/.rvm/gems/ruby-2.5.0/gems/grape-0.9.0/lib/grape/middleware/error.rb:26:in `call!'
     # /home/travis/.rvm/gems/ruby-2.5.0/gems/grape-0.9.0/lib/grape/middleware/base.rb:18:in `call'
     # /home/travis/.rvm/gems/ruby-2.5.0/gems/rack-2.0.6/lib/rack/head.rb:12:in `call'
     # /home/travis/.rvm/gems/ruby-2.5.0/gems/rack-2.0.6/lib/rack/builder.rb:153:in `call'
     # /home/travis/.rvm/gems/ruby-2.5.0/gems/grape-0.9.0/lib/grape/endpoint.rb:172:in `call!'
     # /home/travis/.rvm/gems/ruby-2.5.0/gems/grape-0.9.0/lib/grape/endpoint.rb:160:in `call'
     # /home/travis/.rvm/gems/ruby-2.5.0/gems/rack-mount-0.8.3/lib/rack/mount/route_set.rb:152:in `block in call'
     # /home/travis/.rvm/gems/ruby-2.5.0/gems/rack-mount-0.8.3/lib/rack/mount/code_generation.rb:96:in `block in recognize'
     # /home/travis/.rvm/gems/ruby-2.5.0/gems/rack-mount-0.8.3/lib/rack/mount/code_generation.rb:68:in `optimized_each'
     # /home/travis/.rvm/gems/ruby-2.5.0/gems/rack-mount-0.8.3/lib/rack/mount/code_generation.rb:95:in `recognize'
     # /home/travis/.rvm/gems/ruby-2.5.0/gems/rack-mount-0.8.3/lib/rack/mount/route_set.rb:141:in `call'
     # /home/travis/.rvm/gems/ruby-2.5.0/gems/grape-0.9.0/lib/grape/api.rb:128:in `call'
     # /home/travis/.rvm/gems/ruby-2.5.0/gems/grape-0.9.0/lib/grape/api.rb:43:in `call!'
     # /home/travis/.rvm/gems/ruby-2.5.0/gems/grape-0.9.0/lib/grape/api.rb:39:in `call'
     # /home/travis/.rvm/gems/ruby-2.5.0/gems/rack-test-1.1.0/lib/rack/mock_session.rb:29:in `request'
     # /home/travis/.rvm/gems/ruby-2.5.0/gems/rack-test-1.1.0/lib/rack/test.rb:266:in `process_request'
     # /home/travis/.rvm/gems/ruby-2.5.0/gems/rack-test-1.1.0/lib/rack/test.rb:129:in `custom_request'
     # /home/travis/.rvm/gems/ruby-2.5.0/gems/rack-test-1.1.0/lib/rack/test.rb:58:in `get'
     # ./spec/features/grape-active_model_serializers/render_spec.rb:18:in `get_resource_with'
     # ./spec/features/grape-active_model_serializers/render_spec.rb:24:in `block (3 levels) in <top (required)>'

set scope from params

I need to set the serializer scope from the params, I'm trying like that:

get ':id', scope: { showings: params[:showings] } do
  Movie.find(params[:id])
end

but params is not yet defined.

Is there way to do that?

undefined method `deconstantize' for nil:NilClass

Hello,

Im using Grape, sequel, rake and grape-active_model_serializers( no rails ) for my little application.

Here is code of problem place

helpers do
 def dictionaries_collection
    @dictionaries_collection ||= if params[:page].present?
      DictionarySearch.new(search_params).results.paginate(params[:page], params[:per] ? params[:per] : 50)
    else
      DictionarySearch.new(search_params).results
    end
  end
end

namespace :dictionaries do
  # GET dictionaries
  desc 'List of dictionaries ', {
    http_codes: [
      [200, 'Ok']
    ]
  }

  params do
    optional :per, type: Integer, desc: 'Records count on a page'
    optional :page, type: Integer, default: 1, desc: 'Page number'
    optional :q, type: Hash do
      optional :name_like, type: String, desc: 'Name includes …'
      optional :ancestor_id, type: Integer, desc: 'Parent id'
    end
  end

  get '/', each_serialize: DictionarySerializer do
    dictionaries_collection
  end
end

When im trying to take list of dictionaries im taking such error

Here is backtrace

caught error of type NoMethodError in after callback inside Grape::Middleware::Formatter : undefined method `deconstantize' for nil:NilClass
NoMethodError: undefined method `deconstantize' for nil:NilClass
	/home/ivan/.rvm/gems/ruby-2.3.1/gems/grape-active_model_serializers-1.5.0/lib/grape-active_model_serializers/serializer_resolver.rb:82:in `resource_namespace'
	/home/ivan/.rvm/gems/ruby-2.3.1/gems/grape-active_model_serializers-1.5.0/lib/grape-active_model_serializers/serializer_resolver.rb:72:in `resource_serializer_klass'
	/home/ivan/.rvm/gems/ruby-2.3.1/gems/grape-active_model_serializers-1.5.0/lib/grape-active_model_serializers/serializer_resolver.rb:58:in `namespace_inferred_class'
	/home/ivan/.rvm/gems/ruby-2.3.1/gems/grape-active_model_serializers-1.5.0/lib/grape-active_model_serializers/serializer_resolver.rb:22:in `serializer_class'
	/home/ivan/.rvm/gems/ruby-2.3.1/gems/grape-active_model_serializers-1.5.0/lib/grape-active_model_serializers/serializer_resolver.rb:10:in `serializer'
	/home/ivan/.rvm/gems/ruby-2.3.1/gems/grape-active_model_serializers-1.5.0/lib/grape-active_model_serializers/formatter.rb:27:in `fetch_serializer'
	/home/ivan/.rvm/gems/ruby-2.3.1/gems/grape-active_model_serializers-1.5.0/lib/grape-active_model_serializers/formatter.rb:7:in `call'
	/home/ivan/.rvm/gems/ruby-2.3.1/gems/grape-0.19.2/lib/grape/middleware/formatter.rb:44:in `block in build_formatted_response'
	/home/ivan/.rvm/gems/ruby-2.3.1/gems/grape-0.19.2/lib/grape/middleware/formatter.rb:44:in `collect'
	/home/ivan/.rvm/gems/ruby-2.3.1/gems/grape-0.19.2/lib/grape/middleware/formatter.rb:44:in `build_formatted_response'
	/home/ivan/.rvm/gems/ruby-2.3.1/gems/grape-0.19.2/lib/grape/middleware/formatter.rb:28:in `after'
	/home/ivan/.rvm/gems/ruby-2.3.1/gems/grape-0.19.2/lib/grape/middleware/base.rb:34:in `call!'
	/home/ivan/.rvm/gems/ruby-2.3.1/gems/grape-0.19.2/lib/grape/middleware/base.rb:24:in `call'
	/home/ivan/.rvm/gems/ruby-2.3.1/gems/grape-0.19.2/lib/grape/middleware/error.rb:34:in `block in call!'
	/home/ivan/.rvm/gems/ruby-2.3.1/gems/grape-0.19.2/lib/grape/middleware/error.rb:33:in `catch'
	/home/ivan/.rvm/gems/ruby-2.3.1/gems/grape-0.19.2/lib/grape/middleware/error.rb:33:in `call!'
	/home/ivan/.rvm/gems/ruby-2.3.1/gems/grape-0.19.2/lib/grape/middleware/base.rb:24:in `call'
	/home/ivan/.rvm/gems/ruby-2.3.1/gems/rack-2.0.1/lib/rack/head.rb:12:in `call'
	/home/ivan/.rvm/gems/ruby-2.3.1/gems/grape-0.19.2/lib/grape/endpoint.rb:224:in `call!'
	/home/ivan/.rvm/gems/ruby-2.3.1/gems/grape-0.19.2/lib/grape/endpoint.rb:218:in `call'
	/home/ivan/.rvm/gems/ruby-2.3.1/gems/grape-0.19.2/lib/grape/router/route.rb:72:in `exec'
	/home/ivan/.rvm/gems/ruby-2.3.1/gems/grape-0.19.2/lib/grape/router.rb:119:in `process_route'
	/home/ivan/.rvm/gems/ruby-2.3.1/gems/grape-0.19.2/lib/grape/router.rb:74:in `block in identity'
	/home/ivan/.rvm/gems/ruby-2.3.1/gems/grape-0.19.2/lib/grape/router.rb:93:in `transaction'
	/home/ivan/.rvm/gems/ruby-2.3.1/gems/grape-0.19.2/lib/grape/router.rb:72:in `identity'
	/home/ivan/.rvm/gems/ruby-2.3.1/gems/grape-0.19.2/lib/grape/router.rb:57:in `block in call'
	/home/ivan/.rvm/gems/ruby-2.3.1/gems/grape-0.19.2/lib/grape/router.rb:135:in `with_optimization'
	/home/ivan/.rvm/gems/ruby-2.3.1/gems/grape-0.19.2/lib/grape/router.rb:56:in `call'
	/home/ivan/.rvm/gems/ruby-2.3.1/gems/grape-0.19.2/lib/grape/api.rb:119:in `call'
	/home/ivan/.rvm/gems/ruby-2.3.1/gems/grape-0.19.2/lib/grape/api.rb:45:in `call!'
	/home/ivan/.rvm/gems/ruby-2.3.1/gems/grape-0.19.2/lib/grape/api.rb:40:in `call'
	/home/ivan/workspace/rest-nsi/app/app.rb:19:in `call'
	/home/ivan/.rvm/gems/ruby-2.3.1/gems/rack-cors-0.4.0/lib/rack/cors.rb:80:in `call'
	/home/ivan/.rvm/gems/ruby-2.3.1/gems/rack-2.0.1/lib/rack/tempfile_reaper.rb:15:in `call'
	/home/ivan/.rvm/gems/ruby-2.3.1/gems/rack-2.0.1/lib/rack/lint.rb:49:in `_call'
	/home/ivan/.rvm/gems/ruby-2.3.1/gems/rack-2.0.1/lib/rack/lint.rb:37:in `call'
	/home/ivan/.rvm/gems/ruby-2.3.1/gems/rack-2.0.1/lib/rack/show_exceptions.rb:23:in `call'
	/home/ivan/.rvm/gems/ruby-2.3.1/gems/rack-2.0.1/lib/rack/common_logger.rb:33:in `call'
	/home/ivan/.rvm/gems/ruby-2.3.1/gems/rack-2.0.1/lib/rack/chunked.rb:54:in `call'
	/home/ivan/.rvm/gems/ruby-2.3.1/gems/rack-2.0.1/lib/rack/content_length.rb:15:in `call'
	/home/ivan/.rvm/gems/ruby-2.3.1/gems/puma-3.8.2/lib/puma/configuration.rb:224:in `call'
	/home/ivan/.rvm/gems/ruby-2.3.1/gems/puma-3.8.2/lib/puma/server.rb:600:in `handle_request'
	/home/ivan/.rvm/gems/ruby-2.3.1/gems/puma-3.8.2/lib/puma/server.rb:435:in `process_client'
	/home/ivan/.rvm/gems/ruby-2.3.1/gems/puma-3.8.2/lib/puma/server.rb:299:in `block in run'
	/home/ivan/.rvm/gems/ruby-2.3.1/gems/puma-3.8.2/lib/puma/thread_pool.rb:120:in `block in spawn_thread'

All other routes(get(id), patch, all other) which are working with single record(not array) are fine.

Could you help me with this issue?

Documentation on the ErrorSerializer

I've tried getting it to serialize ActiveModel errors, but to no avail.

Here's what I've got going on..

I've tried both of these for good measure.

# /api/v1/defaults.rb
error_formatter :json, Grape::Formatter::ActiveModelSerializers
error_formatter :jsonapi, Grape::Formatter::ActiveModelSerializers
# api/v1/users.rb, includes defaults

params do
  requires :id, types: String, desc: 'User ID'
end
route_param :id do
  before do
    @user = User.where(id: params[:id]).first!
  end

  desc 'Update a user'
  params do
    optional :first_name, type: String, desc: 'First Name'
    optional :last_name, type: String, desc: 'Last Name'
    optional :email, type: String, desc: 'Email Address'
  end
  patch do
      @user.update(params)
  end
end

Any suggestions greatly appreciated!

Convert all AM keys from snake_case to camelCase

When returning JSON it does make no sense to expose the non-JSON ruby like variable naming scheme, imho. Is there a way to configure the serializer to convert the key names to proper json key names?

Custom Metadata not woking

Hi! I try to use custom metadata to add token included to response In rails 6.

get 'users', serializer: UserSerializer do
   authenticate_request!
   collection = User.all
   render collection, { meta: { token: "token key" }, meta_key: :token_info }
end

But It's just return list user. How i can use custom metadata at this case ? Thank you

Gem.lock

grape-active_model_serializers (1.5.2)
      active_model_serializers (>= 0.10.0)
      grape (>= 0.8.0)

Array root behavior results in less dry code

What is the reason for not using the serializer's root if it's set?

It seems to me like that should be the expected behavior. If you set a self.root = "XXX" in your serializer, that is the root for the array, and that can only be overridden by the root in grape itself.

I can submit a pull request with the change if there isn't a specific reason you've chosen not to implement that pattern.

1.3.1 breaks hash in response

This code:

get 'test' do
    {test: 1}
end

produces [["test",1]] in version 1.3.1, instead of the expected result {"test":1}.

If I turn off the formatter :json, Grape::Formatter::ActiveModelSerializers, then the result is correct.

Custom formatted error messages

I'd like to be able to deliver my errors in jsonapi format. I wrote some code to get it done. If this is something the maintainers are interested in, I can flesh it out with tests and such.

PR here:
#76

Thanks

not working on custom json response

I tried to response user with serialize, but not success

class UserSerializer < ActiveModel::Serializer
  attributes :id, :first_name, :last_name, :email
end

class Registrations < Grape::API
   resource :users do
      get "/" do
         {"message": "Hello", user: User.first}
      end
   end
end

I want result like that {message: "hello", user: {id: 1, first_name: "A", last_name: "B", email: "[email protected]"}}
but it response all attributes of User

API Versioning: Supporting Active Model Serializers namespacing

Hi,

I have a versioned API with versioned Serializers.
Here is my tree:

app/
  api/
    v1/
      base.rb # V1::Base
    v2/
      base.rb # V2::Base
  api.rb

  serializers/
    v1/
      my_shiny_serializer.rb # V1::MyShinySerializer
    v2/
      my_shiny_serializer.rb # V2::MyShinySerializer

I had to monkey patch this gem like in the following code to make it supports the ActiveModelSerializer feature that allow it to look for namespaced Serializers, please see comments in the patch below.

My question is: what is the best way to properly add this feature to the gem?

module Grape
  module Formatter
    module ActiveModelSerializers
      class << self
        def fetch_serializer(resource, env)
          endpoint = env['api.endpoint']
          options = build_options_from_endpoint(endpoint)
          # Monkey patch is essentially here:
          # First I retrieve the current module in which Grape's block is declared
          klass = endpoint.options[:for].to_s
          # Then, I extract the top module name (ie: V1, V2) in the fashion of Rails Inflectors "demodulize"
          # but instead of extracting top right Constant, I take the top left Constant
          namespace = klass[0, klass.index('::') || 0]

          # Finally, I pass the extracted namespace to ActiveModelSerializer 
          # so that it can find my serializer in the good namespace/directory
          serializer = options.fetch(:serializer, ActiveModel::Serializer.serializer_for(resource, { namespace: namespace }))
          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
      end
    end
  end
end

Array root documentation

Maybe I am missing the point... but I don't understand how, why or when the root option works – I am putting it in all sorts of places, on get/post, on namespaces/resources, on CollectionSerializer constructs... and it is never doing what I expect.

I am expecting root: 'users' to make the output:

{
  "users": [
    {
      "name": "steve"
    }
  ]
}

I see past issues that this changed at some point, is there documentation missing around this?

Also I found an issue from years ago that seemed to indicate root: false stopped working on collections at some point – that'd be handy too

Am I right in thinking that a CollectionSerializer with a root option doesn't realise that root until an Adapter handles it or something?

In a bunch of cases in the API I am working on we are returning CollectionSerializer instances from controller action, I've been toying with switching those to a render call – but that doesn't accept any options either

'current_user' helper can't be accessed from serializers

as example said https://github.com/jrhe/grape-active_model_serializers#current_user

grape_helper.rb :

def current_user
  @current_user ||= UserSession.create(User.find_by({access_token: params[:token]}, false)).user
end

grape module :

get ':id/works' do
    generate_mate_and_array Model.find(params[:id])
end

serializer.rb

attributes :id, :user
def user
    current_user
end

But when I call this api, caused an error : " undefined method current_user "

add delegate in baseSerializer

  delegate :current_user, to: :scope

caused an error too " BaseSerializer#current_user delegated to scope.current_user, but scope is nil ".

How can I use current_user in serializer ?

default_serializer_options example is missing an "s" in helpers

The README.md file has the following example:

helper do
def default_serializer_options
{only: params[:only], except: params[:except]}
end
end

Should it be "helpers" instead of "helper"? I received an error until I added the "s" and then it worked as intended.

Issue with Grape: undefined local variable or method `settings' for #<Grape::Endpoint>

I encountered the following error when using grape-active_model_serializers 1.2.0 with grape 0.10.1. I tried version 1.3.1 and the issue still exists.

undefined local variable or method `settings' for #<Grape::Endpoint:0x007f9ed79dd690>

grape-active_model_serializers (1.2.0) lib/grape-active_model_serializers/endpoint_extension.rb:12:in `namespace_options'
grape-active_model_serializers (1.2.0) lib/grape-active_model_serializers/formatter.rb:27:in `build_options_from_endpoint'
grape-active_model_serializers (1.2.0) lib/grape-active_model_serializers/formatter.rb:17:in `fetch_serializer'
grape-active_model_serializers (1.2.0) lib/grape-active_model_serializers/formatter.rb:6:in `call'
grape (0.10.1) lib/grape/middleware/formatter.rb:33:in `block in after'
grape (0.10.1) lib/grape/middleware/formatter.rb:32:in `collect'
grape (0.10.1) lib/grape/middleware/formatter.rb:32:in `after'
grape (0.10.1) lib/grape/middleware/base.rb:25:in `call!'
grape (0.10.1) lib/grape/middleware/base.rb:18:in `call'
grape (0.10.1) lib/grape/middleware/base.rb:24:in `call!'
grape (0.10.1) lib/grape/middleware/base.rb:18:in `call'
grape (0.10.1) lib/grape/middleware/error.rb:27:in `block in call!'
grape (0.10.1) lib/grape/middleware/error.rb:26:in `catch'
grape (0.10.1) lib/grape/middleware/error.rb:26:in `call!'
grape (0.10.1) lib/grape/middleware/base.rb:18:in `call'
rack (1.5.2) lib/rack/head.rb:11:in `call'
rack (1.5.2) lib/rack/builder.rb:138:in `call'
grape (0.10.1) lib/grape/endpoint.rb:196:in `call!'
grape (0.10.1) lib/grape/endpoint.rb:184:in `call'
rack-mount (0.8.3) lib/rack/mount/route_set.rb:152:in `block in call'
rack-mount (0.8.3) lib/rack/mount/code_generation.rb:96:in `block in recognize'
rack-mount (0.8.3) lib/rack/mount/code_generation.rb:75:in `optimized_each'
rack-mount (0.8.3) lib/rack/mount/code_generation.rb:95:in `recognize'
rack-mount (0.8.3) lib/rack/mount/route_set.rb:141:in `call'
grape (0.10.1) lib/grape/api.rb:102:in `call'
grape (0.10.1) lib/grape/api.rb:33:in `call!'
grape (0.10.1) lib/grape/api.rb:29:in `call'
actionpack (4.1.8) lib/action_dispatch/journey/router.rb:73:in `block in call'
actionpack (4.1.8) lib/action_dispatch/journey/router.rb:59:in `each'
actionpack (4.1.8) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:678:in `call'
warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
warden (1.2.3) lib/warden/manager.rb:34:in `catch'
warden (1.2.3) lib/warden/manager.rb:34:in `call'
rack (1.5.2) lib/rack/etag.rb:23:in `call'
rack (1.5.2) lib/rack/conditionalget.rb:25:in `call'
rack (1.5.2) lib/rack/head.rb:11:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/flash.rb:254:in `call'
rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.1.8) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.1.8) lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in `call'
activerecord (4.1.8) lib/active_record/migration.rb:380:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.1.8) lib/active_support/callbacks.rb:82:in `run_callbacks'
actionpack (4.1.8) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.1.8) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.1.8) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.1.8) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.1.8) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.1.8) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.1.8) lib/rails/rack/logger.rb:20:in `call'
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
activesupport (4.1.8) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/static.rb:84:in `call'
railties (4.1.8) lib/rails/engine.rb:514:in `call'
railties (4.1.8) lib/rails/application.rb:144:in `call'
newrelic_rpm (3.9.8.273) lib/new_relic/rack/developer_mode.rb:56:in `traced_call'
newrelic_rpm (3.9.8.273) lib/new_relic/agent/instrumentation/middleware_tracing.rb:55:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
rack (1.5.2) lib/rack/content_length.rb:14:in `call'
rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'

Issues versioning serializers with Grape 1.4 master

I'm building an API with:

  • Ruby 2.2
  • Rails 4.2.6
  • grape-gem 0.16.2
  • active_model_serializers-gem 0.10.2
  • grape-active_model_serializers-gem (1.4 from master)

My JSON serializers work well until I try to introduce API Versioning as described here: https://github.com/ruby-grape/grape-active_model_serializers

Unversioned serializer:

class SignupSerializer < ActiveModel::Serializer
  attributes :id, :comments, :pending
end

Versioned serializer:

module Mobile
  module V1
    class SignupSerializer < ActiveModel::Serializer
      attributes :id, :comments, :pending
    end
  end
end

The error message is:

LoadError (Unable to autoload constant SignupSerializer, expected /Users/username/GitHub/AppName/app/serializers/mobile/v1/signup_serializer.rb to define it):
app/api/mobile/logger.rb:16:in `block in call'
app/api/mobile/logger.rb:15:in `call'

API Organization:

enter image description here

base.rb:

require 'grape-swagger'

module Mobile
  class Dispatch < Grape::API
    mount Mobile::V1::Root
    mount Mobile::V2::Root
    format :json
    route :any, '*path' do
      Rack::Response.new({message: 'Not found'}.to_json, 404).finish
    end
    add_swagger_documentation
  end

  Base = Rack::Builder.new do
    use Mobile::Logger
    run Mobile::Dispatch
  end
end

v1/root.rb:

module Mobile
  module V1
    class Root < Dispatch
      format :json
      formatter :json, Grape::Formatter::ActiveModelSerializers
      version 'v1'
      default_error_formatter :json
      content_type :json, 'application/json'
      use ::WineBouncer::OAuth2

      rescue_from :all do |e|
        eclass = e.class.to_s
        message = "OAuth error: #{e.to_s}" if eclass.match('WineBouncer::Errors')
        status = case 
        when eclass.match('OAuthUnauthorizedError')
          401
        when eclass.match('OAuthForbiddenError')
          403
        when eclass.match('RecordNotFound'), e.message.match(/unable to find/i).present?
          404
        else
          (e.respond_to? :status) && e.status || 500
        end
        opts = { error: "#{message || e.message}" }
        opts[:trace] = e.backtrace[0,10] unless Rails.env.production?
        Rack::Response.new(opts.to_json, status, {
          'Content-Type' => "application/json",
          'Access-Control-Allow-Origin' => '*',
          'Access-Control-Request-Method' => '*',
        }).finish
      end

      mount Mobile::V1::Me
      mount Mobile::V1::Events
      mount Mobile::V1::Signups

      route :any, '*path' do
        raise StandardError, 'Unable to find endpoint'
      end
    end
  end
end

api/v1/signup.rb:

module Mobile
  module V1
    class Signups < Mobile::V1::Root
      include Mobile::V1::Defaults

      resource :signups, desc: 'Operations about the signups' do

        desc "Returns list of signups"
        oauth2 # This endpoint requires authentication

        get '/' do
          Signup.where(pending: false)
        end

      end

    end

  end
end

Any ideas?

Gem Release?

It would be great if there was another release of the gem. I just got bit by the fact that the README states support for current_user which was added back in October of last year but hasn't been released yet.

current_user & grape helper

Hi,

As i'm trying to access to my current_user helper from a serializer, it appears my serializer can't find the method. My helper works great in Grape context and is very basic:

module UserHelpers
  extend Grape::API::Helpers

  def current_user
    @current_user
  end
end

The documentation way don't seems to work either:

helpers do 
  def current_user
    @current_user
  end
end

... And my instance variable @current_user can't be accessed from a serializer, too.

Does this feature really works or was removed in active_models_serializers 0.9.1 ? Can't find a clue in this gem codebase nor in active_models_serializers codebase.

I would be happy to fix it in the gem, but can't find how.

meta bug

grape-active_model_serializers 1.2.0
active_model_serializers 0.8.0

issues: undefined method `render' for #Grape::Endpoint:0x007f1c89ab9350

get "/homes" do
collection = User.all
render collection, { meta: { page: 5, current_page: 3 }, meta_key: :pagination_info }
end

:serializer option not working on collection as a result requests

Hi, I'm using grape-active_model_serializers-1.2.1 in a Rails 4.1.5 project. Specifying a different serializer works only for single instance

resources :items do
  desc "Return all items"
  get '', :serializer => WhiteItemSerializer do
    Item.all
  end

  desc "Return a item"
  params do
    requires :id, type: String, desc: "ID of the item"
  end
  get ":id", :serializer => WhiteItemSerializer  do
    Item.where(id: permitted_params[:id]).first!
  end

end

GET /api/v1/items/{id} works while GET /api/v1/items raises an exception:

NoMethodError at /api/v1/items
==============================

> undefined method `read_attribute_for_serialization' for #<Item::ActiveRecord_Relation:0x007f9065781cf0>

activerecord (4.1.5) lib/active_record/relation/delegation.rb, line 136
-----------------------------------------------------------------------

``` ruby
  131         elsif array_delegable?(method)
  132           to_a.public_send(method, *args, &block)
  133         elsif arel.respond_to?(method)
  134           arel.public_send(method, *args, &block)
  135         else
> 136           super
  137         end
  138       end
  139     end
  140   end
```

App backtrace
-------------



Full backtrace
--------------

 - activerecord (4.1.5) lib/active_record/relation/delegation.rb:136:in `method_missing'
 - activerecord (4.1.5) lib/active_record/relation/delegation.rb:99:in `method_missing'
 - active_model_serializers (0.9.0) lib/active_model/serializer.rb:101:in `block (2 levels) in attributes'
 - active_model_serializers (0.9.0) lib/active_model/serializer.rb:155:in `block in attributes'
 - active_model_serializers (0.9.0) lib/active_model/serializer.rb:154:in `attributes'
 - active_model_serializers (0.9.0) lib/active_model/serializer.rb:260:in `serializable_object'
 - active_model_serializers (0.9.0) lib/active_model/serializable.rb:6:in `block in as_json'
 - activesupport (4.1.5) lib/active_support/notifications.rb:161:in `instrument'
 - active_model_serializers (0.9.0) lib/active_model/serializable.rb:33:in `instrument'
 - active_model_serializers (0.9.0) lib/active_model/serializable.rb:4:in `as_json'
 - activesupport (4.1.5) lib/active_support/json/encoding.rb:34:in `encode'
 - activesupport (4.1.5) lib/active_support/json/encoding.rb:21:in `encode'
 - activesupport (4.1.5) lib/active_support/core_ext/object/json.rb:37:in `to_json_with_active_support_encoder'
 - grape-active_model_serializers (1.2.1) lib/grape-active_model_serializers/formatter.rb:9:in `call'
 - grape (0.9.0) lib/grape/middleware/formatter.rb:33:in `block in after'
 - grape (0.9.0) lib/grape/middleware/formatter.rb:32:in `after'
 - grape (0.9.0) lib/grape/middleware/base.rb:25:in `call!'
 - grape (0.9.0) lib/grape/middleware/base.rb:18:in `call'
 - grape (0.9.0) lib/grape/middleware/base.rb:24:in `call!'
 - grape (0.9.0) lib/grape/middleware/base.rb:18:in `call'
 - grape (0.9.0) lib/grape/middleware/error.rb:27:in `block in call!'
 - grape (0.9.0) lib/grape/middleware/error.rb:26:in `call!'
 - grape (0.9.0) lib/grape/middleware/base.rb:18:in `call'
 - rack (1.5.2) lib/rack/head.rb:11:in `call'
 - rack (1.5.2) lib/rack/builder.rb:138:in `call'
 - grape (0.9.0) lib/grape/endpoint.rb:172:in `call!'
 - grape (0.9.0) lib/grape/endpoint.rb:160:in `call'
 - rack-mount (0.8.3) lib/rack/mount/route_set.rb:152:in `block in call'
 - rack-mount (0.8.3) lib/rack/mount/code_generation.rb:96:in `block in recognize'
 - rack-mount (0.8.3) lib/rack/mount/code_generation.rb:68:in `optimized_each'
 - rack-mount (0.8.3) lib/rack/mount/code_generation.rb:95:in `recognize'
 - rack-mount (0.8.3) lib/rack/mount/route_set.rb:141:in `call'
 - grape (0.9.0) lib/grape/api.rb:128:in `call'
 - grape (0.9.0) lib/grape/api.rb:43:in `call!'
 - grape (0.9.0) lib/grape/api.rb:39:in `call'
 - actionpack (4.1.5) lib/action_dispatch/journey/router.rb:71:in `block in call'
 - actionpack (4.1.5) lib/action_dispatch/journey/router.rb:59:in `call'
 - actionpack (4.1.5) lib/action_dispatch/routing/route_set.rb:678:in `call'
 - meta_request (0.3.4) lib/meta_request/middlewares/app_request_handler.rb:13:in `call'
 - meta_request (0.3.4) lib/meta_request/middlewares/meta_request_handler.rb:13:in `call'
 - rack-cors (0.2.9) lib/rack/cors.rb:54:in `call'
 - warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
 - warden (1.2.3) lib/warden/manager.rb:34:in `call'
 - rack (1.5.2) lib/rack/etag.rb:23:in `call'
 - rack (1.5.2) lib/rack/conditionalget.rb:25:in `call'
 - rack (1.5.2) lib/rack/head.rb:11:in `call'
 - actionpack (4.1.5) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
 - actionpack (4.1.5) lib/action_dispatch/middleware/flash.rb:254:in `call'
 - rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context'
 - rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
 - actionpack (4.1.5) lib/action_dispatch/middleware/cookies.rb:560:in `call'
 - activerecord (4.1.5) lib/active_record/query_cache.rb:36:in `call'
 - activerecord (4.1.5) lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in `call'
 - activerecord (4.1.5) lib/active_record/migration.rb:380:in `call'
 - actionpack (4.1.5) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
 - activesupport (4.1.5) lib/active_support/callbacks.rb:82:in `run_callbacks'
 - actionpack (4.1.5) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
 - actionpack (4.1.5) lib/action_dispatch/middleware/reloader.rb:73:in `call'
 - actionpack (4.1.5) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
 - better_errors (2.0.0) lib/better_errors/middleware.rb:84:in `protected_app_call'
 - better_errors (2.0.0) lib/better_errors/middleware.rb:79:in `better_errors_call'
 - better_errors (2.0.0) lib/better_errors/middleware.rb:57:in `call'
 - rack-contrib (1.1.0) lib/rack/contrib/response_headers.rb:17:in `call'
 - meta_request (0.3.4) lib/meta_request/middlewares/headers.rb:16:in `call'
 - actionpack (4.1.5) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
 - actionpack (4.1.5) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
 - railties (4.1.5) lib/rails/rack/logger.rb:38:in `call_app'
 - railties (4.1.5) lib/rails/rack/logger.rb:20:in `block in call'
 - activesupport (4.1.5) lib/active_support/tagged_logging.rb:68:in `block in tagged'
 - activesupport (4.1.5) lib/active_support/tagged_logging.rb:26:in `tagged'
 - activesupport (4.1.5) lib/active_support/tagged_logging.rb:68:in `tagged'
 - railties (4.1.5) lib/rails/rack/logger.rb:20:in `call'
 - quiet_assets (1.0.3) lib/quiet_assets.rb:23:in `call_with_quiet_assets'
 - actionpack (4.1.5) lib/action_dispatch/middleware/request_id.rb:21:in `call'
 - rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
 - rack (1.5.2) lib/rack/runtime.rb:17:in `call'
 - activesupport (4.1.5) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
 - rack (1.5.2) lib/rack/lock.rb:17:in `call'
 - actionpack (4.1.5) lib/action_dispatch/middleware/static.rb:64:in `call'
 - rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
 - railties (4.1.5) lib/rails/engine.rb:514:in `call'
 - railties (4.1.5) lib/rails/application.rb:144:in `call'
 - rack (1.5.2) lib/rack/lock.rb:17:in `call'
 - rack (1.5.2) lib/rack/content_length.rb:14:in `call'
 - rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
 - /Users/masciugo/.rbenv/versions/2.1.2/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'
 - /Users/masciugo/.rbenv/versions/2.1.2/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
 - /Users/masciugo/.rbenv/versions/2.1.2/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread' 

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.