Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a DelegatePresenter base class #156

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

dasch
Copy link
Contributor

@dasch dasch commented Aug 28, 2015

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 dasch force-pushed the dasch/delegate-presenter branch 3 times, most recently from a5d8bad to ca616d8 Compare August 28, 2015 09:06
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.
@teliosdev
Copy link
Contributor

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

@dasch
Copy link
Contributor Author

dasch commented Sep 1, 2015

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

@hauleth
Copy link
Contributor

hauleth commented Nov 16, 2015

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.

@dasch
Copy link
Contributor Author

dasch commented Nov 16, 2015

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

@hauleth
Copy link
Contributor

hauleth commented Nov 16, 2015

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants