ruby-grape / ruby-grape/grape-entity

Sequel collection support

Open
#188 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature
Dominant language
Ruby
Stars
729
Forks
154
PR merge metrics
No merged PRs in 30d

Description

I'm trying to set up an API that uses Sequel. Trying to present a collection throws this error:

NoMethodError: Entities::Event missing attributename' on #Sequel::Postgres::Dataset:0x007fe4861eb078`

This works with ActiveRecord, but not with Sequel. The check for if an object is a collection is done by checking if the .to_ary method exists on an object. This is true for ActiveRecord, but not for Sequel.

I've made a small workaround to make it work for now, but I feel this should be part of Grape Entity and not Sequel.

module Sequel
  class Dataset
    alias_method :to_ary, :to_a
  end
end

Is there a more reliable way to check if an object is a collection? Such as doing a check on .to_a? Or to check if it is somehow Enumerable?


Example setup:

require "rubygems"
require "pg"
require "sequel"
require "grape"
require "grape-entity"

# Sequel.migration do
#   change do
#     create_table :events do
#       Integer :id, primary_key: true
#       String :name, size: 255
#       DateTime :created_at, null: true, index: true
#     end
#   end
# end

DB = Sequel.connect("postgres://user:password@localhost:5432/my_database")

module Sequel
  class Dataset
    # If this is removed the collection present doesn't work.
    alias_method :to_ary, :to_a
  end
end

class Event < Sequel::Model
end

module Entities
  class Event < Grape::Entity
    expose :name
  end
end

class API < Grape::API
  format :json

  resource :events do
    get do
      present :events, Event.all, with: Entities::Event
    end
  end
end

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Read lib/grape_entity/entity.rb at the collection check linked in the issue, then reproduce the example with a Sequel dataset and Grape presentation. Determine a collection check that works without the Sequel to_ary monkey patch, and verify that presenting Event.all returns the exposed event fields rather than raising the missing-attribute error.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.