Module: StrongPresenter::Associable::ClassMethods

Defined in:
lib/strong_presenter/associable.rb

Instance Method Summary collapse

Instance Method Details

#presents_association(association, options = {}) ⇒ Object

Automatically wraps multiple associations. @param [Symbol] association name of the association to wrap. @option options [Class] :with the presenter to apply to the association. @option options [Symbol] :scope a scope to apply when fetching the association. @yield block executed when association presenter is initialized, in the context of the parent presenter instance (instance_exec-ed) @yieldparam [Presenter] the association presenter @return [void]



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/strong_presenter/associable.rb', line 21

def presents_association(association, options = {})
  options.assert_valid_keys(:with, :scope)
  association = association.to_sym
  options[:with] = Associable.object_association_class(object_class, association) unless options.has_key? :with
  presenter_associations[association] ||= StrongPresenter::PresenterAssociation.new(association, options) do |presenter|
    presenter.link_permissions self, association
    yield presenter if block_given?
  end
  define_method(association) do
    presenter_associations[association] ||= self.class.send(:presenter_associations)[association].wrap(self)
  end
end

#presents_associations(*associations, options = {}) {|the| ... } ⇒ void

This method returns an undefined value.

Automatically wraps multiple associations.

Parameters:

  • associations (Symbols*)

    names of the associations to wrap.

Options Hash (options):

  • :with (Class)

    the presenter to apply to the association.

  • :scope (Symbol)

    a scope to apply when fetching the association.

Yields:

  • block executed when association presenter is initialized, in the context of the parent presenter instance (instance_exec-ed)

Yield Parameters:



47
48
49
50
51
# File 'lib/strong_presenter/associable.rb', line 47

def presents_associations(*associations)
  options = associations.extract_options!
  options.assert_valid_keys(:with, :scope)
  associations.each { |association| presents_association(association, options) {|presenter| yield if block_given?} }
end