Module: Draper::Decoratable::ClassMethods

Defined in:
lib/draper/decoratable.rb

Instance Method Summary collapse

Instance Method Details

#===(other) ⇒ Boolean

Compares with possibly-decorated objects.

Returns:

  • (Boolean)


89
90
91
# File 'lib/draper/decoratable.rb', line 89

def ===(other)
  super || (other.respond_to?(:object) && super(other.object))
end

#decorate(options = {}) ⇒ Object

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

Examples:

Product.popular.decorate

Parameters:



58
59
60
61
# 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_classClass

Infers the decorator class to be used by Draper::Decoratable#decorate (e.g. Product maps to ProductDecorator).

Returns:

  • (Class)

    the inferred decorator class.



73
74
75
76
77
78
79
80
81
82
83
84
# 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?Boolean

Returns:

  • (Boolean)


63
64
65
66
67
# File 'lib/draper/decoratable.rb', line 63

def decorator_class?
  decorator_class
rescue Draper::UninferrableDecoratorError
  false
end