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 87
def ===(other)
  super || (other.is_a?(Draper::Decorator) && 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 57
def decorate(options = {})
  decorator_class.decorate_collection(all, options.reverse_merge(with: nil))
end
decorator_class(called_on = self) 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 71
def decorator_class(called_on = self)
  prefix = respond_to?(:model_name) ? model_name : name
  decorator_name = "#{prefix}Decorator"
  decorator_name_constant = decorator_name.safe_constantize
  return decorator_name_constant unless decorator_name_constant.nil?

  if superclass.respond_to?(:decorator_class)
    superclass.decorator_class(called_on)
  else
    raise Draper::UninferrableDecoratorError.new(called_on)
  end
end
decorator_class?() click to toggle source
# File lib/draper/decoratable.rb, line 61
def decorator_class?
  decorator_class
rescue Draper::UninferrableDecoratorError
  false
end