Code Monkey home page Code Monkey logo

Comments (8)

linhmtran168 avatar linhmtran168 commented on May 18, 2024

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.

dspaeth-faber avatar dspaeth-faber commented on May 18, 2024

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.

phallstrom avatar phallstrom commented on May 18, 2024

@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.

dblock avatar dblock commented on May 18, 2024

@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.

leods92 avatar leods92 commented on May 18, 2024

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.

dblock avatar dblock commented on May 18, 2024

@leods92 Can you open a new issue for each of these, please?

from grape-entity.

leods92 avatar leods92 commented on May 18, 2024

@dblock Done! #93 and #94

from grape-entity.

daveharris avatar daveharris commented on May 18, 2024

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

  1. embedding in a parent response
  2. the index/list view
  3. 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)

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.