Rails的新架構:Trailblazer
Trailblazer是一個在Rails之上的一個輕微封裝。它輕微地加強了封裝,使代碼結構更直觀,并為您提供了一個面向對象的架構。
Trailblazer讓你能夠編寫邏輯少的模型,扮演一個純的數據對象,沒有包含回調,嵌套屬性,校驗或域的邏輯。它消除了笨重的控制器。
A Concept-Driven OOP Framework
Trailblazer offers you a new, more intuitive file layout in Rails apps where you structure files by concepts.
app
├── concepts
│ ├── comment
│ │ ├── cell.rb
│ │ ├── views
│ │ │ ├── show.haml
│ │ │ ├── list.haml
│ │ ├── assets
│ │ │ ├── comment.css.sass
│ │ ├── operation.rb
│ │ ├── twin.rb
Files, classes and views that logically belong to one concept are kept in one place. You are free to use additional namespaces within a concept. Trailblazer tries to keep it as simple as possible, though.
Architecture
Trailblazer extends the conventional MVC stack in Rails. Keep in mind that adding layers doesn't necessarily mean adding more code and complexity.
The opposite is the case: Controller, view and model become lean endpoints for HTTP, rendering and persistence. Redundant code gets eliminated by putting very little application code into the right layer.
Routing
Trailblazer uses Rails routing to map URLs to controllers (we will add simplifications to routing soon).
Controllers
Controllers are lean endpoints for HTTP. They differentiate between request formats like HTML or JSON and immediately dispatch to an operation. Controllers do not contain any business logic.
Trailblazer provides four methods to present and invoke operations. But before that, you need to include the Controller
module.
class CommentsController < ApplicationController include Trailblazer::Operation::Controller