Question

I use the excellent Inherited Resources gem quite a bit in my Rails apps, but whenever I want to use a presenter I fall back to writing controllers by hand.

Is there a good, clean solution for combining Inherited Resources with something like Draper or delegate_presenter?

Was it helpful?

Solution

Author of delegate_presenter here.

delegate_presenter should work just fine with inherited_resources. Now I haven't actually used inherited_resources on a project, so a lot of this is just from the readme.

What I always to do create the presenter object in the view anyway: todo_present = Present(@todo), for example.

If you are having templates from a common source (like LegalTodo is a subclass of Todo, for some bog-unknown reason), and you share templates, you could do this in your template:

presenter = Present(resource)

resource - at least according to the inherited_resources readme - is the helper for the current resource your looking at (so, @todo, or @legal_todo)

Present() will look at the class of the object and instantiate the appropriate presenter object. TodoPresenter if resource returns a Todo object, LegalTodoPresenter if resource returns a LegalTodo object.

Then, assuming those presenters are polymorphic, the presenters will let you abstract the differences away ("The name of a LegalTodo be the description + deposition number, but the name of the Todo item should just be description") - even if you share views!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top