About curly design patterns (question)
yurifrl opened this issue · 2 comments
So, curly is base for my application, and since I've been spending a lot of time with it, i raised a question:
Could we have a presenter for a object? why? what is probably a good idea:
#product_controller.rb
class ProductController < Curly::Presenter
def show
@product = Product.find(params[:id])
end
end
#common_presenter.rb
class CommonPresenter < Curly::Presenter
presents :product, default: nil
# i could have access to the product methods in here, no need in rewriting
def products
Product.all
end
end
#show_presenter.rb
class ShowPresenter < CommonPresenter
presents :product, default: nil
# i could have access to the product methods in here to, no need in rewriting
end
#product.rb
class Product < Curly::Object
presents :product, default: nil
# here i can write all assessors to the product object
# this class would be shared by collections and presenters
#
# product has a image, would be extra nice if i could have a image class, with all the image assessor methods
def setup!
@image = @product.image # setup doesn't work when presenter has no view i guess
end
def name
@product.name
end
def qtd
@product.total_in_hand
end
end
this seems a nice way of doing things, mainly because of the reuse, and seems more object oritendish
the problem I've been facing is the repetition, i have a image presenter and i have identical methods in the product presenter
to render the product image.
does that make sense ? \o/
hope this is not a very specific and there is more people that would like something like it.
We've already got a nice way of presenting objects – partials!
# app/presenters/product_presenter.rb
#
# You need a template in app/views/_product.html.curly
class ProductPresenter < Curly::Presenter
presents :product
def name
@product.name
end
def image
image_tag(@product.image_url, alt: @product.name)
end
end
# app/presenters/products/show_presenter.rb
class Products::ShowPresenter < Curly::Presenter
presents :product
def product_info
# this will automatically render the partial.
render @product
end
end
class Homepage::IndexPresenter < Curly::Presenter
# this will use the ProductPresenter - simply inline the template.
def products
Product.all
end
end
Are there cases where you want multiple templates to reuse the same presenter? That could probably be accomplished with some load path magic.
Closing this unless there are more reasons for Curly to add additional stuff.