class Draper::CollectionDecorator

Attributes

context[RW]

@return [Hash] extra data to be used in user-defined methods, and passed

to each item's decorator.
decorator_class[R]

@return [Class] the decorator class used to decorate each item, as set by

{#initialize}.
object[R]

@return the collection being decorated.

Public Class Methods

decorate(object, options = {})
Alias for: new
new(object, options = {}) click to toggle source

@param [Enumerable] object

collection to decorate.

@option options [Class, nil] :with (nil)

the decorator class used to decorate each item. When `nil`, each item's
{Decoratable#decorate decorate} method will be used.

@option options [Hash] :context ({})

extra data to be stored in the collection decorator and used in
user-defined methods, and passed to each item's decorator.
# File lib/draper/collection_decorator.rb, line 29
def initialize(object, options = {})
  options.assert_valid_keys(:with, :context)
  @object = object
  @decorator_class = options[:with]
  @context = options.fetch(:context, {})
end
Also aliased as: decorate

Public Instance Methods

context=(value) click to toggle source
# File lib/draper/collection_decorator.rb, line 61
def context=(value)
  @context = value
  each {|item| item.context = value } if @decorated_collection
end
decorated?() click to toggle source

@return [true]

# File lib/draper/collection_decorator.rb, line 67
def decorated?
  true
end
decorated_collection() click to toggle source

@return [Array] the decorated items.

# File lib/draper/collection_decorator.rb, line 41
def decorated_collection
  @decorated_collection ||= object.map{|item| decorate_item(item)}
end
find(*args, &block) click to toggle source

Delegated to the decorated collection when using the block form (‘Enumerable#find`) or to the decorator class if not (`ActiveRecord::FinderMethods#find`)

# File lib/draper/collection_decorator.rb, line 48
def find(*args, &block)
  if block_given?
    decorated_collection.find(*args, &block)
  else
    ActiveSupport::Deprecation.warn("Using ActiveRecord's `find` on a CollectionDecorator is deprecated. Call `find` on a model, and then decorate the result", caller)
    decorate_item(object.find(*args))
  end
end
is_a?(klass)
Alias for: kind_of?
kind_of?(klass) click to toggle source
Calls superclass method
# File lib/draper/collection_decorator.rb, line 73
def kind_of?(klass)
  decorated_collection.kind_of?(klass) || super
end
Also aliased as: is_a?
replace(other) click to toggle source
# File lib/draper/collection_decorator.rb, line 78
def replace(other)
  decorated_collection.replace(other)
  self
end
to_s() click to toggle source
# File lib/draper/collection_decorator.rb, line 57
def to_s
  "#<#{self.class.name} of #{decorator_class || "inferred decorators"} for #{object.inspect}>"
end

Protected Instance Methods

decorate_item(item) click to toggle source

Decorates the given item.

# File lib/draper/collection_decorator.rb, line 86
def decorate_item(item)
  item_decorator.call(item, context: context)
end

Private Instance Methods

item_decorator() click to toggle source
# File lib/draper/collection_decorator.rb, line 92
def item_decorator
  if decorator_class
    decorator_class.method(:decorate)
  else
    ->(item, options) { item.decorate(options) }
  end
end