Rails AntiPatterns: Best Practice Ruby on Rails Refactoring

Front Cover
Addison-Wesley Professional, Nov 9, 2010 - Computers - 336 pages

The Complete Guide to Avoiding and Fixing Common Rails 3 Code and Design Problems

As developers worldwide have adopted the powerful Ruby on Rails web framework, many have fallen victim to common mistakes that reduce code quality, performance, reliability, stability, scalability, and maintainability. Rails™ AntiPatterns identifies these widespread Rails code and design problems, explains why they’re bad and why they happen—and shows exactly what to do instead.

The book is organized into concise, modular chapters—each outlines a single common AntiPattern and offers detailed, cookbook-style code solutions that were previously difficult or impossible to find. Leading Rails developers Chad Pytel and Tammer Saleh also offer specific guidance for refactoring existing bad code or design to reflect sound object-oriented principles and established Rails best practices. With their help, developers, architects, and testers can dramatically improve new and existing applications, avoid future problems, and establish superior Rails coding standards throughout their organizations.

This book will help you understand, avoid, and solve problems with

  • Model layer code, from general object-oriented programming violations to complex SQL and excessive redundancy
  • Domain modeling, including schema and database issues such as normalization and serialization
  • View layer tools and conventions
  • Controller-layer code, including RESTful code
  • Service-related APIs, including timeouts, exceptions, backgrounding, and response codes
  • Third-party code, including plug-ins and gems
  • Testing, from test suites to test-driven development processes
  • Scaling and deployment
  • Database issues, including migrations and validations
  • System design for “graceful degradation” in the real world
 

What people are saying - Write a review

User Review - Flag as inappropriate

Rails Anti-patterns shifts the focus beyond the basic mechanisms of the Rails framework, and instead explores Rails in terms of common problems a developer may encounter and the generally-accepted "Rails" solutions.
While other intro to mid-level Rails books I’ve read touch on testing and walk through unit test or Rspec, they still failed to convey many complicated concepts behind testing in general. Rails Anti-Patterns answered a lot of the questions about testing I still had after reading others, such as how to model test data, or how to appropriately use mocks and stub methods. Ruby reaches far beyond Rails, and the author makes a point to explain how to test outside of the framework. Such a testing mentality will do wonders for the aspiring rubyist.
The section on ruby models provided the most beneficial information about re-thinking how to write a rails app. You learn to refactor code duplication across multiple models into a module, and this practice works usefully to clarify the behavior of a large single model as well. The authors show how to refactor similar methods like traditional getters, setters and scopes using dynamic method definition and metaprogramming. The magic behind these patterns makes ruby a powerful tool, and the book
It continues to emphasize the use of the scope method, and of refactoring unstructured, php-like code into Rails-esque data structures with separation into single-purpose methods and classes. It continually stresses the importance of maintaining the single responsibility principle, a very helpful rule that helps clarify and strengthen the code.
Later they write strongly in favor of using the classic design pattern of factories instead of more brittle fixures. They introduce the wonderful Factory Girl and they explain how to get started writing factories with it. Finally, a well-deserved tirade against silent failures. Never let an exception slip through undetected. You always want to collect as much data as possible about your system, especially about failures, so fail loudly!
Overall Rails Anti-Patterns not only contributed a lot to both my Rails knowledge but increased my passion for the ruby language along the way. This book will improve the way you approach code design and problem solving, I highly recommend it.
For additional material on testing and patterns, the authors suggest “Simple Smalltalk Testing: with Patterns” by Kent Beck.
 

Contents

1 Models
1
2 Domain Modeling
73
3 Views
89
4 Controllers
117
5 Services
189
6 Using ThirdParty Code
211
7 Testing
221
8 Scaling and Deploying
267
9 Databases
291
10 Building for Failure
301
Index
311
Copyright

Other editions - View all

Common terms and phrases

About the author (2010)

Chad Pytel is the founder and CEO of thoughtbot, a software development firm specializing in Ruby on Rails, and creators of Paperclip, Shoulda, FactoryGirl, and Hoptoad, among other projects. thoughtbot embraces both agile development methodologies and a “getting real” project philosophy. Chad coauthored Pro Active Record: Databases with Ruby and Rails (Apress, 2007) and has presented at various conferences around the world. To follow along with Chad and the rest of the thoughtbot team’s ideas on development, design, technology, and business, visit their blog at http://robots.thoughtbot.com.

Tammer Saleh is the director of engineering at Engine Yard. He wrote the Shoulda testing framework, was the primary developer and project manager for thoughtbot’s fantastic Hoptoad service, and is an experienced Ruby on Rails trainer and speaker. In previous lives, he’s done AI development for the NCSA and the University of Illinois, as well as systems administration for both Citysearch.com and Caltech’s Earthquake Detection Network. You can find him online at http://tammersaleh.com.

Bibliographic information