When Rails presenters are a smell and what to do about it

34
When Rails presenters are a smell and what to do about it Andrzej Krzywda @andrzejkrzywda

Transcript of When Rails presenters are a smell and what to do about it

Page 1: When Rails presenters are a smell and what to do about it

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

helpers presenters

???

Page 3: When Rails presenters are a smell and what to do about it
Page 4: When Rails presenters are a smell and what to do about it
Page 5: When Rails presenters are a smell and what to do about it

Presenters

Page 6: When Rails presenters are a smell and what to do about it
Page 7: When Rails presenters are a smell and what to do about it

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
Page 9: When Rails presenters are a smell and what to do about it

Remarks

• includes url_helpers

Page 10: When Rails presenters are a smell and what to do about it

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

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

Are there any alternatives to presenters?

Page 13: When Rails presenters are a smell and what to do about it

Read models

CQRS DDD

Page 14: When Rails presenters are a smell and what to do about it

Example

Page 15: When Rails presenters are a smell and what to do about it
Page 16: When Rails presenters are a smell and what to do about it

We need events(or maybe not)

Page 17: When Rails presenters are a smell and what to do about it

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
Page 19: When Rails presenters are a smell and what to do about it
Page 20: When Rails presenters are a smell and what to do about it
Page 21: When Rails presenters are a smell and what to do about it

What to do with the past events?

Page 22: When Rails presenters are a smell and what to do about it

1. RankingHadState 2. turn existing data into events

Page 23: When Rails presenters are a smell and what to do about it
Page 24: When Rails presenters are a smell and what to do about it
Page 25: When Rails presenters are a smell and what to do about it
Page 26: When Rails presenters are a smell and what to do about it
Page 27: When Rails presenters are a smell and what to do about it

What if I don’t want events?

Page 28: When Rails presenters are a smell and what to do about it

Enjoy the coupling and call the read model

directly

Page 29: When Rails presenters are a smell and what to do about it

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

When to choose read models over presenters?

Page 31: When Rails presenters are a smell and what to do about it

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

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

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

Thanks!