Open
Conversation
|
Isn't this feature essentially already available? https://github.com/ruby-grape/grape-entity#returning-only-the-fields-you-want |
Author
|
@dchandekstark I don't think so. The reference you pointed is about evaluating (or data processing) step. But this PR is about types definition and use it with inheritance. |
Author
|
Actually, this PR is not essential. On the project we've come up with the following base class: # Example:
#
# module Map
# class UsersLocationsResponseSerializer < ApplicationSerializer
# # Array of Hashes with Symbol keys that's response of Queries::Map::LastLocations
# expose :locations, using: LocationSerializer
# # Array of Hashes with Symbol keys that's response of Queries::Map::LastActivities
# expose_nested :activities do
# expose :user_id
# expose_time :last_client_activity
# end
# # Array of Task
# expose :tasks, using: TaskSerializer, only: %i[id summary]
# # Array of User
# expose :users, using: UserSerializer, except: %i[email]
# # Boolean, true means the map should be fully redrawn (significant changes since last request)
# expose :clear
# end
# end
class ApplicationSerializer < Grape::Entity
# Extends the standard functionality with `:only` and `:except` options that
# define exact serializable fields for the nested structures specified with `:using`.
#
# @note The `:only` and `:except` options can be used only along with `:using`.
# @see Grape::Entity.expose for the standard set of attributes.
def self.expose(*attributes, only: nil, except: nil, **options)
if only || except
using_class = options.delete(:using)
raise ArgumentError, ':only or :except options can be used only with :using' unless using_class
expose(*attributes, **options, format_with: proc { using_class.represent(_1, only: only, except: except) })
else
super(*attributes, **options)
end
end
# Defines _in-line_ serializers.
def self.expose_nested(attribute, **options, &block)
entity_class = Class.new(ApplicationSerializer, &block)
const_set("#{name.demodulize}_#{attribute}", entity_class)
expose(attribute, using: entity_class, **options, &block)
end
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
These options are useful to specify exposed fields for a using entity per request:
As the result we have one shared type (
User) that can be used in different context that need only specific fields from that type.