As promised, the full text of the book is available to read online, for free.
In addition, the book is available for download in DRM-free PDF, Epub, and Mobi (Kindle) formats, along with full source code, for $5 USD.
I’m also dabbling in a limited form of “pay what you like”: If you’re feeling extra generous and want to contribute more than $5 in order to keep me working on projects like this, there’s a “Sponsor Edition” you might be interested in. See the store page for more details.
About the book
What is Objects on Rails? I’m not going to get into too much detail here, since you can just go read it if you want. But in a nutshell…
Objects on Rails is a sort of “developer notebook” of some ideas and techniques for applying classic object-oriented principles to Rails development. In order to write it I revisited the classic “blog” example, and tried to apply what I’ve learned over the years about OO and TDD to each step. And then at each step I carefully documented what I did, and wrote down the thinking behind my decisions. Sometimes I went on extended digressions about certain patterns and antipatterns. Other times I went down dead-end sidings, thought better of it, and tried to explain the thinking that lead me down them in the first place.
An early reader jokingly called it “how to build a blog in 15 months”, which I got a chuckle out of, because it’s kind of true. I picked a blog example because it’s something that’s familiar to everyone, a kind of “neutral canvas” that wouldn’t distract from the ideas being demonstrated. But the design choices I chose to illustrate are of the kind that are primarily relevant for growing and evolving large systems. Of course, every large, complex system starts out as a small simple one; and a robust architecture comes from making good decisions early on in the process.
While the process of development shown in the text is a more disciplined, thoughtful, and design-oriented one than you’d find in most “get kick-started fast” guides, I’ve tried hard to keep it pragmatic. A recurring theme in the book is trying to find a “middle way” between the Rails “golden path” and OO orthodoxy. I start with the premise that Rails is a fantastic foundation to build on for growing modern objected-oriented web applications. Then I make incremental additions that I try to keep more or less “with the grain” of Rails. Just as one example, you won’t find a lot of coverage in the book about strictly separating ActiveRecord classes from business logic. What you will find are some tips and tricks for treating the ActiveRecord part of your models as an implementation detail, so that if you ever do decide to draw a wider distinction between the two, it shouldn’t be a massive ordeal to tease them apart.
For those who enjoy bullet lists, here are some of the topics covered:
- Working from the outside in, starting from how the app should look to the user.
- Starting with plain objects and leaving persistence for later.
- Treating ActiveRecord as a private implementation detail.
- Sticking to the language of the domain.
- Using placeholder objects for rapid iteration.
- Incrementally extending objects with ActiveModel.
- Trees of objects versus “lone wolf” objects.
- Simple dependency injection with sensible defaults.
- Factoring out object roles.
- Keeping tests isolated and fast.
- When to throw tests away.
- Enabling objects to render themselves with Exhibits.
- Using Exhibits to deliver a rich hypermedia API.
Enjoy, won’t you?
Well I think that’s enough introduction. I hope you enjoy the book, and if nothing else that it gives you some food for thought. If you have questions, comments, or just want to discuss OO and Rails in general, please do join the discussion group.