Comments (8)
Hmm, if I using shop entity like this
present Shop.find(params[:id]), with: API::Entity::Shop, type: :full
Then the shop's owner field is automatically display as a User entity in full. Is is the expected behaviour? What if I only want to present the shop entity in full only?
from grape-entity.
Then the owner field automatically display the shop entity in full. Is is the expected behaviour?
You have in you'r Shop entity also fields exposed with expose :..., if: { type: :full }
right?
And now you see all fields of Shop and all fields of User? I think that this is the expected
behaviour. I would use 2 different options. :full_user_info and :full_shop_info.
from grape-entity.
@linhmtran168 Just ran into this myself. This is working for me (so far).
entities/alert.rb
module Api
class Entities::Alert < Grape::Entity
def initialize(object, options = {})
options.reverse_merge!(short_city: true)
super
end
root 'alerts', 'alert'
expose :id, documentation: { type: :integer, desc: nil }
expose :city, using: Entities::City, documentation: {type: 'City' }
end
end
entities/city.rb
module Api
class Entities::City < Grape::Entity
root 'cities', 'city'
expose :id, documentation: { type: :integer, desc: nil }
expose :name, documentation: { type: :string, desc: nil }
expose :latitude, unless: :short_city, documentation: { type: :float, desc: nil }
expose :longitude, unless: :short_city, documentation: { type: :float, desc: nil }
end
end
endpoints/alerts.rb
desc "Return list of current user's alerts"
get '', entity: Entities::Alert do
present current_user.alerts, with: Entities::Alert
end
from grape-entity.
@linhmtran168 I am closing this, please feel free to bring it up on the Grape mailing list if none of the solutions pan out.
from grape-entity.
I think this should be reopened, it's a missing feature.
It's not currently possible to change variables from an entity.
Another user case that would not be possible because of this missing feature is referencing other objects of the same entity. Since we cannot attribute variables, we'd fall in a circular trap.
from grape-entity.
@leods92 Can you open a new issue for each of these, please?
from grape-entity.
from grape-entity.
For anyone coming here from Google, I have a different work-around for this, using the OO approach, as hinted in the README (i.e class StatusDetailed < API::Entities::Status
).
I wanted 3 slightly different versions, when
- embedding in a parent response
- the index/list view
- the show/detail view
It would be great if I could just do :
class API::Entities::Shop < API::Entities::Base
expose :owner, using: API::Entity::User, type: :base
end
But I can't, so I used OO to define each use-case, which is arguably better.
api/entities/user.rb
class API::Entities::User < API::Entities::Base
expose :id
expose :username
end
api/entities/user_index.rb
class API::Entities::UserIndex < API::Entities::User
expose :billing_address
end
api/entities/user_show.rb
class API::Entities::UserShow < API::Entities::UserIndex
expose :shipping_addresses
end
Then I can use them like so:
class API::Entities::Shop < API::Entities::Base
expose :owner, using: API::Entity::User # base version
end
Then define the API and use with:
to declarative the right entity version to use:
class API::V1::Shop < API::V1::Base
get '/' do
present Shop.all, with: API::Entities::Shop # returns user.id, user.username
end
end
class API::V1::User < API::V1::Base
get '/' do
present Shop.all, with: API::Entities::UserIndex # returns user.id, user.username, user.billing_address
end
params do
requires :id, type: Integer
end
get ':id' do
present User.find(params[:id]), with: API::Entities::UserShow # returns user.id, user.username, user.billing_address, user.shipping_addresses
end
end
from grape-entity.
Related Issues (20)
- How to get the specified expose
- Adding default value to expose
- Version 0.8 compatibility with grape 1.3.1 HOT 3
- Why docs suggest strings for root option? HOT 1
- Sending an array of hash HOT 1
- Proxy API modeling HOT 1
- Grape::Entity#as_json does not return a recursive set of hashes.
- Ruby 3: Entities exposing fields via Symbol#to_proc are broken HOT 3
- Frozen/Deadlocked Ruby thread instead of NoMethodError HOT 1
- expose_nil option doesn't respect Entity.hash_access
- Same name nested arguments get value from root HOT 5
- Inheritance issue with the entity HOT 2
- Inheritance issue with the grape-entity version >= 0.5.0
- `FetchableObject` can cause unexpected behavior HOT 4
- Override Grape Entity Model Description
- Truncate Formatter
- Expose default value doesn't work for me HOT 2
- `expose_nil:` conflicts with options passing to a block
- `route.entity` can not get entity class in Rails 7.1
- Custom Entity options support
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-entity.