An Introduction to Trailblazer
-
Upload
steve-brudz -
Category
Technology
-
view
482 -
download
2
Transcript of An Introduction to Trailblazer
An Introduction to Trailblazer
Steve BrudzLead Consultant @ Cyrus Innovation
@SteveBrudz / github.com/sbrudzMarch 8th, NYC.rb Meetup
#trbrb
The Challenge: Managing Complexity
Complexity by Mark Skipper via Flickr CC BY 2.0
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• …
Every Application Faces It
Rails MVC
Best Practices
• Too much logic in your view?
- Bryan Helmkamp, Brian Vanloo, James Golick, and many others
Best Practices
• Too much logic in your view?– Put it in a view model insteadt
- Bryan Helmkamp, Brian Vanloo, James Golick, and many others
Best Practices
• Too much logic in your view?– Put it in a view model instead
• Too much logic in your controller?
- Bryan Helmkamp, Brian Vanloo, James Golick, and many others
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
- Bryan Helmkamp, Brian Vanloo, James Golick, and many others
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
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
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
Great!
Great!But there are so many options
which do I choose…and how do I know if I’m on the right track…
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?
Trailblazer
An architectural layer for ruby-based web applications
Trailblazer
A set of Gems to help with implementing these patterns
Trailblazer
With clear conventions for where to put your logic
Trailblazer
And extensive documentation
https://leanpub.com/trailblazer http://trailblazer.to/
Trailblazer
A set of Gems to help with implementing these patterns
Trailblazer
A set of Gems to help with implementing these patterns
Trailblazer
A set of Gems to help with implementing these patterns
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
Rails MVC
Trailblazer
GET /accounts/1
Traditional Rails controllers/accounts_controller.rb
GET /accounts/1
Trailblazer controllers/accounts_controller.rb
GET /accounts/1
Trailblazer concepts/account/read.rb
GET /accounts/1
Trailblazer concepts/account/policy.rb
GET /accounts/newPOST /accounts
Traditional Rails controllers/accounts_controller.rb
GET /accounts/newPOST /accounts
Trailblazer controllers/accounts_controller.rb
GET /accounts/newPOST /accounts
Trailblazer concepts/account/create.rb
GET /accounts/newPOST /accounts
Trailblazer spec/concepts/account/create_spec.rb
GET /accounts/newPOST /accounts
Trailblazer concepts/account/policy.rb
GET /accounts/newPOST /accounts
Trailblazer concepts/account/contract.rb
GET /accounts/newPOST /accounts
Trailblazer concepts/account/contract.rb
GET /accounts/newPOST /accounts
Traditional Rails models/account.rb
GET /accounts/newPOST /accounts
Trailblazer models/account.rb
GET /accounts/newPOST /accounts
Traditional Rails views/accounts/show.html.erb
GET /accounts/newPOST /accounts
Trailblazer views/accounts/show.html.erb
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• …
New Requirements
Account Summary Page
Account Summary Page
Traditional Rails views/accounts/show.html.erb
Account Summary Page
views/accounts/show.html.erb
Trailblazer
Account Summary Page
Trailblazer concepts/account/cell.rb
Account Summary Page
Trailblazer concepts/account/views/show.erb
Account Summary Page
Trailblazer concepts/account/row_cell.rb
Account Summary Page
Trailblazer
concepts/account/views/row_cell.erb
Account Summary Page
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
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)
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
Thank you
• Cyrus Innovation• Nick Sutterer (@apotonick) • The Trailblazer community
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/
Trailblazer Resources
• http://trailblazer.to• https://leanpub.com/trailblazer• https://gitter.im/trailblazer/chat
Articles Referenced
• http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/
• https://www.toptal.com/ruby-on-rails/top-10-mistakes-that-rails-programmers-make
• http://jamesgolick.com/2010/3/14/crazy-heretical-and-awesome-the-way-i-write-rails-apps.html
• http://objectsonrails.com/
Questions?