Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.

Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.

Like this presentation? Why not share!

1,407 views

1,239 views

1,239 views

Published on

Published in:
Technology

License: CC Attribution License

No Downloads

Total views

1,407

On SlideShare

0

From Embeds

0

Number of Embeds

12

Shares

0

Downloads

7

Comments

0

Likes

2

No embeds

No notes for slide

- 1. Dealing with Obese Models<br />A 5 Step Guide to Getting your models in shape<br />Ryan Brunner, Influitive<br />@ryanbrunner<br />
- 2. The Problem<br />My Models scare me. They’re too big and hard to deal with.<br />Usually, when I run into a structural problem with Rails, there’s one place I can turn to.<br />
- 3. CONVENTIONS<br />
- 4. Conventions<br />
- 5. Your folders should look like this.<br />
- 6. Your views should look like this.<br />
- 7. Your controllers should look like this.<br />
- 8. Your stylesheetsshould look like this.<br />
- 9. Your javascriptshould apparently look like this.<br />
- 10. So why is this ok for your models?Is there a Rails convention to deal with this?<br />
- 11. SKINNY CONTROLLERS<br />FAT MODELS<br />
- 12. Maybe fat models aren’t the solution…<br />
- 13. SKINNY CONTROLLERS<br />FAT MODELS<br />REASONABLY HEALTHY MODELS<br />
- 14. EXAMPLE TIME!<br />Let’s build a person model!<br />
- 15. Person<br />Attributes<br />
- 16. Person<br />Attributes<br />Associations<br />
- 17. Person<br />Attributes<br />Associations<br />Validation<br />
- 18. Attributes<br />Associations<br />Validation<br />Scopes for searching<br />Person<br />
- 19. Attributes<br />Associations<br />Validation<br />Scopes for searching<br />Scopes for authorization<br />Person<br />
- 20. Attributes<br />Associations<br />Validation<br />Scopes for searching<br />Scopes for authorization<br />State Machine<br />Person<br />
- 21. Attributes<br />Associations<br />Validation<br />Scopes for searching<br />Scopes for authorization<br />State Machine<br />Location / GeoTagging Stuff<br />Person<br />
- 22. Attributes<br />Associations<br />Validation<br />Scopes for searching<br />Scopes for authorization<br />State Machine<br />Location / GeoTagging Stuff<br />Leaderboard<br />Person<br />
- 23. Attributes<br />Associations<br />Validation<br />Scopes for searching<br />Scopes for authorization<br />State Machine<br />Location / GeoTagging Stuff<br />Leaderboard<br />Relationship helper methods<br />Person<br />
- 24. Attributes<br />Associations<br />Validation<br />Scopes for searching<br />Scopes for authorization<br />State Machine<br />Location / GeoTagging Stuff<br />Leaderboard<br />Relationship helper methods<br />Formatting methods<br />Person<br />
- 25. WHAT HAPPENED?<br />
- 26. OBESITY<br />It’s not just for Texas anymore.<br />
- 27. Let’s get our model back in shape!<br />
- 28. Step 1:Organization<br />
- 29. Big, Beautiful Models should be organized.<br />
- 30. Big, Beautiful Models should be organized.<br />Whitespace is your friend.<br />
- 31. Big, Beautiful Models should be organized.<br />Whitespace is your friend.<br />Don’t be afraid of comments.<br />
- 32. Big, Beautiful Models should be organized.<br />Whitespace is your friend.<br />Don’t be afraid of comments.<br />Play around with indenting.<br />
- 33. Step 2:Relocating<br />
- 34. Look for code that’s Too interested in another model.<br />person.rb<br />
- 35. Look for code that’s too interested in another model.<br />person.rb<br />person.rb<br />It might make more sense to move it there.<br />feedback.rb<br />
- 36. Look for code that’s too interested in another model.<br />person.rb<br />person.rb<br />It might make more sense to move it there.<br />feedback.rb<br />Fatness isn’t just about LOC.<br />
- 37. Step 3:Abstracting common behaviour<br />
- 38. Sometimes things aren’t central to your model.<br />
- 39. Sometimes things aren’t central to your model.<br />Build your own framework.<br />
- 40. Sometimes things aren’t central to your model.<br />Build your own framework.<br />Even if you only do it once.<br />
- 41. Step 4:PRESENTATION LOGIC<br />
- 42. Watch for Presentation Logic in your Models<br />Consider HTML Harmful.<br />Models shouldn’t format data.<br />If you’re returning something other than models, be careful..<br />
- 43. The Traditional Rails Solution - Helpers<br />
- 44. But..Helpers suck when you depend on state.<br />
- 45. Decorators<br />Like Helpers, but don’t suck.<br />
- 46. Delegates methods to the subjectof the decorator.<br />Provides additional methods for presentation logic.<br />Use them like you’d use the model, but feel free to add presentation logic. <br />Anatomy of a decorator<br />
- 47. Decorators are a way to extend your models to Add Presentation Logic<br />
- 48. All the delegation work is done for you.<br />https://github.com/jcasimir/rails_decorators<br />
- 49. Step 5:Presenters<br />
- 50. EXAMPLE TIME!<br />Relationship Viewer<br />
- 51. We needed this interface.<br />It needs to interact with Filtered relationships in a lot of different ways<br />
- 52. All we need is methods for:<br /><ul><li>Whether the current user is connected.
- 53. Counts of all connections filtered by customer type.
- 54. Lists of relationships by relationship type.
- 55. Whether the user has submitted feedback.
- 56. How many users are currently in the system.
- 57. Content linked to those users.</li></li></ul><li>Phew!<br />That there’s a lot of stuff.<br />
- 58. Attempt #1<br />Let’s fatten up some models.<br />
- 59. relationship.rb<br />
- 60. relationships_controller.rb<br />
- 61. Let’s add a Presenter!<br />
- 62. prospect_match_presenter.rb<br />
- 63. prospect_match_controller.rb<br />
- 64. CLEAN, Wonderful views<br />
- 65. Why Use presenters?<br /><ul><li>Your models are concerned only with what they do.
- 66. Your controllers and views talk to a well-defined interface.</li></li></ul><li>When to use a presenter<br /><ul><li>Your view talks about more than one model (in a poorly related way)
- 67. You have lots of instance variables on your controller action.
- 68. Your model is too clever about things it isn’t related to.</li></li></ul><li>READY FOR BEACH SEASON<br />
- 69. Summary<br />Sweat the easy stuff first.<br />Organization goes a long way.<br />Limit how much your models know about other things.<br />Consider presenters or decorators for complicated scenarios.<br />

No public clipboards found for this slide

×
### Save the most important slides with Clipping

Clipping is a handy way to collect and organize the most important slides from a presentation. You can keep your great finds in clipboards organized around topics.

Be the first to comment