𝔖 Scriptorium
✦   LIBER   ✦

📁

Rails AntiPatterns: Best Practice Ruby on Rails Refactoring (Addison-Wesley Professional Ruby Series)

✍ Scribed by Chad Pytel, Tammer Saleh


Publisher
Addison-Wesley Professional
Year
2010
Tongue
English
Leaves
341
Edition
1
Category
Library

⬇  Acquire This Volume

No coin nor oath required. For personal study only.

✦ Synopsis


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 redundancyDomain modeling, including schema and database issues such as normalization and serializationView layer tools and conventionsController-layer code, including RESTful codeService-related APIs, including timeouts, exceptions, backgrounding, and response codesThird-party code, including plug-ins and gemsTesting, from test suites to test-driven development processesScaling and deploymentDatabase issues, including migrations and validationsSystem design for “graceful degradation” in the real world

✦ Table of Contents


Contents......Page 7
Foreword......Page 11
Introduction......Page 13
Acknowledgments......Page 17
About the Authors......Page 19
1 Models......Page 21
AntiPattern: Voyeuristic Models......Page 22
Solution: Follow the Law of Demeter......Page 23
Solution: Push All find() Calls into Finders on the Model......Page 27
Solution: Keep Finders on Their Own Model......Page 30
AntiPattern: Fat Models......Page 34
Solution: Delegate Responsibility to New Classes......Page 35
Solution: Make Use of Modules......Page 41
Solution: Reduce the Size of Large Transaction Blocks......Page 44
AntiPattern: Spaghetti SQL......Page 51
Solution: Use Your Active Record Associations and Finders Effectively......Page 52
Solution: Learn and Love the Scope Method......Page 56
Solution: Use a Full-Text Search Engine......Page 62
Solution: Extract into Modules......Page 70
Solution: Write a Plugin......Page 79
Solution: Make Magic Happen with Metaprogramming......Page 84
2 Domain Modeling......Page 93
AntiPattern: Authorization Astronaut......Page 94
Solution: Simplify with Simple Flags......Page 96
Solution: Denormalize into Text Fields......Page 99
Solution: Make Use of Rails Serialization......Page 102
3 Views......Page 109
AntiPattern: PHPitis......Page 111
Solution: Learn About the View Helpers That Come with Rails......Page 112
Solution: Add Useful Accessors to Your Models......Page 118
Solution: Extract into Custom Helpers......Page 120
AntiPattern: Markup Mayhem......Page 127
Solution: Make Use of the Rails Helpers......Page 129
Solution: Use Haml......Page 131
4 Controllers......Page 137
AntiPattern: Homemade Keys......Page 138
Solution: Use Clearance......Page 139
Solution: Use Authlogic......Page 141
Solution: Use Active Record Callbacks and Setters......Page 143
Solution: Move to a Presenter......Page 162
Solution: Store References Instead of Instances......Page 174
Solution: Embrace REST......Page 181
Solution: Refactor Non-RESTful Actions into a Separate Controller......Page 187
AntiPattern: A Lost Child Controller......Page 190
Solution: Make Use of Nested Resources......Page 193
AntiPattern: Rat’s Nest Resources......Page 200
Solution: Use Separate Controllers for Each Nesting......Page 201
AntiPattern: Evil Twin Controllers......Page 204
Solution: Use Rails 3 Responders......Page 206
5 Services......Page 209
Solution: Know What Exceptions to Look Out For......Page 210
Solution: Move the Task to the Background......Page 215
AntiPattern: Pitiful Page Parsing......Page 217
Solution: Use a Gem......Page 218
AntiPattern: Successful Failure......Page 221
Solution: Obey the HTTP Codes......Page 223
Solution: Divide into Confederated Applications......Page 227
6 Using Third-Party Code......Page 231
Solution: Look for a Gem First......Page 233
Solution: Follow TAM......Page 234
Solution: Prune Irrelevant or Unused Gems......Page 236
Solution: Consider Vendored Code Sacrosanct......Page 237
7 Testing......Page 241
AntiPattern: Fixture Blues......Page 243
Solution: Make Use of Factories......Page 245
Solution: Refactor into Contexts......Page 248
AntiPattern: Lost in Isolation......Page 256
Solution: Watch Your Integration Points......Page 258
AntiPattern: Mock Suffocation......Page 260
Solution: Tell, Don’t Ask......Page 261
AntiPattern: Untested Rake......Page 266
Solution: Extract to a Class Method......Page 268
Solution: Write Normal Unit Tests Without Rails......Page 271
Solution: Load Only the Parts of Rails You Need......Page 274
Solution: Break Out the Atom Bomb......Page 279
8 Scaling and Deploying......Page 287
Solution: Build to Scale from the Start......Page 288
Solution: Make Use of the System Directory......Page 291
Solution: Add Indexes......Page 292
Solution: Reassess Your Domain Model......Page 297
Solution: Don’t Do in Ruby What You Can Do in SQL......Page 302
Solution: Move Processing into Background Jobs......Page 306
9 Databases......Page 311
Solution: Never Modify the up Method on a Committed Migration......Page 312
Solution: Never Use External Code in a Migration......Page 313
Solution: Always Provide a down Method in Migrations......Page 315
AntiPattern: Wet Validations......Page 317
Solution: Eschew Constraints in the Database......Page 318
10 Building for Failure......Page 321
Solution: Fail Fast......Page 322
AntiPattern: Inaudible Failures......Page 326
Solution: Never Fail Quietly......Page 327
A......Page 331
C......Page 332
E......Page 333
G......Page 334
K......Page 335
O......Page 336
R......Page 337
S......Page 338
T......Page 339
V......Page 340
Y......Page 341


📜 SIMILAR VOLUMES


Rails AntiPatterns: Best Practice Ruby o
✍ Chad Pytel, Tammer Saleh 📂 Library 📅 2010 🏛 Addison-Wesley 🌐 English

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, ex

Ruby on Rails 3 Tutorial: Learn Rails by
✍ Michael Hartl 📂 Library 📅 2010 🏛 Addison-Wesley Professional 🌐 English

“Ruby on Rails™ 3 Tutorial: Learn Rails by Example by Michael Hartl has become a must read for developers learning how to build Rails apps.” —Peter Cooper, Editor of Ruby Inside   Using Rails 3, developers can build web applications of exceptional elegance and power. Although its remarkable capabili

Addison-Wesley Professional Ruby Series
✍ Adam J. 📂 Library 📅 2006 🌐 English

One of the most powerful aspects of the Rails framework is the ability to extend and override it within your own applications. This is achieved through the Rails plugin mechanism, which provides an avenue for Rails developers to add powerful new features to their applications and share this function

The Rails 3 Way, 2nd Edition (Addison-We
✍ Obie Fernandez 📂 Library 📅 2010 🏛 Addison-Wesley Professional 🌐 English

The Rails™ 3 Way is a comprehensive resource that digs into the new features in Rails 3 and perhaps more importantly, the rationale behind them.—Yehuda Katz, Rails Core The Bible for Ruby on Rails Application Development   Ruby on Rails strips complexity from the development process, enabling profes

Eloquent Ruby (Addison-Wesley Profession
✍ Russ Olsen 📂 Library 📅 2011 🏛 Addison-Wesley Professional 🌐 English

It’s easy to write correct Ruby code, but to gain the fluency needed to write great Ruby code, you must go beyond syntax and absorb the “Ruby way” of thinking and problem solving. In Eloquent Ruby, Russ Olsen helps you write Ruby like true Rubyists do–so you can leverage its immense, surprising powe

Eloquent Ruby (Addison-Wesley Profession
✍ Russ Olsen 📂 Library 📅 2011 🏛 Addison-Wesley Professional 🌐 English

It’s easy to write correct Ruby code, but to gain the fluency needed to write great Ruby code, you must go beyond syntax and absorb the “Ruby way” of thinking and problem solving. In Eloquent Ruby, Russ Olsen helps you write Ruby like true Rubyists do–so you can leverage its immense, surprising powe