def decorate(obj)
      return if defined?(Jbuilder) && (Jbuilder === obj)
      return if obj.nil?

      if obj.is_a?(Array)
        obj.each do |r|
          decorate r
        end
      elsif obj.is_a?(Hash)
        obj.each_value do |v|
          decorate v
        end
      elsif defined?(ActiveRecord) && obj.is_a?(ActiveRecord::Relation)
        # don't call each nor to_a immediately
        if obj.respond_to?(:records)
          # Rails 5.0
          obj.extend ActiveDecorator::RelationDecorator unless obj.is_a? ActiveDecorator::RelationDecorator
        else
          # Rails 3.x and 4.x
          obj.extend ActiveDecorator::RelationDecoratorLegacy unless obj.is_a? ActiveDecorator::RelationDecoratorLegacy
        end
      else
        if defined?(ActiveRecord) && obj.is_a?(ActiveRecord::Base) && !obj.is_a?(ActiveDecorator::Decorated)
          obj.extend ActiveDecorator::Decorated
        end

        d = decorator_for obj.class
        return obj unless d
        obj.extend d unless obj.is_a? d
      end

      obj
    end
def decorate(obj)
      return if defined?(Jbuilder) && (Jbuilder === obj)
      return if obj.nil?

      if obj.is_a?(Array)
        obj.each do |r|
          decorate r
        end
      elsif obj.is_a?(Hash)
        obj.each_value do |v|
          decorate v
        end
      elsif defined?(ActiveRecord) && obj.is_a?(ActiveRecord::Relation)
        # don't call each nor to_a immediately
        if obj.respond_to?(:records)
          # Rails 5.0
          obj.extend ActiveDecorator::RelationDecorator unless obj.is_a? ActiveDecorator::RelationDecorator
        else
          # Rails 3.x and 4.x
          obj.extend ActiveDecorator::RelationDecoratorLegacy unless obj.is_a? ActiveDecorator::RelationDecoratorLegacy
        end
      else
        if defined?(ActiveRecord) && obj.is_a?(ActiveRecord::Base) && !obj.is_a?(ActiveDecorator::Decorated)
          obj.extend ActiveDecorator::Decorated
        end

        d = decorator_for obj.class
        return obj unless d
        obj.extend d unless obj.is_a? d
      end

      obj
    end
Decorates the given object.
 Plus, performs special decoration for the classes below:
   Array: decorates its each element
   Hash: decorates its each value
   AR::Relation: decorates its each record lazily
   AR model: decorates its associations on the fly

 Always returns the object, regardless of whether decorated or not decorated.

 This method can be publicly called from anywhere by `ActiveDecorator::Decorator.instance.decorate(obj)`.