curly icon indicating copy to clipboard operation
curly copied to clipboard

Add a DelegatePresenter base class

Open dasch opened this issue 9 years ago • 5 comments

Subclasses of this class can easily delegate presenter methods to a backing object. A parameter with the same name as the class (e.g. product for ProductPresenter) must be passed and its value will receive delegated calls. Only simple components are allowed, and methods must still be whitelisted in the presenter class.

Example
class ProductPresenter < Curly::DelegatePresenter
  # There's an implicit `presents :product`.

  # Methods must be whitelisted.
  delegates :name, :category

  # Additional methods can also be added.
  def price
    number_to_currency(@product.price)
  end
end

dasch avatar Aug 28 '15 08:08 dasch

I was more thinking of something like this:

class Articles::ShowPresenter < Curly::Presenter
  presents :article, :user
  context :user, delegate: [:id, :name]
end

Which is the same as:

class Articles::ShowPresenter < Curly::Presenter
  presents :article, :user

  def user
    yield @user
  end

  class UserPresenter < Curly::Presenter
    presents :user
    delegate :id, :name, to: :@user
  end
end

teliosdev avatar Aug 28 '15 19:08 teliosdev

That feels a bit too much like magic to me :-/

dasch avatar Sep 01 '15 08:09 dasch

I would rather see:

class ProductPresenter < Curly::DelegatePresenter
  presents product: [:name, :category]

  # Additional methods can also be added.
  def price
    number_to_currency(@product.price)
  end
end

As it explicitly mark where we delegate this methods.

hauleth avatar Nov 16 '15 20:11 hauleth

But then there's no way to extend the sub-context's presenter.

dasch avatar Nov 16 '15 21:11 dasch

Then add to: param which allow delegating to custom field and IMHO will be ok.

Łukasz Jan Niemier

Dnia 16 lis 2015 o godz. 22:18 Daniel Schierbeck [email protected] napisał(a):

But then there's no way to extend the sub-context's presenter.

— Reply to this email directly or view it on GitHub.

hauleth avatar Nov 16 '15 21:11 hauleth