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)


94
95
96
# File 'lib/draper/decoratable.rb', line 94

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:



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

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.



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/draper/decoratable.rb', line 78

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)


68
69
70
71
72
# File 'lib/draper/decoratable.rb', line 68

def decorator_class?
  decorator_class
rescue Draper::UninferrableDecoratorError
  false
end