An Introduction to Trailblazer

61
An Introduction to Trailblazer Steve Brudz Lead Consultant @ Cyrus Innovation @SteveBrudz / github.com/sbrudz March 8 th , NYC.rb Meetup

Transcript of An Introduction to Trailblazer

Page 1: An Introduction to Trailblazer

An Introduction to Trailblazer

Steve BrudzLead Consultant @ Cyrus Innovation

@SteveBrudz / github.com/sbrudzMarch 8th, NYC.rb Meetup

Page 2: An Introduction to Trailblazer

#trbrb

Page 3: An Introduction to Trailblazer
Page 4: An Introduction to Trailblazer

The Challenge: Managing Complexity

Complexity by Mark Skipper via Flickr CC BY 2.0

Page 5: An Introduction to Trailblazer

Common Symptoms

• New features are hard to estimate• People are afraid to touch parts of the code• New developers take a long time to ramp up• Code is hard to unit test• CI build is brittle and takes forever• Big classes with many responsibilities• Hard to know where to put new logic• …

Page 6: An Introduction to Trailblazer

Every Application Faces It

Page 7: An Introduction to Trailblazer

Rails MVC

Page 12: An Introduction to Trailblazer

Best Practices

• Too much logic in your view?– Put it in a view model instead

• Too much logic in your controller?– Put it in a form object, a policy object, or a service

object instead• Too much logic in your model?

- Bryan Helmkamp, Brian Vanloo, James Golick, and many others

Page 13: An Introduction to Trailblazer

Best Practices

• Too much logic in your view?– Put it in a view model instead

• Too much logic in your controller?– Put it in a form object, a policy object, or a service

object instead• Too much logic in your model?– Put it in a service object or a value object instead– t

- Bryan Helmkamp, Brian Vanloo, James Golick, and many others

Page 14: An Introduction to Trailblazer

Encapsulate Related Logic

• View Model• Form Object• Service Object• Policy Object• Value Object• Query Object• …

Benefits• Small classes• Easier to unit test• Easier to understand• Single responsibility

Page 15: An Introduction to Trailblazer

Great!

Page 16: An Introduction to Trailblazer

Great!But there are so many options

which do I choose…and how do I know if I’m on the right track…

Page 17: An Introduction to Trailblazer

Great!But there are so many options

which do I choose…and how do I know if I’m on the right track…

Wait, isn’t there a gem for that?

Page 18: An Introduction to Trailblazer

Trailblazer

An architectural layer for ruby-based web applications

Page 19: An Introduction to Trailblazer

Trailblazer

A set of Gems to help with implementing these patterns

Page 20: An Introduction to Trailblazer

Trailblazer

With clear conventions for where to put your logic

Page 21: An Introduction to Trailblazer

Trailblazer

And extensive documentation

https://leanpub.com/trailblazer http://trailblazer.to/

Page 22: An Introduction to Trailblazer
Page 23: An Introduction to Trailblazer

Trailblazer

A set of Gems to help with implementing these patterns

Page 24: An Introduction to Trailblazer

Trailblazer

A set of Gems to help with implementing these patterns

Page 25: An Introduction to Trailblazer

Trailblazer

A set of Gems to help with implementing these patterns

Page 26: An Introduction to Trailblazer

An Example

• A User can only view and edit their own Accounts• New Accounts should belong to the User that created them• An Account must have a name

Page 27: An Introduction to Trailblazer

Rails MVC

Page 28: An Introduction to Trailblazer

Trailblazer

Page 29: An Introduction to Trailblazer

GET /accounts/1

Traditional Rails controllers/accounts_controller.rb

Page 30: An Introduction to Trailblazer

GET /accounts/1

Trailblazer controllers/accounts_controller.rb

Page 31: An Introduction to Trailblazer

GET /accounts/1

Trailblazer concepts/account/read.rb

Page 32: An Introduction to Trailblazer

GET /accounts/1

Trailblazer concepts/account/policy.rb

Page 33: An Introduction to Trailblazer

GET /accounts/newPOST /accounts

Traditional Rails controllers/accounts_controller.rb

Page 34: An Introduction to Trailblazer

GET /accounts/newPOST /accounts

Trailblazer controllers/accounts_controller.rb

Page 35: An Introduction to Trailblazer

GET /accounts/newPOST /accounts

Trailblazer concepts/account/create.rb

Page 36: An Introduction to Trailblazer

GET /accounts/newPOST /accounts

Trailblazer spec/concepts/account/create_spec.rb

Page 37: An Introduction to Trailblazer

GET /accounts/newPOST /accounts

Trailblazer concepts/account/policy.rb

Page 38: An Introduction to Trailblazer

GET /accounts/newPOST /accounts

Trailblazer concepts/account/contract.rb

Page 39: An Introduction to Trailblazer

GET /accounts/newPOST /accounts

Trailblazer concepts/account/contract.rb

Page 40: An Introduction to Trailblazer

GET /accounts/newPOST /accounts

Traditional Rails models/account.rb

Page 41: An Introduction to Trailblazer

GET /accounts/newPOST /accounts

Trailblazer models/account.rb

Page 42: An Introduction to Trailblazer

GET /accounts/newPOST /accounts

Traditional Rails views/accounts/show.html.erb

Page 43: An Introduction to Trailblazer

GET /accounts/newPOST /accounts

Trailblazer views/accounts/show.html.erb

Page 44: An Introduction to Trailblazer

New Requirements

As a user, I should be able to:• View all transactions for an account• See my current and available balance• See a visual indicator for pending transactions• See the category for each transaction• See a flag for uncategorized transactions• …

Page 45: An Introduction to Trailblazer

New Requirements

Page 46: An Introduction to Trailblazer

Account Summary Page

Page 47: An Introduction to Trailblazer

Account Summary Page

Traditional Rails views/accounts/show.html.erb

Page 48: An Introduction to Trailblazer

Account Summary Page

views/accounts/show.html.erb

Trailblazer

Page 49: An Introduction to Trailblazer

Account Summary Page

Trailblazer concepts/account/cell.rb

Page 50: An Introduction to Trailblazer

Account Summary Page

Trailblazer concepts/account/views/show.erb

Page 51: An Introduction to Trailblazer

Account Summary Page

Trailblazer concepts/account/row_cell.rb

Page 52: An Introduction to Trailblazer

Account Summary Page

Trailblazer

concepts/account/views/row_cell.erb

Page 53: An Introduction to Trailblazer

Account Summary Page

Page 54: An Introduction to Trailblazer

The Cool Stuff

• Operations orchestrate complex business logic• Policies cleanly handle authorization• Cells manage complexity in the view layer• Reform enables editing multiple models at once• Representable and ROAR for APIs• Testability• Great documentation with clear patterns• Supportive community

Page 55: An Introduction to Trailblazer

The Not-so-Cool Stuff

• The learning curve• ReForm objects with nested properties can be

finicky• Partial use of Trailblazer can make app harder

to understand• Sharing model namespace can sometimes

cause weird TypeErrors (Account::Create shares Account < ActiveRecord::Base)

Page 56: An Introduction to Trailblazer

When and where to use Trailblazer?

• New projects that will need to scale• Existing large projects that use patterns

inconsistently• Existing projects that need only part of it

Page 57: An Introduction to Trailblazer

Thank you

• Cyrus Innovation• Nick Sutterer (@apotonick) • The Trailblazer community

Page 58: An Introduction to Trailblazer

Cyrus Innovation

• If you need help with getting your Rails app under control, come talk to us:

http://www.cyrusinnovation.com/contact/

• We’re hiring, too:http://www.cyrusinnovation.com/careers/current-openings/

Page 59: An Introduction to Trailblazer

Trailblazer Resources

• http://trailblazer.to• https://leanpub.com/trailblazer• https://gitter.im/trailblazer/chat

Page 61: An Introduction to Trailblazer

Questions?