curly
curly copied to clipboard
Add a DelegatePresenter base class
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
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
That feels a bit too much like magic to me :-/
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.
But then there's no way to extend the sub-context's presenter.
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.