Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Rails Presenters


Published on

A quick run down on the presenter pattern in rails, code for the talk is here

Published in: Software
  • Be the first to comment

  • Be the first to like this

Rails Presenters

  1. 1. PRESENTERS with a guest appearance from decorators
  2. 2. HI • • @tjridge •
  4. 4. VIEW RELATED LOGIC • welp
  6. 6. HELPERS AREN’T AWESOME • at least for this task • they share a global namespace • aren’t a very oo way of solving the problem • great for shared logic that doesn't relate to the object. Use sparingly
  7. 7. I KNOW MODELS! • also nope • All these if conditions relate to the display of data • Don’t overload your model with that level of responsibility
  8. 8. ENTER PRESENTERS • Presenters are a simple class with knowledge of the model and the view. • Not a stretch to call them a ViewModel
  9. 9. PURPOSE • Presenters help to achieve adherence to SRP • A Presenter’s purpose is a decorator who has the job of massaging the decorated object into a ui/ view friendly manner.
  10. 10. DECORATOR? • Taking one object and adding, replacing or extending its behaviour, whilst allowing access to the underlying objects methods.
  11. 11. PRESENTERS VS DECORATORS • A decorator isn’t always a presenter, but a presenter is always likely to be a decorator.
  12. 12. END RESULT • This is more in line with a decorator approach • We just delegate to the underlying object
  13. 13. MULTIPLE OBJECTS? • ensure you delegate methods to the underlying objects
  14. 14. MULTIPLE RECORDS? • Just create a wrap method to instantiate your presenter/decorator across multiple records.
  15. 15. OTHER OPTIONS? • Draper • Great for when things start getting more complex
  16. 16. OTHER VALUE POINTS • Easy-ish to test • less conditionals in your views • happier designers/front end developers
  17. 17. CONS • More objects == harder to learn the codebase • Don’t introduce them until you need to.
  18. 18. GREAT RESOURCES • Railscasts - draper and presenters from scratch (pro) • skinny-everything • delegators-rails/
  19. 19. QUESTIONS?