Class: Draper::CollectionDecorator

Inherits:
Object
  • Object
show all
Extended by:
Delegation
Includes:
QueryMethods, ViewHelpers, Enumerable
Defined in:
lib/draper/collection_decorator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Delegation

delegate

Methods included from QueryMethods

#respond_to_missing?

Methods included from ViewHelpers

#helpers

Constructor Details

#initialize(object, options = {}) ⇒ CollectionDecorator

Returns a new instance of CollectionDecorator.

Parameters:

  • object (Enumerable)

    collection to decorate.

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :with (Class, nil) — default: nil

    the decorator class used to decorate each item. When nil, each item's decorate method will be used.

  • :context (Hash) — default: {}

    extra data to be stored in the collection decorator and used in user-defined methods, and passed to each item's decorator.



30
31
32
33
34
35
# File 'lib/draper/collection_decorator.rb', line 30

def initialize(object, options = {})
  options.assert_valid_keys(:with, :context)
  @object = object
  @decorator_class = options[:with]
  @context = options.fetch(:context, {})
end

Instance Attribute Details

#contextHash

Returns extra data to be used in user-defined methods, and passed to each item's decorator.

Returns:

  • (Hash)

    extra data to be used in user-defined methods, and passed to each item's decorator.



17
18
19
# File 'lib/draper/collection_decorator.rb', line 17

def context
  @context
end

#decorator_classClass (readonly)

Returns the decorator class used to decorate each item, as set by #initialize.

Returns:

  • (Class)

    the decorator class used to decorate each item, as set by #initialize.



13
14
15
# File 'lib/draper/collection_decorator.rb', line 13

def decorator_class
  @decorator_class
end

#objectObject (readonly)

Returns the collection being decorated.

Returns:

  • the collection being decorated.



9
10
11
# File 'lib/draper/collection_decorator.rb', line 9

def object
  @object
end

Instance Method Details

#decorated?true

Returns:

  • (true)


58
59
60
# File 'lib/draper/collection_decorator.rb', line 58

def decorated?
  true
end

#decorated_collectionArray

Returns the decorated items.

Returns:

  • (Array)

    the decorated items.



42
43
44
# File 'lib/draper/collection_decorator.rb', line 42

def decorated_collection
  @decorated_collection ||= object.map{|item| decorate_item(item)}
end

#kind_of?(klass) ⇒ Boolean Also known as: is_a?

Returns:

  • (Boolean)


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

def kind_of?(klass)
  decorated_collection.kind_of?(klass) || super
end

#replace(other) ⇒ Object



70
71
72
73
# File 'lib/draper/collection_decorator.rb', line 70

def replace(other)
  decorated_collection.replace(other)
  self
end

#to_sObject



48
49
50
# File 'lib/draper/collection_decorator.rb', line 48

def to_s
  "#<#{self.class.name} of #{decorator_class || "inferred decorators"} for #{object.inspect}>"
end