Class: StrongPresenter::CollectionPresenter

Inherits:
Object
  • Object
show all
Includes:
Enumerable, StrongAttributes::Permissible, Delegation, ViewHelpers
Defined in:
lib/strong_presenter/collection_presenter.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ViewHelpers

#helpers, #localize

Constructor Details

#initialize(object, options = {}) {|_self| ... } ⇒ CollectionPresenter

Returns a new instance of CollectionPresenter.

Parameters:

  • object (Enumerable)

    collection to present

Yields:

  • (_self)

Yield Parameters:



13
14
15
16
17
18
19
# File 'lib/strong_presenter/collection_presenter.rb', line 13

def initialize(object, options = {})
  options.assert_valid_keys(:with)
  @object = object
  @presenter_class = options[:with]

  yield self if block_given?
end

Class Method Details

.presents_with(presenter) ⇒ Object

Sets the presenter used to wrap models in the collection



67
68
69
70
# File 'lib/strong_presenter/collection_presenter.rb', line 67

def presents_with presenter
  @presenter_class = presenter
  self
end

Instance Method Details

#permit!(*attribute_paths) ⇒ Object

Permits given attributes, with propagation to collection items.



26
27
28
29
30
# File 'lib/strong_presenter/collection_presenter.rb', line 26

def permit! *attribute_paths
  super
  @collection.each { |presenter| presenter.permit! *attribute_paths } unless @collection.nil?
  self
end

#reload!Object

Resets item presenters - clears the cache



33
34
35
36
# File 'lib/strong_presenter/collection_presenter.rb', line 33

def reload!
  @collection = nil
  self
end

#to_sObject



21
22
23
# File 'lib/strong_presenter/collection_presenter.rb', line 21

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