Skip to content
Rainer Borene edited this page Oct 27, 2017 · 2 revisions

If your decorators aren't reloading after every request on development environment then you must add this code to an initializer:

ActiveSupport::Reloader.to_prepare do
  ActiveDecorator::Decorator.class_variable_set :@@decorators, {}
end

And if you're using the decent_exposure gem you might want to include this module in your ApplicationController:

module Exposable
  extend ActiveSupport::Concern

  included do
    exposure_config :default, decorate: -> (instance) { decorate instance }
  end

  def decorate(obj)
    ActiveDecorator::Decorator.instance.decorate(obj)
  end

  module ClassMethods
    def expose(*args)
      procs = args.any? { |n| n.respond_to? :call }

      args.map! do |value|
        if value.is_a? Proc
          proc { decorate instance_exec(&value) }
        elsif value.is_a?(Hash) && !procs
          value.merge(with: :default)
        else
          value
        end
      end

      super
    end
  end
end

It's not necessary to override expose! method because active_decorator already decorates instance variables passed to views.

Clone this wiki locally