Crafting Rails Applications
Expert Practices for Everyday Rails Development
Jos Valim
Version: P1.0 (March 2011)
Copyright 2011, The Pragmatic Bookshelf. This book is licensed tothe individual who purchased it. We don't copy-protect itbecause that would limit your ability to use it for yourown purposes. Please don't break this trustdon't allow othersto use your copy of the book. Thanks.
Dave & Andy.
Table of Contents
Copyright 2011, The Pragmatic Bookshelf.
Chapter 1
Acknowledgments
First and foremost, I am grateful to my wife for the care, for the love, and for occasionally dragging me outside to enjoy the world around us. Also thanks to my brother, parents, and the rest of family, who have always supported me.
I also want to thank the guys at Plataforma Tecnologia, specially George Guimares, Hugo Barana, and Marcelo Park. Without them, this book would not have been possible. Everyone at Plataforma helped since day one, when we were deciding the chapters contents, until the final paragraphs.
I am also thankful for the time given by the book reviewers, who steadily pushed me to increase the book quality. Thank you, Andre Arko, Bill Lazzar, Carlos Antnio da Silva, Daniel Neighman, Fbio Yamate, Gavin Hughes, Jonas Nicklas, Josh Kalderimis, Piotr Sarnacki, Santiago Pastorino, Trevor Turk, Vincius Baggio, and Xavier Noria.
Special thanks to my editor, Brian Hogan, and the Pragmatic Programmers, who helped me to change the book from great to excellent, and to Yehuda Katz for supporting me not only while writing this book but in Rails Core development as a whole.
Copyright 2011, The Pragmatic Bookshelf.
Chapter 2
Preface
Rails 3 is so much more than the next iteration of an excellent web development framework.
When Rails was first released in 2004, it revolutionized how web development was done by embracing concepts like Dont Repeat Yourself (DRY) and convention over configuration. As Rails gained momentum, the conventions that were making things work so well on the golden path started to get in the way of developers who had the urge to extend how Rails behaved or even replace whole components.
Some developers felt that using DataMapper instead of Active Record was a better fit. Other developers turned to MongoDB and other nonrelational databases but still wanted to use their favorite web framework. Then there were those developers who preferred RSpec to Test::Unit. These developers hacked, cobbled, or monkey-patched solutions together to accomplish their goals because previous versions of Rails did not provide a solid API or the modularity required to make these changes in a clean, maintainable fashion. Rails 3 significantly changes this game by exposing a set of more robust, modular, and performant APIs.
This book guides you through these new APIs through practical examples. In each chapter, we will use test-driven development to build a Rails extension or application that covers new Rails 3 features and how these features fit in the Rails 3 architecture. By the time you finish this book, you will understand Rails better and be more productive while writing more modular and faster Rails applications.
Who Should Read This Book?
If youre an intermediate or advanced Rails developer looking to dig deeper and make the Rails framework work for you, this is for you. Well go beyond the basics of Rails; instead of showing how Rails lets you use its built-in features to render HTML or XML from a controller, well show you how the render
method works so you can customize it to accept custom options, such as :pdf
.
Rails Versions
All projects in Crafting Rails Applications were developed and tested against Rails 3.0.3. Future stable releases, like Rails 3.0.4, 3.0.5, and so forth, should be suitable as well. You can check your Rails version with the following command:
And you can use gem install
to get the most appropriate version:
gem install rails -v 3.0.3 |
This book also has excerpts from Rails source code. All these excerpts were extracted from Rails 3.0.3.
All of the projects well build in this book should be compatible with Rails 3.1. In case we have small compatibility issues and deprecations, they will be posted in the online forum at the books website.
Note for Windows Developers
Some chapters have dependencies that rely on C extensions. These dependencies install fine in UNIX systems, but Windows developers need the DevKit, a toolkit that enables you to build many of the native C/C++ extensions available for Ruby.
Download and installation instructions are available online at http://rubyinstaller.org/downloads/.
Alternatively, you can get everything you need by installing RailsInstaller, which packages Ruby, Rails, and the DevKit, as well as several other common libraries.
What Is in the Book?
Well explore the inner workings of Rails across seven chapters.
In the Chapter a tool used throughout this book to create Rails extensions, and customize render
to accept :pdf
as an option with a behavior we will define. This chapter starts a series of discussions about Rails rendering stack.
In the Chapter , we will take a look at Active Model and its modules as we create an extension called Mail Form that receives data through a form and sends it to a preconfigured email.
Then in the Chapter , we will revisit the Rails rendering stack and customize it to read templates from a database instead of the filesystem. At the end of the chapter, we will learn how to build faster controllers using Rails 3s modularity.
In the Chapter Well then create new generators and seamlessly integrate them into Rails.
And in the Chapter , we will build a Rails engine that stores all SQL queries executed by our application in a MongoDB database and exposes them for further analysis through a web interface. We will also see how we can use Rubys Thread
and Queue
classes in the Ruby Standard Library to do the asynchronous processing.
In the Chapter , we will study Rails 3s responders and how we can use them to encapsulate controllers behavior, making our controllers simpler and our applications more modular. We will then extend Rails responders to add HTTP Cache and internationalized Flash messages by default. At the end of the chapter, we will learn how to customize Rails scaffold generators for enhanced productivity.
Finally, in the Chapter flexibility to write integration tests for different browsers.
How to Read This Book
Well build a project from scratch in each chapter. Although these projects do not depend on each other, most of the discussions in each chapter depend on what you learned previously. For example, in the Chapter . In other words, you can skip around, but to get the big picture, you should read the chapters in the order they are presented.
Online Resources
The books website has links to an interactive discussion forum as well as errata for the book. Youll also find the source code for all the projects we build. Readers of the ebook can click the gray box above the code excerpts to download that snippet directly.
If you find a mistake, please create an entry on the errata page so we can address it. If you have an electronic copy of this book, there are links in the footer of each page that you can use to easily submit errata to us.