Class: StrongPresenter::CollectionPresenter

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Permissible

#permit_all!, #select_permitted

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



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

def presents_with presenter
  @presenter_class = presenter
  self
end

Instance Method Details

#permit!(*attribute_paths) ⇒ Object

Permits given attributes, with propagation to collection items.

Parameters:

  • attribute_paths ([Symbols*]*)

    the attributes to permit. An array of symbols represents an attribute path.



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

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



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

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