Comments (7)
I took some time to inspect the code for #lib/grape/entity
I see that represent expects an array of objects or a single object
# This convenience method allows you to instantiate one or more entities by
# passing either a singular or collection of objects. Each object will be
# initialized with the same options. If an array of objects is passed in,
# an array of entities will be returned. If a single object is passed in,
# a single entity will be returned.
#
# @param objects [Object or Array] One or more objects to be represented.
# @param options [Hash] Options that will be passed through to each entity
# representation.
def self.represent(objects, options = {})
if objects.is_a?(Array)
objects.map{|o| self.new(o, {:collection => true}.merge(options))}
else
self.new(objects, options)
end
end
Would it be reasonable to add a condition to map an ActiveRecord::Relation?
from grape.
I'd caution against checking whether the object is an ActiveRecord::Relation – why not do the check for Enumerable? That would, hopefully, allow compatibility with other ORMs.
from grape.
same w/ mongoid #76
from grape.
The problem with checking for Enumerable is that a Hash is also an Enumerable and a hash should not have the same behavior mapped to it. I'm open to suggestions but not entirely sure what to do to solve the issue reliably for all cases.
from grape.
OK, I think I know what needs to be done. This will solve the problem in ActiveRecord and should solve the problem in any well-implemented ORM. I'm documenting this here because I don't have time to make the change and test it right now:
Grape should do a respond_to?(:to_ary)
on the incoming object and call it if it exists. #to_ary
should only exist on objects that can be considered an array, not objects that can be converted to an array (so Hashes, which do respond to #to_a
, do not respond to #to_ary
).
objects = objects.to_ary if objects.respond_to?(:to_ary)
If someone sends a pull with tests before I get to it, I will accept. This should fix #76 as well.
from grape.
I have confirmed that changing that to check for Enumerable solves the error I am having.
from grape.
This should be fixed now.
from grape.
Related Issues (20)
- Define contract instead of params HOT 8
- undefined method `rewind' for #<Rack::Lint::Wrapper::InputWrapper w/Rack >= 3.0 HOT 4
- custom type not suppot multiple types, raise `Grape::Exceptions::ValidationErrors` HOT 1
- Route#options and Route#settings are confusing HOT 1
- Ruby 3.3 && cookiejar = ArgumentError HOT 1
- Use Rack's HeadersHash instead of {}
- endpoint method_missing vs override inspect HOT 2
- Values Validator => Proc's arity < 2 ? HOT 3
- Rails Edge requires Ruby >= 3.1
- Fix specs for Rack Edge
- Fix `params` warning method redefined
- Better tracking of gems deprecation
- Rack::Lint::Error => a header value must be a String or Array of Strings, but the value of 'content-type' is a NilClass HOT 2
- Recognize_path should account for HTTP method HOT 1
- Grape and Protecting Against Mass Assignment Abuse HOT 8
- Why do you convert PATH_INFO? Is there some RFC? HOT 2
- Add support for exclusive parameter groups (e.g. exactly_one_of) HOT 2
- Drop support rack ~> 1 ? HOT 2
- Replace Appraisals by eval_gemfile ? HOT 3
- DeprecatedConstantProxy without a deprecator is deprecated HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from grape.