when rails presenters are a smell and what to do about it
TRANSCRIPT
![Page 1: When Rails presenters are a smell and what to do about it](https://reader031.vdocuments.mx/reader031/viewer/2022030309/58f306cf1a28ab11418b45a3/html5/thumbnails/1.jpg)
When Rails presenters are a smell and what to
do about itAndrzej Krzywda @andrzejkrzywda
![Page 2: When Rails presenters are a smell and what to do about it](https://reader031.vdocuments.mx/reader031/viewer/2022030309/58f306cf1a28ab11418b45a3/html5/thumbnails/2.jpg)
helpers presenters
???
![Page 3: When Rails presenters are a smell and what to do about it](https://reader031.vdocuments.mx/reader031/viewer/2022030309/58f306cf1a28ab11418b45a3/html5/thumbnails/3.jpg)
![Page 4: When Rails presenters are a smell and what to do about it](https://reader031.vdocuments.mx/reader031/viewer/2022030309/58f306cf1a28ab11418b45a3/html5/thumbnails/4.jpg)
![Page 5: When Rails presenters are a smell and what to do about it](https://reader031.vdocuments.mx/reader031/viewer/2022030309/58f306cf1a28ab11418b45a3/html5/thumbnails/5.jpg)
Presenters
![Page 6: When Rails presenters are a smell and what to do about it](https://reader031.vdocuments.mx/reader031/viewer/2022030309/58f306cf1a28ab11418b45a3/html5/thumbnails/6.jpg)
![Page 7: When Rails presenters are a smell and what to do about it](https://reader031.vdocuments.mx/reader031/viewer/2022030309/58f306cf1a28ab11418b45a3/html5/thumbnails/7.jpg)
Remarks
• takes multiple AR objects
• uses the exposed AR state
• returns a hash
• some repetition solved by meta
![Page 8: When Rails presenters are a smell and what to do about it](https://reader031.vdocuments.mx/reader031/viewer/2022030309/58f306cf1a28ab11418b45a3/html5/thumbnails/8.jpg)
![Page 9: When Rails presenters are a smell and what to do about it](https://reader031.vdocuments.mx/reader031/viewer/2022030309/58f306cf1a28ab11418b45a3/html5/thumbnails/9.jpg)
Remarks
• includes url_helpers
![Page 10: When Rails presenters are a smell and what to do about it](https://reader031.vdocuments.mx/reader031/viewer/2022030309/58f306cf1a28ab11418b45a3/html5/thumbnails/10.jpg)
What’s good?
• no need for helpers
• view logic in one place
• better OOP than helpers
• handles non-CRUD views quite well
![Page 11: When Rails presenters are a smell and what to do about it](https://reader031.vdocuments.mx/reader031/viewer/2022030309/58f306cf1a28ab11418b45a3/html5/thumbnails/11.jpg)
What’s bad?• ActiveRecord all the way down
• access attributes
• even when we pass domain POROs
• tell, don’t ask
• coupling to AR or POROs
• potentially slow
![Page 12: When Rails presenters are a smell and what to do about it](https://reader031.vdocuments.mx/reader031/viewer/2022030309/58f306cf1a28ab11418b45a3/html5/thumbnails/12.jpg)
Are there any alternatives to presenters?
![Page 13: When Rails presenters are a smell and what to do about it](https://reader031.vdocuments.mx/reader031/viewer/2022030309/58f306cf1a28ab11418b45a3/html5/thumbnails/13.jpg)
Read models
CQRS DDD
![Page 14: When Rails presenters are a smell and what to do about it](https://reader031.vdocuments.mx/reader031/viewer/2022030309/58f306cf1a28ab11418b45a3/html5/thumbnails/14.jpg)
Example
![Page 15: When Rails presenters are a smell and what to do about it](https://reader031.vdocuments.mx/reader031/viewer/2022030309/58f306cf1a28ab11418b45a3/html5/thumbnails/15.jpg)
![Page 16: When Rails presenters are a smell and what to do about it](https://reader031.vdocuments.mx/reader031/viewer/2022030309/58f306cf1a28ab11418b45a3/html5/thumbnails/16.jpg)
We need events(or maybe not)
![Page 17: When Rails presenters are a smell and what to do about it](https://reader031.vdocuments.mx/reader031/viewer/2022030309/58f306cf1a28ab11418b45a3/html5/thumbnails/17.jpg)
rails_event_store
The fastest way to get events in your ugly Rails apps
https://github.com/arkency/rails_event_store
![Page 18: When Rails presenters are a smell and what to do about it](https://reader031.vdocuments.mx/reader031/viewer/2022030309/58f306cf1a28ab11418b45a3/html5/thumbnails/18.jpg)
![Page 19: When Rails presenters are a smell and what to do about it](https://reader031.vdocuments.mx/reader031/viewer/2022030309/58f306cf1a28ab11418b45a3/html5/thumbnails/19.jpg)
![Page 20: When Rails presenters are a smell and what to do about it](https://reader031.vdocuments.mx/reader031/viewer/2022030309/58f306cf1a28ab11418b45a3/html5/thumbnails/20.jpg)
![Page 21: When Rails presenters are a smell and what to do about it](https://reader031.vdocuments.mx/reader031/viewer/2022030309/58f306cf1a28ab11418b45a3/html5/thumbnails/21.jpg)
What to do with the past events?
![Page 22: When Rails presenters are a smell and what to do about it](https://reader031.vdocuments.mx/reader031/viewer/2022030309/58f306cf1a28ab11418b45a3/html5/thumbnails/22.jpg)
1. RankingHadState 2. turn existing data into events
![Page 23: When Rails presenters are a smell and what to do about it](https://reader031.vdocuments.mx/reader031/viewer/2022030309/58f306cf1a28ab11418b45a3/html5/thumbnails/23.jpg)
![Page 24: When Rails presenters are a smell and what to do about it](https://reader031.vdocuments.mx/reader031/viewer/2022030309/58f306cf1a28ab11418b45a3/html5/thumbnails/24.jpg)
![Page 25: When Rails presenters are a smell and what to do about it](https://reader031.vdocuments.mx/reader031/viewer/2022030309/58f306cf1a28ab11418b45a3/html5/thumbnails/25.jpg)
![Page 26: When Rails presenters are a smell and what to do about it](https://reader031.vdocuments.mx/reader031/viewer/2022030309/58f306cf1a28ab11418b45a3/html5/thumbnails/26.jpg)
![Page 27: When Rails presenters are a smell and what to do about it](https://reader031.vdocuments.mx/reader031/viewer/2022030309/58f306cf1a28ab11418b45a3/html5/thumbnails/27.jpg)
What if I don’t want events?
![Page 28: When Rails presenters are a smell and what to do about it](https://reader031.vdocuments.mx/reader031/viewer/2022030309/58f306cf1a28ab11418b45a3/html5/thumbnails/28.jpg)
Enjoy the coupling and call the read model
directly
![Page 29: When Rails presenters are a smell and what to do about it](https://reader031.vdocuments.mx/reader031/viewer/2022030309/58f306cf1a28ab11418b45a3/html5/thumbnails/29.jpg)
Read models• Event handlers
• Customized for the view
• denormalized / derived data
• easy to rebuild
• cache-like
• tell, don’t ask
![Page 30: When Rails presenters are a smell and what to do about it](https://reader031.vdocuments.mx/reader031/viewer/2022030309/58f306cf1a28ab11418b45a3/html5/thumbnails/30.jpg)
When to choose read models over presenters?
![Page 31: When Rails presenters are a smell and what to do about it](https://reader031.vdocuments.mx/reader031/viewer/2022030309/58f306cf1a28ab11418b45a3/html5/thumbnails/31.jpg)
Choose presenters
• When your app is still very CRUDish
• what you create is what you list
• No goal to escape AR
• No performance problems
![Page 32: When Rails presenters are a smell and what to do about it](https://reader031.vdocuments.mx/reader031/viewer/2022030309/58f306cf1a28ab11418b45a3/html5/thumbnails/32.jpg)
Choose read models
• Performance
• When you prefer domain objects not to leak attributes
• show data from different modules
• gateway drug to DDD/CQRS
![Page 33: When Rails presenters are a smell and what to do about it](https://reader031.vdocuments.mx/reader031/viewer/2022030309/58f306cf1a28ab11418b45a3/html5/thumbnails/33.jpg)
Read it online
http://blog.arkency.com/2015/05/introducing-read-models-in-your-legacy-application/
![Page 34: When Rails presenters are a smell and what to do about it](https://reader031.vdocuments.mx/reader031/viewer/2022030309/58f306cf1a28ab11418b45a3/html5/thumbnails/34.jpg)
Thanks!