A New Architecture For Rails

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.

Nick Sutterer

Nick loves working on open-source frameworks, and that's what he does almost every day. He also loves people, surfing, beer, skateboarding and loud music. Make sure to hug him should you ever meet him in person.

  • Introduction

    • How To Read This Book

    • The Missing Architecture

    • Learnings About Reality

  • The Trailblazer Architectural Style

    • Why Trailblazer?

    • A Non Intrusive Framework

    • Trailblazer In A Nutshell

    • Logicless Models

    • Concepts and File Structure

    • High-Level Domain

    • Operation

    • High-Level Architecture

    • Validations

    • Inheritance Over Configuration

    • Builders

    • Authorization And Policy

    • Authentication

    • Representers

    • API: Parsing and Rendering

    • Using Hypermedia

    • Rendering Views

    • Cells

    • Twin

    • Testing

    • A Note On Complexity

    • Summary

  • Operations And Forms

    • The Example app

    • The Create Operation

    • Controllers

    • Reform: Form Objects

    • Operation’s Form Integration

    • Model Semantics

    • Rendering Forms

    • Testing Validations

    • Testing Controllers: Integration Tests

    • Update Operation

    • Reform and strong_parameters

  • Cells

    • Rails Views and Encapsulation

    • View Models

    • Modelling the UI with Cells

    • Anatomy of a Cell

    • Rendering Collections

    • Integration Tests

    • Cell Tests

    • Nesting Cells

    • Rails and MVC

    • Final Test Setup

    • The cell Helper

    • Summary

  • Nested Forms

    • Adding Comments

    • The Comment Concept

    • The setup_model! Hook

    • Nested Contracts

    • Pre-populating Forms

    • Form Processing

    • Populating Forms for Validation

    • Saving Nested Objects

    • Flash messages and Redirecting

    • Readers for Operations

    • Static Form Population

    • Form Presentation Helpers

    • Pre-selecting Values

    • Operation and Form Tests

  • Composed Views

    • The present Helper.

    • Multiple Operations for Composed Pages

    • Form Submission: Widgets vs. “RESTful”

    • Writing a Cells Feature

    • Application-wide Features

    • Simple Decorator Helpers

    • Rails’ View Architecture

    • Kaminari Pagination and Cells

    • AJAX Pagination with Cells

    • On Controller Structuring

    • Cells Tests

    • Smoke Tests

    • Summary

  • Mastering Forms

    • Things and Authors

    • Adding Authors

    • Dynamic Prepopulation

    • Validation Population

    • Skipping Blanks

    • Form Debugging Techniques

    • Saving

    • Adding Authorships

    • Non-CRUD Behavior

    • Complex Validations

    • Testing Create

    • Presentation Tests

    • Updating Things

    • Hacking the View

    • Helpers in Forms

    • Form Inheritance

    • Virtual Properties

    • Populator

    • Persisting the Deletion

    • Testing Update

    • Extracting Forms to Separate Files

  • Callbacks

    • Domain Callbacks

    • The Persisted Module

    • Explicit Callbacks

    • Imperative Callbacks

    • Callbacks in Operations

    • Testing Callbacks

    • View Caching

    • Cache Keys

    • Expiring Keys

    • Debugging View Caching

    • Caching Composed Views

    • The CacheVersion Pattern

    • File Uploads

    • Paperdragon

    • File Validations

    • Callback Groups

    • Rendering Images

    • Testing Uploads

    • Conclusion

  • Authentication

    • Populating by ID

    • Tyrant

    • Sign Up

    • Authenticatable

    • SignUp Form

    • Testing SignUp

    • Sign In

    • Modelless Forms

    • Login

    • Application-wide Tyrant

    • A Warm Greeting

    • Signing Out

    • Testing Logins

    • Putting Users to Sleep

    • Waking Up Sleeping Users

    • Integration Tests for Wake Up

  • Authorization And Polymorphism

    • Policies

    • Operation Inheritance

    • Polymorphic Builders

    • Resolver

    • Refining Operations with Modules

    • Polymorphic Testing

    • Polymorphic Views

    • Integration Test: Create

    • Updating

    • Composable Operations

    • Update Tests

    • Delete

    • Impersonate

  • Hypermedia APIs: Rendering

    • File Structure

    • Rendering Comments

    • HAL Hypermedia Links

    • Rendering

    • Representer

    • GET Controller

    • Responders

    • Inferring Representer

    • Composing Representer

    • Rendering Nested Documents

    • Scopes and Sorting

    • Index Operation

    • Lonely Collections

    • Filtering

  • Hypermedia APIs: Deserialization

    • Deserialization in Trailblazer

    • Parsing Comment

    • Deserialization and Contract

    • Deserialization in Reform

    • Deserializing HAL

    • Create

    • Polymorphic Update

    • Basic Auth

    • Discussion: Polymorphic Operations and APIs

  • Perspectives

    • This Book

    • Why Not a New Framework?

    • Rails

    • Webmachine

    • Grape

    • ROM

    • Roda

    • Lotus

    • Trailblazer