dealing with obese models

64
DEALING WITH OBESE MODELS A 5 Step Guide to Getting your models in shape Ryan Brunner, Influitive @ryanbrunner

Upload: ryan-brunner

Post on 13-Jul-2015

1.134 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Dealing with Obese Models

DEALING WITH

OBESE MODELS

A 5 Step Guide to Getting your models in shape

Ryan Brunner, Influitive

@ryanbrunner

Page 2: Dealing with Obese Models

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

CONVENTIONS

Page 4: Dealing with Obese Models

CONVENTIONS

Page 5: Dealing with Obese Models

Your folders should look like this.

Page 6: Dealing with Obese Models

Your views should look like this.

Page 7: Dealing with Obese Models

Your controllers should look like this.

Page 8: Dealing with Obese Models

Your stylesheets should look like this.

Page 9: Dealing with Obese Models

Your javascript should apparently look like this.

Page 10: Dealing with Obese Models

SO WHY IS THIS

OK FOR YOUR

MODELS?

IS THERE A

RAILS

CONVENTION

TO DEAL WITH

THIS?

Page 11: Dealing with Obese Models

FAT MODELS

SKINNY CONTROLLERS

Page 12: Dealing with Obese Models

MAYBE FAT

MODELS AREN’T

THE

SOLUTION…

Page 13: Dealing with Obese Models

FAT MODELS

SKINNY CONTROLLERS

REASONABLY HEALTHY MODELS

Page 14: Dealing with Obese Models

EXAMPLE

TIME!

Let’s build a person model!

Page 15: Dealing with Obese Models

PERSON • Attributes

Page 16: Dealing with Obese Models

PERSON • Attributes

• Associations

Page 17: Dealing with Obese Models

PERSON • Attributes

• Associations

• Validation

Page 18: Dealing with Obese Models

• Attributes

• Associations

• Validation

• Scopes for searching

PERSON

Page 19: Dealing with Obese Models

• Attributes

• Associations

• Validation

• Scopes for searching

• Scopes for authorization

PERSON

Page 20: Dealing with Obese Models

• Attributes

• Associations

• Validation

• Scopes for searching

• Scopes for authorization

• State Machine

PERSON

Page 21: Dealing with Obese Models

• Attributes

• Associations

• Validation

• Scopes for searching

• Scopes for authorization

• State Machine

• Location / GeoTagging Stuff

PERSON

Page 22: Dealing with Obese Models

• Attributes

• Associations

• Validation

• Scopes for searching

• Scopes for authorization

• State Machine

• Location / GeoTagging Stuff

• Leaderboard

PERSON

Page 23: Dealing with Obese Models

• 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

• 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

WHAT

HAPPENED?

Page 26: Dealing with Obese Models

OBESITY

It’s not just for Texas anymore.

Page 27: Dealing with Obese Models

LET’S GET

OUR MODEL

BACK IN

SHAPE!

Page 28: Dealing with Obese Models

STEP 1:

ORGANIZATION

Page 29: Dealing with Obese Models

BIG,

BEAUTIFUL

MODELS

SHOULD BE

ORGANIZED.

Page 30: Dealing with Obese Models

BIG,

BEAUTIFUL

MODELS

SHOULD BE

ORGANIZED.

WHITESPACE

IS YOUR

FRIEND.

Page 31: Dealing with Obese Models

BIG,

BEAUTIFUL

MODELS

SHOULD BE

ORGANIZED.

WHITESPACE

IS YOUR

FRIEND.

DON’T BE

AFRAID OF

COMMENTS.

Page 32: Dealing with Obese Models

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

STEP 2:

RELOCATING

Page 34: Dealing with Obese Models

LOOK FOR

CODE THAT’S

TOO

INTERESTED

IN ANOTHER

MODEL.

person.rb

Page 35: Dealing with Obese Models

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

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

STEP 3:

ABSTRACTING

COMMON

BEHAVIOUR

Page 38: Dealing with Obese Models

SOMETIMES

THINGS

AREN’T

CENTRAL TO

YOUR MODEL.

Page 39: Dealing with Obese Models

SOMETIMES

THINGS

AREN’T

CENTRAL TO

YOUR MODEL.

BUILD YOUR

OWN

FRAMEWORK.

Page 40: Dealing with Obese Models

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

STEP 4:

PRESENTATION

LOGIC

Page 42: Dealing with Obese Models

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

THE

TRADITIONAL

RAILS

SOLUTION -

HELPERS

Page 44: Dealing with Obese Models

BUT..

HELPERS

SUCK WHEN

YOU DEPEND

ON STATE.

Page 45: Dealing with Obese Models

DECORATORS

Like Helpers, but don’t suck.

Page 46: Dealing with Obese Models

• 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

DECORATORS

ARE A WAY TO

EXTEND YOUR

MODELS TO

ADD

PRESENTATION

LOGIC

Page 48: Dealing with Obese Models

ALL THE DELEGATION WORK IS DONE FOR YOU.

https://github.com/jcasimir/rails_decorators

Page 49: Dealing with Obese Models

STEP 5:

PRESENTERS

Page 50: Dealing with Obese Models

EXAMPLE

TIME!

Relationship Viewer

Page 51: Dealing with Obese Models

WE NEEDED THIS

INTERFACE.

IT NEEDS TO

INTERACT WITH

FILTERED

RELATIONSHIPS

IN A LOT OF

DIFFERENT WAYS

Page 52: Dealing with Obese Models

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

PHEW!

That there’s a lot of stuff.

Page 54: Dealing with Obese Models

ATTEMPT #1

Let’s fatten up some models.

Page 55: Dealing with Obese Models

relationship.rb

Page 56: Dealing with Obese Models

relationships_controller.rb

Page 57: Dealing with Obese Models

LET’S ADD A

PRESENTER!

Page 58: Dealing with Obese Models

prospect_match_presenter.rb

Page 59: Dealing with Obese Models

prospect_match_controller.rb

Page 60: Dealing with Obese Models

CLEAN,

WONDERFUL

VIEWS

Page 61: Dealing with Obese Models

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

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

READY FOR

BEACH

SEASON

Page 64: Dealing with Obese Models

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.