Skip to content

Strategies: Draper (Decorator) implementation

Matt Polito edited this page Apr 13, 2017 · 4 revisions

NOTICE - This is only valid for versions < 3

If you want to use the Draper gem together with Decent Exposure, you can use the following strategy:

class DecoratorStrategy < DecentExposure::ActiveRecordWithEagerAttributesStrategy

  def resource
    r = super.tap { |r| r.class.send(:include, Draper::Decoratable) unless r.respond_to?(:decorate) }
    r.decorate
  end
end
class UsersController < ApplicationController
  expose :user, strategy: DecoratorStrategy
end

It uses the default strategy (ActiveRecordWithEagerAttributesStrategy) and does two things on top of it:

  1. Dynamically include Draper::Decoratable to the exposed object;
  2. call .decorate on the resource being exposed by decent exposure.