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)


87
88
89
# File 'lib/draper/decoratable.rb', line 87

def ===(other)
  super || (other.is_a?(Draper::Decorator) && 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:



57
58
59
# 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) ⇒ Class

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

Returns:

  • (Class)

    the inferred decorator class.



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

Returns:

  • (Boolean)


61
62
63
64
65
# File 'lib/draper/decoratable.rb', line 61

def decorator_class?
  decorator_class
rescue Draper::UninferrableDecoratorError
  false
end