rupicon2014 useful design patterns in rails

24
Design Patterns in Rails A kweek case study

Upload: bordei-cristian

Post on 22-Aug-2015

243 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Rupicon2014   useful design patterns in rails

Design Patterns in RailsA kweek case study

Page 2: Rupicon2014   useful design patterns in rails

1. Introduction to the idea“Meet the parents”

Page 3: Rupicon2014   useful design patterns in rails
Page 4: Rupicon2014   useful design patterns in rails

Old school

Page 5: Rupicon2014   useful design patterns in rails

Old School- code smell- design smell

Page 6: Rupicon2014   useful design patterns in rails

2. Challenge“Meet the Fockers”

Page 7: Rupicon2014   useful design patterns in rails

Challenge- new design is a one step form vs the 3 step

on the old design- save as draft implies no validations- preview will suffer from the new “draft”

feature- publish will invoke validations

Page 8: Rupicon2014   useful design patterns in rails

3. Where to start???

Page 9: Rupicon2014   useful design patterns in rails

SRP- Add Form - to update the model with data- Validation “Contract” - validate the model- Publish Facade- Presenter - to preview the event

Page 10: Rupicon2014   useful design patterns in rails

Add FormAvdi Grimm describes form objects as boarder guards. They validate and verify that all data coming into the system is correct.

Once the data moves past the checkpoint it is not checked again.

- a single place to handle input coercion- virtus model- validations

Page 11: Rupicon2014   useful design patterns in rails

Facade“The Facade Pattern is simply a formalization of the idea that if you cannot avoid complexity, then the next best thing to do is to isolate it, to keep it from contaminating your whole system” -- Russ Olsen

- simplifies access to the object being wrapped or the subsystem- opposite to the decorator- they are not meant to hide anything

Page 12: Rupicon2014   useful design patterns in rails

Facade

Page 13: Rupicon2014   useful design patterns in rails

Facade and Friends- adapters exist to hide the fact that we are stuck using an object

that has the wrong interface- a proxy stands between the user of an object and the object

itself and somehow controls access to the object- decorators add functionality to an object

Page 14: Rupicon2014   useful design patterns in rails

Validations on steroidsRemember the contract?

What if you want to validate certain contracts at a certain time?

What if you don’t want to validate a contract at a certain point?

- separate the contracts- group them together based on your needs- validate them…?

Page 15: Rupicon2014   useful design patterns in rails

Validations on steroidsWikipedia says : The composite pattern describes that a group of objects is to be treated in the same way as a single instance of an object

Page 16: Rupicon2014   useful design patterns in rails

Validations on steroids

Page 17: Rupicon2014   useful design patterns in rails

PresenterBordei Cristian

- is a way to keep your view and controller logic as clean as possible by having a Ruby class serve as the interface between your model and the view/controller.

Page 18: Rupicon2014   useful design patterns in rails

Presenter

Page 19: Rupicon2014   useful design patterns in rails

Presenter

Page 20: Rupicon2014   useful design patterns in rails

Presenter vs HelperWhy not Helper you say?

- have logic specific to your models that you need encapsulated

- something you can test

- something you can extend

Page 21: Rupicon2014   useful design patterns in rails

Preview event

- Saving something as a draft can be a b***h

- To preview, you would need defaults

Page 22: Rupicon2014   useful design patterns in rails

4. Conclusions“Little Fockers”

Page 23: Rupicon2014   useful design patterns in rails

Conclusions

- Facades are fun and easy but are not an excuse for adding features to a subsystem

- If you want to make several types of coffee with different coffee machines, use a composite

- Presenters make you think helpers are like a compliment that you don't want to give, but feel obligated to: "Yeah, I mean, well, you tried, and that's what counts, right?"

Page 24: Rupicon2014   useful design patterns in rails

QUESTIONS?!