module Draper::Decoratable::ClassMethods

Public Instance Methods

===(other) click to toggle source

Compares with possibly-decorated objects.

@return [Boolean]

Calls superclass method
# File lib/draper/decoratable.rb, line 89
def ===(other)
  super || (other.respond_to?(:object) && super(other.object))
end
decorate(options = {}) click to toggle source

Decorates a collection of objects. Used at the end of a scope chain.

@example

Product.popular.decorate

@param [Hash] options

see {Decorator.decorate_collection}.
# File lib/draper/decoratable.rb, line 58
def decorate(options = {})
  collection = Rails::VERSION::MAJOR >= 4 ? all : scoped
  decorator_class.decorate_collection(collection, options.reverse_merge(with: nil))
end
decorator_class() click to toggle source

Infers the decorator class to be used by {Decoratable#decorate} (e.g. ‘Product` maps to `ProductDecorator`).

@return [Class] the inferred decorator class.

# File lib/draper/decoratable.rb, line 73
def decorator_class
  prefix = respond_to?(:model_name) ? model_name : name
  decorator_name = "#{prefix}Decorator"
  decorator_name.constantize
rescue NameError => error
  if superclass.respond_to?(:decorator_class)
    superclass.decorator_class
  else
    raise unless error.missing_name?(decorator_name)
    raise Draper::UninferrableDecoratorError.new(self)
  end
end
decorator_class?() click to toggle source
# File lib/draper/decoratable.rb, line 63
def decorator_class?
  decorator_class
rescue Draper::UninferrableDecoratorError
  false
end