Class: Draper::CollectionDecorator
- Inherits:
-
Object
- Object
- Draper::CollectionDecorator
- Extended by:
- Delegation
- Includes:
- ViewHelpers, Enumerable
- Defined in:
- lib/draper/collection_decorator.rb
Instance Attribute Summary collapse
-
#context ⇒ Hash
Extra data to be used in user-defined methods, and passed to each item's decorator.
-
#decorator_class ⇒ Class
readonly
The decorator class used to decorate each item, as set by #initialize.
-
#object ⇒ Object
readonly
The collection being decorated.
Instance Method Summary collapse
- #decorated? ⇒ true
-
#decorated_collection ⇒ Array
The decorated items.
-
#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). -
#initialize(object, options = {}) ⇒ CollectionDecorator
constructor
A new instance of CollectionDecorator.
- #kind_of?(klass) ⇒ Boolean (also: #is_a?)
- #replace(other) ⇒ Object
- #to_s ⇒ Object
Methods included from Delegation
Methods included from ViewHelpers
Constructor Details
#initialize(object, options = {}) ⇒ CollectionDecorator
Returns a new instance of CollectionDecorator.
29 30 31 32 33 34 |
# File 'lib/draper/collection_decorator.rb', line 29 def initialize(object, = {}) .assert_valid_keys(:with, :context) @object = object @decorator_class = [:with] @context = .fetch(:context, {}) end |
Instance Attribute Details
#context ⇒ Hash
Returns 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_class ⇒ Class (readonly)
Returns 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 |
#object ⇒ Object (readonly)
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
67 68 69 |
# File 'lib/draper/collection_decorator.rb', line 67 def decorated? true end |
#decorated_collection ⇒ Array
Returns 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?
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_s ⇒ Object
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 |