Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Blueprint Decorator

This example adds metadata to the output of a Blueprint, similar to Legacy/V1’s transformer feature.

class DecoratorExtension < Blueprinter::Extension
  def initialize(attr, &decorator)
    @attr = attr
    @decorator = decorator
  end

  # @param ctx [Blueprinter::V2::Context::Object]
  def around_blueprint(ctx)
    result = yield ctx
    result[@attr] = @decorator.call(ctx.object)
    result
  end
end

Add the extension to whatever blueprints need metadata:

class MyBlueprint < ApplicationBlueprint
  add DecoratorExtension.new(:metadata) { |object|
    # extract and return metadata from the object
  }
end