Class: Draper::CollectionDecorator

Inherits:
Object
  • Object
show all
Extended by:
Delegation
Includes:
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 ViewHelpers

#helpers, #localize

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.



29
30
31
32
33
34
# 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

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.



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

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.



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

def decorator_class
  @decorator_class
end

#objectObject (readonly)

Returns the collection being decorated.

Returns:

  • the collection being decorated.



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

def object
  @object
end

Instance Method Details

#decorated?true

Returns:

  • (true)


67
68
69
# File 'lib/draper/collection_decorator.rb', line 67

def decorated?
  true
end

#decorated_collectionArray

Returns the decorated items.

Returns:

  • (Array)

    the decorated items.



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

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

#find(*args, &block) ⇒ Object

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



48
49
50
51
52
53
54
55
# 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

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

Returns:

  • (Boolean)


73
74
75
# File 'lib/draper/collection_decorator.rb', line 73

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

#replace(other) ⇒ Object



78
79
80
81
# File 'lib/draper/collection_decorator.rb', line 78

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

#to_sObject



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

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