Trailblazer: A New Architecture For Rails by Nick Sutterer
English | 2015 | ISBN: N/A | 259 pages | PDF/EPUB/MOBI | 19 Mb
English | 2015 | ISBN: N/A | 259 pages | PDF/EPUB/MOBI | 19 Mb
Note: This book discusses Trailblazer 1.x. We provide free guides for TRB2 on the website, but the general idea of this book still applies.
Trailblazer is a thin layer on top of Rails and brings a high-level application architecture, decent encapsulation and a new code structure. Here are the main features.
Logicless models solely focusing on persistence.
Skinny controllers working as endpoints, only.
Domain objects for your business logic, known as operations.
Form objects for deserialisation and validation.
View models to introduce a widget architecture.
Representers that parse and render documents for APIs.
Twin objects for decorating models.
This book takes you for an adventure trip from Rails' code jungle to Trailblazer beach. We're building a complete Rails application using all of Trailblazer's goodies while exploiting the Rails Way where it helps us.
What we build here is not just a simple 5-minutes blog, but a full-blown commenting application with complex forms, composed views, signed-in users and admin, authentication rules, polymorphic views and operatoins, a document-based JSON API, and more. We have a strong focus on testing, object design and structuring the code using Trailblazer's concept-oriented framework.
2. The Trailblazer Architectural Style
Chapter 2 is a complete overview about all the concepts of Trailblazer, starting from domain objects, integrated form to view models, policies and twins.
3. Operations and Forms
The third chapter gives you everything you need to encapsulate your business logic into operations and building rock-solid domain components using the integrated form. We also learn how to render forms from operations, and how to test both for a good sleep.
4. Cells
We then discuss the principles of Cells view models, how to encapsulate your view into components and the way helpers work in this layer. Learning how to test your rendering and integrating those with smoke tests round up this fine chapter.
5. Nested Forms
Forms can and do represent deeply nested object structures. In this chapter we learn how to nest forms to create more than one model in an operation.
6. Composed Views and AJAX Pagination
Usability, or "UI is king!" is one of Trailblazer's maxims. We walk through advanced Cells features, learn how to embedd forms into existing pages, break Rails' "RESTfulness" and discuss AJAX-backed pagination with Cells.
7. Mastering Forms
Forms are an integral part of Trailblazer. They orchestrate deserialization of input, population and validation. The seventh chapter really dives into Reform, dynamic population, skipping nested items, form inheritance and removing items from collections. We also discuss Non-CRUD behavior and how operation and form work together to expose more than just plain CRUD semantics.
8. Callbacks
Callbacks in Trailblazer no longer sit in one huge model file but get restructured in operations. Sending email notifications, adding and inheriting callbacks, and the structure of operations are the main content of this chapter. A discussion about view caching and expiring those via callbacks will prepare you to write advanced operations.
A section about file uploads will make sure this chapter covers all major parts of post-processing business logic.
9. Authentication
Operations are the perfect place for authentication logic. We will implement sign in, sign up, mail notifications, and many more authentication-related functions in this chapter. Strong tests will make sure only authenticated users get access to certain parts of the application.
You will learn how the Tyrant gem helps implementing authentication workflows without all the pain you've had with Devise.
10. Policies and Authorization
Policies are a technique to restrict users to specific operations and UI elements. But, there's more to it. Using policies and builders, we can use polymorphism to map operations to different contexts, like anonymous users or admins. We'll refine our forms and UI for specific user types in this chapter and learn everything about policies to protect your functions from unauthorized use.
11. Hypermedia APIs: Rendering
Operations in Trailblazer integrate with representers from the Representable/Roar gem. This helps quickly setting up document APIs. This chapter discusses how representers can be inferred, specified and explicitly created to render things, comments, and their associations following the HAL media format.
12. Hypermedia APIs: Deserialization
The hardest part of API code is the deserialization: parsing documents into object graphs. Trailblazer leverages representers to parse incoming documents directly to your operation's contract, and this chapter explains all you got to know to understand every step of it.