class StrongPresenter::CollectionPresenter

Attributes

object[R]

@return the collection being presented.

Public Class Methods

new(object, options = {}) { |self| ... } click to toggle source

@param [Enumerable] object

collection to present
# 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

Protected Class Methods

presents_with(presenter) click to toggle source

Sets the presenter used to wrap models in the collection

# File lib/strong_presenter/collection_presenter.rb, line 67
def presents_with presenter
  @presenter_class = presenter
  self
end
set_item_presenter_collection() click to toggle source
# File lib/strong_presenter/collection_presenter.rb, line 73
def set_item_presenter_collection
  collection = self
  presenter_class.instance_exec do
    unless nil?
      if !const_defined? :Collection
        const_set :Collection, collection
      elsif self::Collection.name.demodulize == "Collection"
        remove_const :Collection
        const_set :Collection, collection
      end
    end
  end
rescue NameError => error
end

Private Class Methods

inferred_presenter_class() click to toggle source
# File lib/strong_presenter/collection_presenter.rb, line 94
def inferred_presenter_class
  presenter_class = Inferrer.new(name).chomp("Presenter").inferred_class { |name| "#{name.singularize}Presenter" }
  presenter_class == self ? nil : presenter_class
end
inherited(subclass) click to toggle source
Calls superclass method
# File lib/strong_presenter/collection_presenter.rb, line 89
def inherited(subclass)
  subclass.set_item_presenter_collection
  super
end
presenter_class() click to toggle source
# File lib/strong_presenter/collection_presenter.rb, line 99
def presenter_class
  @presenter_class ||= inferred_presenter_class
end

Public Instance Methods

permit!(*attribute_paths) click to toggle source

Permits given attributes, with propagation to collection items.

Calls superclass method
# 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!() click to toggle source

Resets item presenters - clears the cache

# File lib/strong_presenter/collection_presenter.rb, line 33
def reload!
  @collection = nil
  self
end
to_s() click to toggle source
# File lib/strong_presenter/collection_presenter.rb, line 21
def to_s
  "#<#{self.class.name} of #{presenter_class || "inferred presenters"} for #{object.inspect}>"
end

Protected Instance Methods

collection() click to toggle source

@return [Array] the items being presented

# File lib/strong_presenter/collection_presenter.rb, line 61
def collection
  @collection ||= object.map{|item| wrap_item(item)}
end
item_presenter(item) click to toggle source
# File lib/strong_presenter/collection_presenter.rb, line 45
def item_presenter(item)
  return presenter_class if presenter_class
  StrongPresenter::Presenter.inferred_presenter(item)
end
presenter_class() click to toggle source

@return [Class] the presenter class used to present each item, as set by

{#initialize}.
# File lib/strong_presenter/collection_presenter.rb, line 41
def presenter_class
  @presenter_class = @presenter_class.nil? ? self.class.send(:presenter_class) : @presenter_class
end
wrap_item(item) click to toggle source

Wraps the given item.

# File lib/strong_presenter/collection_presenter.rb, line 54
def wrap_item(item)
  item_presenter(item).new(item).tap do |presenter|
    presenter.link_permissions self # item's permitted_attributes is linked to collection
  end
end