Skip to content
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

:only/:except options for expose #353

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

ka8725
Copy link

@ka8725 ka8725 commented Jun 22, 2021

These options are useful to specify exposed fields for a using entity per request:

class User < GE
  expose :name
  expose :email
  expose :age
end

class Responce1 < GE
  expose :users, using: User, only: :name
end

class Respose2 < GE 
  expose :users, using: User, except: :email
end

class Respose3 < GE 
  expose :users, using: User, except: :name
end

As the result we have one shared type (User) that can be used in different context that need only specific fields from that type.

@dchandekstark
Copy link

Isn't this feature essentially already available? https://github.com/ruby-grape/grape-entity#returning-only-the-fields-you-want

@ka8725
Copy link
Author

ka8725 commented Apr 20, 2022

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

@ka8725
Copy link
Author

ka8725 commented Apr 20, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants