The Art of Rails®

Book description

Ruby on Rails offers the ability to rapidly design and write high-quality code with elegance and maintainability. Written by an experienced web application developer, this book explains how to take good Rails code and turn it into beautiful Rails code: simple, effective, reusable, evolvable code. He discusses the very latest in design patterns, programming abstractions, and development methodologies that have emerged for the modern web. With each design technique, you?ll discover how to make your own additions to Rails and craft a development environment tailored to your specific needs.

Table of contents

  1. Copyright
  2. About the Author
  3. Credits
  4. Acknowledgments
  5. Introduction
    1. 0.1. What Is the Art of Rails?
      1. 0.1.1. Development Philosophy of the New Web
      2. 0.1.2. Advanced Tricks and Patterns for MVC Development
      3. 0.1.3. Read-Write Web: APIs, Resources, and REST
      4. 0.1.4. AJAX Patterns
      5. 0.1.5. Advanced Ruby and Meta-programming
      6. 0.1.6. Group Schema Development and Behavior-Driven Development
    2. 0.2. Whom This Book Is For
    3. 0.3. What's Up With the Stories?
    4. 0.4. Conventions
    5. 0.5. Source Code
    6. 0.6. Errata
    7. 0.7. p2p.wrox.com
  6. 1. Emergence(y) of the New Web
    1. 1.1. Rails, Art, and the New Web
      1. 1.1.1. Art and Engineering
      2. 1.1.2. The New Web
      3. 1.1.3. The Truth about Web Applications
    2. 1.2. Patient History: The World Wide Web
      1. 1.2.1. From Documents to Interfaces
      2. 1.2.2. The Decline of Semantics
        1. 1.2.2.1. HTML and the Rise of Pages
        2. 1.2.2.2. HTTP and the Rise of Forms
      3. 1.2.3. Hello, Web Applications
        1. 1.2.3.1. Code-First Web Development
        2. 1.2.3.2. Document-First Web Development
        3. 1.2.3.3. Emergency Room
    3. 1.3. Emergence of the New Web
  7. 2. The Rails Concept
    1. 2.1. One Part Framework
      1. 2.1.1. The Configuration View
      2. 2.1.2. The Code View
      3. 2.1.3. The Process View
    2. 2.2. One Part Language
      1. 2.2.1. Models on Rails
      2. 2.2.2. Web-Specific Tasks
      3. 2.2.3. JavaScript
      4. 2.2.4. Plug-Ins
    3. 2.3. Two Parts Mindset
      1. 2.3.1. Web Sites Are MVC Applications
      2. 2.3.2. Web Applications Are Ecosystems
      3. 2.3.3. Convention over Configuration
      4. 2.3.4. A Little Assumption Goes a Long Way
      5. 2.3.5. Aesthetics Matter
      6. 2.3.6. Restraint Can Be Liberating
      7. 2.3.7. You Repeat Yourself Too Much
        1. 2.3.7.1. Repeated Objects
        2. 2.3.7.2. Repeated Behaviors and Processes
      8. 2.3.8. Testing Isn't a Choice
      9. 2.3.9. The Web Is a Set of Resources, Not Services
    4. 2.4. Summary
      1. 2.4.1. No Silver Bullet
      2. 2.4.2. Optimize Your Coding Before You Optimize Your Code
  8. 3. The Server as an Application
    1. 3.1. Model-View-Controller: The Abbreviated Version
    2. 3.2. MVC and the Web
    3. 3.3. The MVC Design Process
      1. 3.3.1. The View Is Your Spec
      2. 3.3.2. Example: Social Networking for Cooks
        1. 3.3.2.1. Visualizing a Run-Through
        2. 3.3.2.2. The Model
        3. 3.3.2.3. The Static HTML Prototype
        4. 3.3.2.4. The Controllers
        5. 3.3.2.5. The Actions
        6. 3.3.2.6. The Icing
    4. 3.4. Managing the Application Lifecycle
      1. 3.4.1. Think Agile, Not Engineered
      2. 3.4.2. Think Controlled, Not Organic
      3. 3.4.3. Beware of Open-Heart Surgery
    5. 3.5. Summary
  9. 4. Getting the Most from M, V, and C
    1. 4.1. The Best API Documentation Is Free
    2. 4.2. The Model
      1. 4.2.1. Model Objects Should Understand Each Other
      2. 4.2.2. Use Exceptions to Make Code Cleaner
        1. 4.2.2.1. The Flash Variable
        2. 4.2.2.2. Defining Your Own Exceptions
      3. 4.2.3. Mapping from Rows to Objects
      4. 4.2.4. Polymorphic Associations
      5. 4.2.5. The World Outside of ActiveRecord
    3. 4.3. The View
      1. 4.3.1. The Variable Problem
      2. 4.3.2. Rails-Style JavaScript
      3. 4.3.3. Partials as Atoms and Molecules
      4. 4.3.4. Picking the Proper Partials
        1. 4.3.4.1. The Full Profile
        2. 4.3.4.2. The Form
        3. 4.3.4.3. The Basic Search Form
        4. 4.3.4.4. The Extended Search Form
        5. 4.3.4.5. The Row Summary
        6. 4.3.4.6. The Badge Summary
        7. 4.3.4.7. The Inline/Sparkline Reference
      5. 4.3.5. The View Isn't Just HTML
    4. 4.4. The Controller
      1. 4.4.1. Reusable CRUD
      2. 4.4.2. Rails Scaffolding Is a Bunch of CRUD
      3. 4.4.3. Dealing with Two-Step Actions
        1. 4.4.3.1. Keeping Forms and Their Processing Together
        2. 4.4.3.2. Keeping Object Mutation and Object Display Separate
      4. 4.4.4. Knowing When to Outsource
        1. 4.4.4.1. Pulling Out the Housekeeping with Filters
        2. 4.4.4.2. Pulling Out Data-Related Operations with Model Objects
          1. 4.4.4.2.1. Loading Data from Parameters
          2. 4.4.4.2.2. Security
        3. 4.4.4.3. Validations
      5. 4.4.5. Knowing When to Refactor
    5. 4.5. Summary
  10. 5. Beautiful Web APIs
    1. 5.1. Two Big Ideas for Web APIs
      1. 5.1.1. The New URL: Addressing Concepts, Not Files
      2. 5.1.2. The Application Is the API
    2. 5.2. Routing
    3. 5.3. Anatomy of the Web API Call
    4. 5.4. Overlaying the API
      1. 5.4.1. The respond_to Method
      2. 5.4.2. Writing a Non-HTML Result
        1. 5.4.2.1. XML
        2. 5.4.2.2. RSS
        3. 5.4.2.3. RDF
    5. 5.5. Adding Custom MIME Types
      1. 5.5.1. Registering Types with Rails
      2. 5.5.2. Creating Your Own MIME Type
    6. 5.6. API-Metering, the Rails Way
      1. 5.6.1. Authenticating the User
      2. 5.6.2. The Metering Algorithm
      3. 5.6.3. Applying Metering via Filters
    7. 5.7. What about SOAP/XML-RPC Services?
      1. 5.7.1.
        1. 5.7.1.1. Service API
        2. 5.7.1.2. Service Implementation
        3. 5.7.1.3. Creating Structs
    8. 5.8. Summary
  11. 6. Resources and REST
    1. 6.1. A Web of Resources
      1. 6.1.1. Identifying Resources
      2. 6.1.2. Talking About Resources
      3. 6.1.3. Representing Resources
    2. 6.2. Representational State Transfer
      1. 6.2.1. HTTP: The Resource CRUD
        1. 6.2.1.1. RESTful Endpoints
        2. 6.2.1.2. Backward Compatibility Workaround
      2. 6.2.2. Defining an Application in Terms of Resources
        1. 6.2.2.1. RESTful Routing: Mapping HTTP Commands into the Controller
        2. 6.2.2.2. Resources Lead to Simple Endpoints
        3. 6.2.2.3. An Example of REST-Based refactoring
      3. 6.2.3. Communicating with the Client: Resources as Your API
      4. 6.2.4. Put Another Way: The Network Is the Computer
    3. 6.3. REST and Rails
      1. 6.3.1. Mapping Resources in the Router
      2. 6.3.2. But It's the Real World: Named Routes are Still Needed
        1. 6.3.2.1. Automatically Provided Routes
        2. 6.3.2.2. Extra Route Shorthand
        3. 6.3.2.3. REST and URL Helpers
      3. 6.3.3. Resource Scaffolds
      4. 6.3.4. Nested Resources
        1. 6.3.4.1. Creating a Nested Route
        2. 6.3.4.2. Deep Nesting
        3. 6.3.4.3. Writing a Nested Resource Controller
        4. 6.3.4.4. Hiding Your Primary Keys
      5. 6.3.5. Singleton Resources versus Regular Resources
    4. 6.4. Summary
  12. 7. The Five Styles of AJAX
    1. 7.1. The Big Secrets
      1. 7.1.1. AJAX Isn't Necessarily the Hard Part
      2. 7.1.2. AJAX Introduces Tough Design Issues
      3. 7.1.3. You Have Your Pick of JavaScript Frameworks, Even in Rails
    2. 7.2. The Five Styles of AJAX
      1. 7.2.1. Proxy Style
        1. 7.2.1.1. Buddy List
        2. 7.2.1.2. Points to Ponder
        3. 7.2.1.3. Chatting with meebo
        4. 7.2.1.4. The Real meebo
      2. 7.2.2. Partial Style
        1. 7.2.2.1. Buddy List
        2. 7.2.2.2. Points to Ponder
        3. 7.2.2.3. Chatting with meebo
      3. 7.2.3. Puppet Style
        1. 7.2.3.1. Buddy List
        2. 7.2.3.2. Points to Ponder
        3. 7.2.3.3. Chatting with meebo
      4. 7.2.4. Compiled-to-Web Style
        1. 7.2.4.1. Buddy List
        2. 7.2.4.2. Points to Ponder
        3. 7.2.4.3. Chatting with meebo
      5. 7.2.5. In-Place Application Style
        1. 7.2.5.1. Buddy List
        2. 7.2.5.2. Points to Ponder
        3. 7.2.5.3. Chatting with meebo
    3. 7.3. AJAX as Just Another API
    4. 7.4. Rails-Style AJAX
      1. 7.4.1. Partial-Style AJAX Controllers (and AJAX CRUD)
        1. 7.4.1.1. CRUDy AJAX
        2. 7.4.1.2. Learning to Use "Inner Layouts"
      2. 7.4.2. Puppet-Style AJAX Controllers (and RJS)
        1. 7.4.2.1. RJS: A Different Kind of View
        2. 7.4.2.2. How RJS Works
    5. 7.5. Elegant Degradation
      1. 7.5.1. Moving Backward from the Partial-Style
        1. 7.5.1.1. Backward-Compatible Links
        2. 7.5.1.2. Backward-Compatible Superlinks
      2. 7.5.2. Moving Backward from Rich User Interfaces
    6. 7.6. Summary
  13. 8. Playing with Blocks
    1. 8.1. The Block Mindset
    2. 8.2. Comparing Methods, Procs, and Blocks
      1. 8.2.1. Methods
        1. 8.2.1.1. Method Scope
        2. 8.2.1.2. The Message Paradigm
      2. 8.2.2. Procs
      3. 8.2.3. Blocks
      4. 8.2.4. Moving Between Blocks and Procs
        1. 8.2.4.1. From Block to Proc
        2. 8.2.4.2. From Proc to Block
    3. 8.3. The Big Scope Experiment
      1. 8.3.1. Experiment 1: Blocks Are Affected by Changes in Their Source Environment
      2. 8.3.2. Experiment 2: Blocks Can Affect the Environment from Which They Came
    4. 8.4. Block Patterns, Blocks in Rails
      1. 8.4.1. Iteration
        1. 8.4.1.1. Defining an Iterator Method
      2. 8.4.2. Aspect-Oriented Programming
        1. 8.4.2.1. Benchmarking
        2. 8.4.2.2. Filters
        3. 8.4.2.3. ActiveRecord Transactions
        4. 8.4.2.4. ActiveRecord Scope
      3. 8.4.3. Building Output in HTML and XML
        1. 8.4.3.1. Blocks for HTML
        2. 8.4.3.2. Blocks for XML
      4. 8.4.4. Dual-Use Functions
      5. 8.4.5. The Callback
    5. 8.5. Summary
  14. 9. Mixins and Monkey Patching
    1. 9.1. Mixins
      1. 9.1.1. Organizing Code into Modules
      2. 9.1.2. Methods in Modules
      3. 9.1.3. Mixing Modules into Classes
        1. 9.1.3.1. What Gets Mixed In?
          1. 9.1.3.1.1. Mixins with include
          2. 9.1.3.1.2. Mixins with extend
        2. 9.1.3.2. Do You Walk Like a Duck?
      4. 9.1.4. Mixins in Rails
        1. 9.1.4.1. The Rails Source
        2. 9.1.4.2. Your Rails Code
          1. 9.1.4.2.1. Example 1: Geolocatable Model Objects
          2. 9.1.4.2.2. Example 2: A Yahoo! UI Helper
    2. 9.2. Monkey Patching
      1. 9.2.1. Eval: The Interpreter's Back Door
      2. 9.2.2. Eval's Two Siblings
        1. 9.2.2.1. class_eval
        2. 9.2.2.2. class Class ; end
        3. 9.2.2.3. instance_eval
          1. 9.2.2.3.1. Executing instance_eval on a Class
          2. 9.2.2.3.2. Executing instance_eval on an Instance Variable
        4. 9.2.2.4. class ⪌ Class
        5. 9.2.2.5. class_eval and instance_eval Compared
      3. 9.2.3. Good Monkey Patching Technique
        1. 9.2.3.1. When to Monkey Patch
        2. 9.2.3.2. Be Aware of the Hazards
        3. 9.2.3.3. Monkey Patching Is a Temporary Solution
        4. 9.2.3.4. "Backing Up" Methods before You Patch Them
    3. 9.3. Summary
  15. 10. Code That Writes Code (That Writes Code)
    1. 10.1. Dynamic Code and DSLs Revisited
    2. 10.2. Code-Writing Macros
      1. 10.2.1. Creating Methods on the Fly with define_method
      2. 10.2.2. define_method Example: The Pentagon and the Kremlin
      3. 10.2.3. Scope and define_method
      4. 10.2.4. Using define_method for Rails Macros
      5. 10.2.5. Macro Summary
    3. 10.3. Calling Methods That Don't Exist: Objects That Adapt to the Way You Use Them
      1. 10.3.1. Some Basic Examples
      2. 10.3.2. Example: A Shortcut for Array.each
      3. 10.3.3. Beware of Catching Everything
      4. 10.3.4. method_missing Patterns
      5. 10.3.5. Implementing method_missing Patterns
        1. 10.3.5.1. Facades
        2. 10.3.5.2. Adding Filter Support to the Wrapper
        3. 10.3.5.3. Data-Driven Objects: Building a Language Translator
        4. 10.3.5.4. Creative APIs (The XML Builder)
    4. 10.4. Reflection
      1. 10.4.1. Variables and Constants
      2. 10.4.2. Methods
      3. 10.4.3. Modules
    5. 10.5. Summary
  16. 11. How I Learned to Stop Worrying and Love the Schema
    1. 11.1. Bringing the Database into the Picture: The LAMP Stack
    2. 11.2. Thinking in Migrations
      1. 11.2.1. Writing Migrations
      2. 11.2.2. Performing Schema Migrations
    3. 11.3. Team Schema Development
    4. 11.4. Seeding Data for Production
      1. 11.4.1. Small Datasets: Seed Migrations
      2. 11.4.2. Medium Datasets: Seed Fixtures
      3. 11.4.3. Large Datasets: Dumpfiles
    5. 11.5. When a Database Isn't Enough
      1. 11.5.1. Model Object Hierarchies
        1. 11.5.1.1. Differing Fields from Subclass to Subclass
        2. 11.5.1.2. Differing Helper Methods from Subclass to Subclass
          1. 11.5.1.2.1. Refining Method Implementations
          2. 11.5.1.2.2. Subclass-Specific Methods
      2. 11.5.2. Storing Lists, Hashes, and Other Fun Things
      3. 11.5.3. Custom Getters and Setters
    6. 11.6. Summary
  17. 12. Behavior-Driven Development and RSpec
    1. 12.1. Behavior-Driven Development
    2. 12.2. RSpec: BDD for Ruby and Rails
      1. 12.2.1. The Spec Development Cycle
      2. 12.2.2. Writing the Spec
      3. 12.2.3. Implementing Examples
      4. 12.2.4. Matchers
      5. 12.2.5. Custom Matchers
      6. 12.2.6. Before and After
    3. 12.3. An Example Trip through the Development Cycle
      1. 12.3.1. Part 1: Writing The Story
      2. 12.3.2. Part 2: Writing the Specs
      3. 12.3.3. Part 3: Initializing and Writing a Basic Test
      4. 12.3.4. Part 4: Writing Behavior Tests That Motivate Development
      5. 12.3.5. Part 5: Completing the Behavioral Test Implementations
    4. 12.4. But Wait, There's More
    5. 12.5. Summary

Product information

  • Title: The Art of Rails®
  • Author(s): Edward Benson
  • Release date: May 2008
  • Publisher(s): Wrox
  • ISBN: 9780470189481