Dealing with Obese Models
-
Upload
ryan-brunner -
Category
Technology
-
view
1.136 -
download
0
Transcript of Dealing with Obese Models
![Page 1: Dealing with Obese Models](https://reader034.fdocuments.us/reader034/viewer/2022042614/55a3505f1a28abf3478b4661/html5/thumbnails/1.jpg)
DEALING WITH
OBESE MODELS
A 5 Step Guide to Getting your models in shape
Ryan Brunner, Influitive
@ryanbrunner
![Page 2: Dealing with Obese Models](https://reader034.fdocuments.us/reader034/viewer/2022042614/55a3505f1a28abf3478b4661/html5/thumbnails/2.jpg)
THE
PROBLEM
My Models scare me. They’re too big and hard to deal with.
Usually, when I run into a structural problem with Rails, there’s one place I can turn to.
![Page 3: Dealing with Obese Models](https://reader034.fdocuments.us/reader034/viewer/2022042614/55a3505f1a28abf3478b4661/html5/thumbnails/3.jpg)
CONVENTIONS
![Page 4: Dealing with Obese Models](https://reader034.fdocuments.us/reader034/viewer/2022042614/55a3505f1a28abf3478b4661/html5/thumbnails/4.jpg)
CONVENTIONS
![Page 5: Dealing with Obese Models](https://reader034.fdocuments.us/reader034/viewer/2022042614/55a3505f1a28abf3478b4661/html5/thumbnails/5.jpg)
Your folders should look like this.
![Page 6: Dealing with Obese Models](https://reader034.fdocuments.us/reader034/viewer/2022042614/55a3505f1a28abf3478b4661/html5/thumbnails/6.jpg)
Your views should look like this.
![Page 7: Dealing with Obese Models](https://reader034.fdocuments.us/reader034/viewer/2022042614/55a3505f1a28abf3478b4661/html5/thumbnails/7.jpg)
Your controllers should look like this.
![Page 8: Dealing with Obese Models](https://reader034.fdocuments.us/reader034/viewer/2022042614/55a3505f1a28abf3478b4661/html5/thumbnails/8.jpg)
Your stylesheets should look like this.
![Page 9: Dealing with Obese Models](https://reader034.fdocuments.us/reader034/viewer/2022042614/55a3505f1a28abf3478b4661/html5/thumbnails/9.jpg)
Your javascript should apparently look like this.
![Page 10: Dealing with Obese Models](https://reader034.fdocuments.us/reader034/viewer/2022042614/55a3505f1a28abf3478b4661/html5/thumbnails/10.jpg)
SO WHY IS THIS
OK FOR YOUR
MODELS?
IS THERE A
RAILS
CONVENTION
TO DEAL WITH
THIS?
![Page 11: Dealing with Obese Models](https://reader034.fdocuments.us/reader034/viewer/2022042614/55a3505f1a28abf3478b4661/html5/thumbnails/11.jpg)
FAT MODELS
SKINNY CONTROLLERS
![Page 12: Dealing with Obese Models](https://reader034.fdocuments.us/reader034/viewer/2022042614/55a3505f1a28abf3478b4661/html5/thumbnails/12.jpg)
MAYBE FAT
MODELS AREN’T
THE
SOLUTION…
![Page 13: Dealing with Obese Models](https://reader034.fdocuments.us/reader034/viewer/2022042614/55a3505f1a28abf3478b4661/html5/thumbnails/13.jpg)
FAT MODELS
SKINNY CONTROLLERS
REASONABLY HEALTHY MODELS
![Page 14: Dealing with Obese Models](https://reader034.fdocuments.us/reader034/viewer/2022042614/55a3505f1a28abf3478b4661/html5/thumbnails/14.jpg)
EXAMPLE
TIME!
Let’s build a person model!
![Page 15: Dealing with Obese Models](https://reader034.fdocuments.us/reader034/viewer/2022042614/55a3505f1a28abf3478b4661/html5/thumbnails/15.jpg)
PERSON • Attributes
![Page 16: Dealing with Obese Models](https://reader034.fdocuments.us/reader034/viewer/2022042614/55a3505f1a28abf3478b4661/html5/thumbnails/16.jpg)
PERSON • Attributes
• Associations
![Page 17: Dealing with Obese Models](https://reader034.fdocuments.us/reader034/viewer/2022042614/55a3505f1a28abf3478b4661/html5/thumbnails/17.jpg)
PERSON • Attributes
• Associations
• Validation
![Page 18: Dealing with Obese Models](https://reader034.fdocuments.us/reader034/viewer/2022042614/55a3505f1a28abf3478b4661/html5/thumbnails/18.jpg)
• Attributes
• Associations
• Validation
• Scopes for searching
PERSON
![Page 19: Dealing with Obese Models](https://reader034.fdocuments.us/reader034/viewer/2022042614/55a3505f1a28abf3478b4661/html5/thumbnails/19.jpg)
• Attributes
• Associations
• Validation
• Scopes for searching
• Scopes for authorization
PERSON
![Page 20: Dealing with Obese Models](https://reader034.fdocuments.us/reader034/viewer/2022042614/55a3505f1a28abf3478b4661/html5/thumbnails/20.jpg)
• Attributes
• Associations
• Validation
• Scopes for searching
• Scopes for authorization
• State Machine
PERSON
![Page 21: Dealing with Obese Models](https://reader034.fdocuments.us/reader034/viewer/2022042614/55a3505f1a28abf3478b4661/html5/thumbnails/21.jpg)
• Attributes
• Associations
• Validation
• Scopes for searching
• Scopes for authorization
• State Machine
• Location / GeoTagging Stuff
PERSON
![Page 22: Dealing with Obese Models](https://reader034.fdocuments.us/reader034/viewer/2022042614/55a3505f1a28abf3478b4661/html5/thumbnails/22.jpg)
• Attributes
• Associations
• Validation
• Scopes for searching
• Scopes for authorization
• State Machine
• Location / GeoTagging Stuff
• Leaderboard
PERSON
![Page 23: Dealing with Obese Models](https://reader034.fdocuments.us/reader034/viewer/2022042614/55a3505f1a28abf3478b4661/html5/thumbnails/23.jpg)
• Attributes
• Associations
• Validation
• Scopes for searching
• Scopes for authorization
• State Machine
• Location / GeoTagging Stuff
• Leaderboard
• Relationship helper methods
PERSON
![Page 24: Dealing with Obese Models](https://reader034.fdocuments.us/reader034/viewer/2022042614/55a3505f1a28abf3478b4661/html5/thumbnails/24.jpg)
• Attributes
• Associations
• Validation
• Scopes for searching
• Scopes for authorization
• State Machine
• Location / GeoTagging Stuff
• Leaderboard
• Relationship helper methods
• Formatting methods
PERSON
![Page 25: Dealing with Obese Models](https://reader034.fdocuments.us/reader034/viewer/2022042614/55a3505f1a28abf3478b4661/html5/thumbnails/25.jpg)
WHAT
HAPPENED?
![Page 26: Dealing with Obese Models](https://reader034.fdocuments.us/reader034/viewer/2022042614/55a3505f1a28abf3478b4661/html5/thumbnails/26.jpg)
OBESITY
It’s not just for Texas anymore.
![Page 27: Dealing with Obese Models](https://reader034.fdocuments.us/reader034/viewer/2022042614/55a3505f1a28abf3478b4661/html5/thumbnails/27.jpg)
LET’S GET
OUR MODEL
BACK IN
SHAPE!
![Page 28: Dealing with Obese Models](https://reader034.fdocuments.us/reader034/viewer/2022042614/55a3505f1a28abf3478b4661/html5/thumbnails/28.jpg)
STEP 1:
ORGANIZATION
![Page 29: Dealing with Obese Models](https://reader034.fdocuments.us/reader034/viewer/2022042614/55a3505f1a28abf3478b4661/html5/thumbnails/29.jpg)
BIG,
BEAUTIFUL
MODELS
SHOULD BE
ORGANIZED.
![Page 30: Dealing with Obese Models](https://reader034.fdocuments.us/reader034/viewer/2022042614/55a3505f1a28abf3478b4661/html5/thumbnails/30.jpg)
BIG,
BEAUTIFUL
MODELS
SHOULD BE
ORGANIZED.
WHITESPACE
IS YOUR
FRIEND.
![Page 31: Dealing with Obese Models](https://reader034.fdocuments.us/reader034/viewer/2022042614/55a3505f1a28abf3478b4661/html5/thumbnails/31.jpg)
BIG,
BEAUTIFUL
MODELS
SHOULD BE
ORGANIZED.
WHITESPACE
IS YOUR
FRIEND.
DON’T BE
AFRAID OF
COMMENTS.
![Page 32: Dealing with Obese Models](https://reader034.fdocuments.us/reader034/viewer/2022042614/55a3505f1a28abf3478b4661/html5/thumbnails/32.jpg)
BIG,
BEAUTIFUL
MODELS
SHOULD BE
ORGANIZED.
WHITESPACE
IS YOUR
FRIEND.
DON’T BE
AFRAID OF
COMMENTS.
PLAY AROUND
WITH
INDENTING.
![Page 33: Dealing with Obese Models](https://reader034.fdocuments.us/reader034/viewer/2022042614/55a3505f1a28abf3478b4661/html5/thumbnails/33.jpg)
STEP 2:
RELOCATING
![Page 34: Dealing with Obese Models](https://reader034.fdocuments.us/reader034/viewer/2022042614/55a3505f1a28abf3478b4661/html5/thumbnails/34.jpg)
LOOK FOR
CODE THAT’S
TOO
INTERESTED
IN ANOTHER
MODEL.
person.rb
![Page 35: Dealing with Obese Models](https://reader034.fdocuments.us/reader034/viewer/2022042614/55a3505f1a28abf3478b4661/html5/thumbnails/35.jpg)
LOOK FOR
CODE THAT’S
TOO
INTERESTED
IN ANOTHER
MODEL.
person.rb
IT MIGHT
MAKE MORE
SENSE TO
MOVE IT
THERE.
person.rb
feedback.rb
![Page 36: Dealing with Obese Models](https://reader034.fdocuments.us/reader034/viewer/2022042614/55a3505f1a28abf3478b4661/html5/thumbnails/36.jpg)
LOOK FOR
CODE THAT’S
TOO
INTERESTED
IN ANOTHER
MODEL.
person.rb
IT MIGHT
MAKE MORE
SENSE TO
MOVE IT
THERE.
person.rb
feedback.rb
FATNESS
ISN’T JUST
ABOUT LOC.
![Page 37: Dealing with Obese Models](https://reader034.fdocuments.us/reader034/viewer/2022042614/55a3505f1a28abf3478b4661/html5/thumbnails/37.jpg)
STEP 3:
ABSTRACTING
COMMON
BEHAVIOUR
![Page 38: Dealing with Obese Models](https://reader034.fdocuments.us/reader034/viewer/2022042614/55a3505f1a28abf3478b4661/html5/thumbnails/38.jpg)
SOMETIMES
THINGS
AREN’T
CENTRAL TO
YOUR MODEL.
![Page 39: Dealing with Obese Models](https://reader034.fdocuments.us/reader034/viewer/2022042614/55a3505f1a28abf3478b4661/html5/thumbnails/39.jpg)
SOMETIMES
THINGS
AREN’T
CENTRAL TO
YOUR MODEL.
BUILD YOUR
OWN
FRAMEWORK.
![Page 40: Dealing with Obese Models](https://reader034.fdocuments.us/reader034/viewer/2022042614/55a3505f1a28abf3478b4661/html5/thumbnails/40.jpg)
SOMETIMES
THINGS
AREN’T
CENTRAL TO
YOUR MODEL.
BUILD YOUR
OWN
FRAMEWORK.
EVEN IF YOU
ONLY DO IT
ONCE.
![Page 41: Dealing with Obese Models](https://reader034.fdocuments.us/reader034/viewer/2022042614/55a3505f1a28abf3478b4661/html5/thumbnails/41.jpg)
STEP 4:
PRESENTATION
LOGIC
![Page 42: Dealing with Obese Models](https://reader034.fdocuments.us/reader034/viewer/2022042614/55a3505f1a28abf3478b4661/html5/thumbnails/42.jpg)
WATCH FOR
PRESENTATION
LOGIC IN YOUR
MODELS
CONSIDER
HTML
HARMFUL.
MODELS
SHOULDN’T
FORMAT DATA.
IF YOU’RE
RETURNING
SOMETHING
OTHER THAN
MODELS, BE
CAREFUL..
![Page 43: Dealing with Obese Models](https://reader034.fdocuments.us/reader034/viewer/2022042614/55a3505f1a28abf3478b4661/html5/thumbnails/43.jpg)
THE
TRADITIONAL
RAILS
SOLUTION -
HELPERS
![Page 44: Dealing with Obese Models](https://reader034.fdocuments.us/reader034/viewer/2022042614/55a3505f1a28abf3478b4661/html5/thumbnails/44.jpg)
BUT..
HELPERS
SUCK WHEN
YOU DEPEND
ON STATE.
![Page 45: Dealing with Obese Models](https://reader034.fdocuments.us/reader034/viewer/2022042614/55a3505f1a28abf3478b4661/html5/thumbnails/45.jpg)
DECORATORS
Like Helpers, but don’t suck.
![Page 46: Dealing with Obese Models](https://reader034.fdocuments.us/reader034/viewer/2022042614/55a3505f1a28abf3478b4661/html5/thumbnails/46.jpg)
• Delegates methods to the subject of the decorator.
• Provides additional methods for presentation logic.
• Use them like you’d use the model, but feel free to add presentation logic.
ANATOMY OF
A DECORATOR
![Page 47: Dealing with Obese Models](https://reader034.fdocuments.us/reader034/viewer/2022042614/55a3505f1a28abf3478b4661/html5/thumbnails/47.jpg)
DECORATORS
ARE A WAY TO
EXTEND YOUR
MODELS TO
ADD
PRESENTATION
LOGIC
![Page 48: Dealing with Obese Models](https://reader034.fdocuments.us/reader034/viewer/2022042614/55a3505f1a28abf3478b4661/html5/thumbnails/48.jpg)
ALL THE DELEGATION WORK IS DONE FOR YOU.
https://github.com/jcasimir/rails_decorators
![Page 49: Dealing with Obese Models](https://reader034.fdocuments.us/reader034/viewer/2022042614/55a3505f1a28abf3478b4661/html5/thumbnails/49.jpg)
STEP 5:
PRESENTERS
![Page 50: Dealing with Obese Models](https://reader034.fdocuments.us/reader034/viewer/2022042614/55a3505f1a28abf3478b4661/html5/thumbnails/50.jpg)
EXAMPLE
TIME!
Relationship Viewer
![Page 51: Dealing with Obese Models](https://reader034.fdocuments.us/reader034/viewer/2022042614/55a3505f1a28abf3478b4661/html5/thumbnails/51.jpg)
WE NEEDED THIS
INTERFACE.
IT NEEDS TO
INTERACT WITH
FILTERED
RELATIONSHIPS
IN A LOT OF
DIFFERENT WAYS
![Page 52: Dealing with Obese Models](https://reader034.fdocuments.us/reader034/viewer/2022042614/55a3505f1a28abf3478b4661/html5/thumbnails/52.jpg)
ALL WE NEED IS
METHODS FOR:
• Whether the current user is connected.
• Counts of all connections filtered by customer type.
• Lists of relationships by relationship type.
• Whether the user has submitted feedback.
• How many users are currently in the system.
• Content linked to those users.
![Page 53: Dealing with Obese Models](https://reader034.fdocuments.us/reader034/viewer/2022042614/55a3505f1a28abf3478b4661/html5/thumbnails/53.jpg)
PHEW!
That there’s a lot of stuff.
![Page 54: Dealing with Obese Models](https://reader034.fdocuments.us/reader034/viewer/2022042614/55a3505f1a28abf3478b4661/html5/thumbnails/54.jpg)
ATTEMPT #1
Let’s fatten up some models.
![Page 55: Dealing with Obese Models](https://reader034.fdocuments.us/reader034/viewer/2022042614/55a3505f1a28abf3478b4661/html5/thumbnails/55.jpg)
relationship.rb
![Page 56: Dealing with Obese Models](https://reader034.fdocuments.us/reader034/viewer/2022042614/55a3505f1a28abf3478b4661/html5/thumbnails/56.jpg)
relationships_controller.rb
![Page 57: Dealing with Obese Models](https://reader034.fdocuments.us/reader034/viewer/2022042614/55a3505f1a28abf3478b4661/html5/thumbnails/57.jpg)
LET’S ADD A
PRESENTER!
![Page 58: Dealing with Obese Models](https://reader034.fdocuments.us/reader034/viewer/2022042614/55a3505f1a28abf3478b4661/html5/thumbnails/58.jpg)
prospect_match_presenter.rb
![Page 59: Dealing with Obese Models](https://reader034.fdocuments.us/reader034/viewer/2022042614/55a3505f1a28abf3478b4661/html5/thumbnails/59.jpg)
prospect_match_controller.rb
![Page 60: Dealing with Obese Models](https://reader034.fdocuments.us/reader034/viewer/2022042614/55a3505f1a28abf3478b4661/html5/thumbnails/60.jpg)
CLEAN,
WONDERFUL
VIEWS
![Page 61: Dealing with Obese Models](https://reader034.fdocuments.us/reader034/viewer/2022042614/55a3505f1a28abf3478b4661/html5/thumbnails/61.jpg)
WHY USE
PRESENTERS?
• Your models are concerned only with what they do.
• Your controllers and views talk to a well-defined interface.
![Page 62: Dealing with Obese Models](https://reader034.fdocuments.us/reader034/viewer/2022042614/55a3505f1a28abf3478b4661/html5/thumbnails/62.jpg)
WHEN TO
USE A
PRESENTER
• Your view talks about more than one model (in a poorly related way)
• You have lots of instance variables on your controller action.
• Your model is too clever about things it isn’t related to.
![Page 63: Dealing with Obese Models](https://reader034.fdocuments.us/reader034/viewer/2022042614/55a3505f1a28abf3478b4661/html5/thumbnails/63.jpg)
READY FOR
BEACH
SEASON
![Page 64: Dealing with Obese Models](https://reader034.fdocuments.us/reader034/viewer/2022042614/55a3505f1a28abf3478b4661/html5/thumbnails/64.jpg)
SUMMARY
1. Sweat the easy stuff first.
2. Organization goes a long way.
3. Limit how much your models know about other things.
4. Consider presenters or decorators for complicated scenarios.