Method: Blueprinter::Base.identifier

Defined in:
lib/blueprinter/base.rb

.identifier(method, name: method, extractor: Blueprinter.configuration.extractor_default.new) {|object, options| ... } ⇒ Field

Specify a field or method name used as an identifier. Usually, this is something like :id

Note: identifiers are always rendered and considered their own view, similar to the :default view.

Examples:

Specifying a uuid as an identifier.

class UserBlueprint < Blueprinter::Base
  identifier :uuid
  # other code
end

Passing a block to be evaluated as the value.

class UserBlueprint < Blueprinter::Base
  identifier :uuid do |user, options|
    options[:current_user].anonymize(user.uuid)
  end
end

Yields:

  • (object, options)

    The object and the options passed to render are also yielded to the block.

    Kind of extractor to use. Either define your own or use Blueprinter’s premade extractors. Defaults to AutoExtractor

Parameters:

  • the method or field used as an identifier that you want to set for serialization.

  • (defaults to: method)

    to rename the identifier key in the JSON output. Defaults to method given.

  • (defaults to: Blueprinter.configuration.extractor_default.new)

Returns:

  • A Field object



57
58
59
60
61
62
63
64
65
# File 'lib/blueprinter/base.rb', line 57

def self.identifier(method, name: method, extractor: Blueprinter.configuration.extractor_default.new, &block)
  view_collection[:identifier] << Field.new(
    method,
    name,
    extractor,
    self,
    block: block
  )
end