-
Notifications
You must be signed in to change notification settings - Fork 153
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature request: Expose a Hash merged to the root, not as a sub key #138
Comments
For the first part of the example my initial reaction is, why isn't this just Maybe I just don't love the name, and it could be as simple as I think |
because you can return any hash that would be merged to root, not just a specified set of keys. Let's say that only for admins I want to expose a few unknown properties on the root. If I don't know them in advance, I can't use It helps when the model is normalized and the JSON is denormalized. I wrote a 🐒 monkey patch module Grape
class Entity
def self.exposures_on_root
return @exposures_on_root unless @exposures_on_root.nil?
@exposures_on_root = []
if superclass.respond_to? :exposures_on_root
@exposures_on_root = superclass.exposures_on_root + @exposures_on_root
end
@exposures_on_root
end
def self.expose_on_root(options={}, &block)
options[:proc] = block if block_given?
exposures_on_root << options
end
alias_method :as_json_old, :as_json
alias_method :serializable_hash_old, :serializable_hash
def serializable_hash(runtime_options = {})
output = serializable_hash_old(runtime_options)
self.class.exposures_on_root.each do |exposure_on_root|
partial_output = instance_exec(object, options, &exposure_on_root[:proc])
output.reverse_merge!(partial_output)
end
output
end
alias_method :as_json, :serializable_hash
end
end
class ArticleEntity < Grape::Entity
# ...
expose_on_root do |object, options|
if options[:admin]
object.admin_metadata.merge(some_other_key: object.something_else.some_other_key)
end
end
end |
In Rabl it's called |
As much as I like |
I like |
… or into the root. This also closes ruby-grape#138
… or into the root. This also closes ruby-grape#138
… or into the root. This also closes ruby-grape#138
… or into the root. This also closes ruby-grape#138
Please check out #204 - add your 👍 or/and comment. |
@dblock thanks! I've been on the dark side of Node for a while now but it seems great. |
… or into the root. This also closes ruby-grape#138
I'd like to be able to append an arbitrary hash to the final result of the entity, without nesting it.
There are a couple of ways I can think of:
Thoughts?
The text was updated successfully, but these errors were encountered: