dealing with obese models
TRANSCRIPT
DEALING WITH
OBESE MODELS
A 5 Step Guide to Getting your models in shape
Ryan Brunner, Influitive
@ryanbrunner
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.
CONVENTIONS
CONVENTIONS
Your folders should look like this.
Your views should look like this.
Your controllers should look like this.
Your stylesheets should look like this.
Your javascript should apparently look like this.
SO WHY IS THIS
OK FOR YOUR
MODELS?
IS THERE A
RAILS
CONVENTION
TO DEAL WITH
THIS?
FAT MODELS
SKINNY CONTROLLERS
MAYBE FAT
MODELS AREN’T
THE
SOLUTION…
FAT MODELS
SKINNY CONTROLLERS
REASONABLY HEALTHY MODELS
EXAMPLE
TIME!
Let’s build a person model!
PERSON • Attributes
PERSON • Attributes
• Associations
PERSON • Attributes
• Associations
• Validation
• Attributes
• Associations
• Validation
• Scopes for searching
PERSON
• Attributes
• Associations
• Validation
• Scopes for searching
• Scopes for authorization
PERSON
• Attributes
• Associations
• Validation
• Scopes for searching
• Scopes for authorization
• State Machine
PERSON
• Attributes
• Associations
• Validation
• Scopes for searching
• Scopes for authorization
• State Machine
• Location / GeoTagging Stuff
PERSON
• Attributes
• Associations
• Validation
• Scopes for searching
• Scopes for authorization
• State Machine
• Location / GeoTagging Stuff
• Leaderboard
PERSON
• Attributes
• Associations
• Validation
• Scopes for searching
• Scopes for authorization
• State Machine
• Location / GeoTagging Stuff
• Leaderboard
• Relationship helper methods
PERSON
• Attributes
• Associations
• Validation
• Scopes for searching
• Scopes for authorization
• State Machine
• Location / GeoTagging Stuff
• Leaderboard
• Relationship helper methods
• Formatting methods
PERSON
WHAT
HAPPENED?
OBESITY
It’s not just for Texas anymore.
LET’S GET
OUR MODEL
BACK IN
SHAPE!
STEP 1:
ORGANIZATION
BIG,
BEAUTIFUL
MODELS
SHOULD BE
ORGANIZED.
BIG,
BEAUTIFUL
MODELS
SHOULD BE
ORGANIZED.
WHITESPACE
IS YOUR
FRIEND.
BIG,
BEAUTIFUL
MODELS
SHOULD BE
ORGANIZED.
WHITESPACE
IS YOUR
FRIEND.
DON’T BE
AFRAID OF
COMMENTS.
BIG,
BEAUTIFUL
MODELS
SHOULD BE
ORGANIZED.
WHITESPACE
IS YOUR
FRIEND.
DON’T BE
AFRAID OF
COMMENTS.
PLAY AROUND
WITH
INDENTING.
STEP 2:
RELOCATING
LOOK FOR
CODE THAT’S
TOO
INTERESTED
IN ANOTHER
MODEL.
person.rb
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
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.
STEP 3:
ABSTRACTING
COMMON
BEHAVIOUR
SOMETIMES
THINGS
AREN’T
CENTRAL TO
YOUR MODEL.
SOMETIMES
THINGS
AREN’T
CENTRAL TO
YOUR MODEL.
BUILD YOUR
OWN
FRAMEWORK.
SOMETIMES
THINGS
AREN’T
CENTRAL TO
YOUR MODEL.
BUILD YOUR
OWN
FRAMEWORK.
EVEN IF YOU
ONLY DO IT
ONCE.
STEP 4:
PRESENTATION
LOGIC
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..
THE
TRADITIONAL
RAILS
SOLUTION -
HELPERS
BUT..
HELPERS
SUCK WHEN
YOU DEPEND
ON STATE.
DECORATORS
Like Helpers, but don’t suck.
• 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
DECORATORS
ARE A WAY TO
EXTEND YOUR
MODELS TO
ADD
PRESENTATION
LOGIC
ALL THE DELEGATION WORK IS DONE FOR YOU.
https://github.com/jcasimir/rails_decorators
STEP 5:
PRESENTERS
EXAMPLE
TIME!
Relationship Viewer
WE NEEDED THIS
INTERFACE.
IT NEEDS TO
INTERACT WITH
FILTERED
RELATIONSHIPS
IN A LOT OF
DIFFERENT WAYS
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.
PHEW!
That there’s a lot of stuff.
ATTEMPT #1
Let’s fatten up some models.
relationship.rb
relationships_controller.rb
LET’S ADD A
PRESENTER!
prospect_match_presenter.rb
prospect_match_controller.rb
CLEAN,
WONDERFUL
VIEWS
WHY USE
PRESENTERS?
• Your models are concerned only with what they do.
• Your controllers and views talk to a well-defined interface.
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.
READY FOR
BEACH
SEASON
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.