Class: ActiveAdmin::CollectionDecorator

Inherits:
Object
  • Object
show all
Defined in:
lib/active_admin/collection_decorator.rb

Overview

This class decorates a collection of objects delegating mehods to behave like an Array. It's used to decouple ActiveAdmin from Draper and thus being able to use PORO decorators as well.

It's implementation is heavily based on the Draper::CollectionDecorator github.com/drapergem/draper/blob/aaa06bd2f1e219838b241a5534e7ca513edd1fe2/lib/draper/collection_decorator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, with:) ⇒ CollectionDecorator

Returns a new instance of CollectionDecorator.


18
19
20
21
# File 'lib/active_admin/collection_decorator.rb', line 18

def initialize(object, with:)
  @object = object
  @decorator_class = with
end

Instance Attribute Details

#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/active_admin/collection_decorator.rb', line 13

def decorator_class
  @decorator_class
end

#objectObject (readonly)

Returns the collection being decorated.

Returns:

  • the collection being decorated.


10
11
12
# File 'lib/active_admin/collection_decorator.rb', line 10

def object
  @object
end

Instance Method Details

#decorated_collectionObject


27
28
29
# File 'lib/active_admin/collection_decorator.rb', line 27

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