rupicon 2014 useful design patterns in rails

24
Design Patterns in Rails A kweek case study

Upload: rupicon

Post on 10-Apr-2017

252 views

Category:

Technology


1 download

TRANSCRIPT

Design Patterns in RailsA kweek case study

1. Introduction to the idea“Meet the parents”

Old school

Old School- code smell- design smell

2. Challenge“Meet the Fockers”

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

3. Where to start???

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

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

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

Facade

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

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…?

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

Validations on steroids

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.

Presenter

Presenter

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

Preview event

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

- To preview, you would need defaults

4. Conclusions“Little Fockers”

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?"

QUESTIONS?!