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.



26
27
28
29
30
31
# File 'lib/draper/collection_decorator.rb', line 26

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.



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

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.



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

def decorator_class
  @decorator_class
end

Instance Method Details

#decorated?true

Returns:

  • (true)


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

def decorated?
  true
end

#decorated_collectionArray

Returns the decorated items.

Returns:

  • (Array)

    the decorated items.



38
39
40
# File 'lib/draper/collection_decorator.rb', line 38

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)



45
46
47
48
49
50
51
52
# File 'lib/draper/collection_decorator.rb', line 45

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)


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

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

#replace(other) ⇒ Object



75
76
77
78
# File 'lib/draper/collection_decorator.rb', line 75

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

#to_sObject



54
55
56
# File 'lib/draper/collection_decorator.rb', line 54

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