You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm new to Grape, so maybe I'm missing something. When using a custom entity with present_collection true the implicit lookup for the entity class of records in the :items array does not work.
Thank you for looking into this.
# frozen_string_literal: truebeginrequire"bundler/inline"rescueLoadError=>e
$stderr.puts"Bundler version 1.10 or later is required. Please update your Bundler"raiseeendgemfile(true)dosource'https://rubygems.org'gem'grape'gem'grape-entity'gem'rack-test'gem'rspec'gem'byebug'endclassItemattr_accessor:id,:namedefinitialize(id,name)@id,@name=id,nameenddefto_json(_opts={}){id: id,name: name}.to_jsonenddefentityEntity.newselfendclassEntity < Grape::Entityexpose:id,as: 'THE_ID'expose:name,as: 'THE_NAME'endendclassCollectionEntity < Grape::Entitypresent_collectiontrue,:itemsexpose:metadoexpose:current_pageexpose:next_pageend# No ', using: Item::Entity' here as I'd like to use the collecion# entity for different types of objects. I'd expect the implicit entity# lookup here.expose:itemsdefnext_pagecurrent_page + 1enddefcurrent_pageoptions.fetch:current_pageendendclassAPI < Grape::APIversion'v1'format:jsonresource:itemsdodesc'Get items'paramsdooptional:page,type: Integer,default: 1optional:per_page,type: Integer,default: 10endgetdoitems=(1..1).to_a.map{ |n| Item.new(n,"Item_#{n}")}presentitems,with: CollectionEntity,current_page: params[:page]enddesc'Get a item'paramsdorequires:id,type: Integerendroute_param:iddogetdoid=params[:id]presentItem.newid,"Item_#{id}"endendendendrequire'json'require'rspec/autorun'describeAPIdoincludeRack::Test::MethodsdefappAPIendcontext'get /v1/items'do# BROKEN TEST :(it'is using Item::Entity for collection members'doget'/v1/items'expect(last_response.status).toeq(200)json=JSON.parselast_response.bodyitem=json['items'].firstexpect(item).toeq({'THE_ID'=>1,'THE_NAME'=>'Item_1'})endendcontext'get /v1/items/:id'doit'is using Item::Entity'doget'/v1/items/1'expect(last_response.status).toeq(200)item=JSON.parselast_response.bodyexpect(item).toeq({'THE_ID'=>1,'THE_NAME'=>'Item_1'})endendend
The text was updated successfully, but these errors were encountered:
Hi there!
Thanks for the great gem. I like it a lot.
I'm new to Grape, so maybe I'm missing something. When using a custom entity with
present_collection true
the implicit lookup for the entity class of records in the:items
array does not work.Thank you for looking into this.
The text was updated successfully, but these errors were encountered: