O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Ruby on Rails Enterprise Application Development

Book Description

Building a complete Ruby on Rails business application from start to finish

  • Create a non-trivial, business-focused Rails application

  • Solve the real-world problems of developing and deploying Rails applications in a business environment

  • Apply the principles behind Rails development to practical real-world situations

In Detail

All businesses have processes that can be automated via computer applications, thereby reducing costs and simplifying everyday operations. This book demonstrates that a modern web application framework makes an ideal platform for such applications. It shows how the attributes that make the Rails framework so successful for Internet applications also provide great benefit within a business intranet. These attributes include easy roll-out and update of applications, centralized processing and data handling, simple maintenance, straightforward code development, and scalability.

Ruby on Rails is an open-source web application framework ideally suited to building business applications, accelerating and simplifying the creation of database-driven websites. Often shortened to Rails or RoR, it provides a stack of tools to rapidly build web applications based on the Model-View-Controller design pattern.

This book covers topics such as installing Ruby, Rubygems, and Rails on Windows, Linux, and Mac OS X; choosing and installing a database; installing an IDE for Rails development; setting up a Subversion repository to manage your code; creating a new Rails application; understanding Rails models; understanding controllers and views; improving user interfaces with Ajax; using Rails plugins to manage file uploads; using Capistrano to manage application deployment; techniques for scaling Rails applications, such as caching and using Apache to proxy through to the Mongrel server. The example application is straightforward to develop, easy to roll out, and simple to maintain.

Table of Contents

  1. Ruby on Rails Enterprise Application Development
    1. Table of Contents
    2. Ruby on Rails Enterprise Application Development
    3. Credits
    4. About the Authors
    5. Preface
      1. What This Book Covers
      2. What You Need for This Book
      3. Who is This Book for
      4. Conventions
      5. Reader Feedback
      6. Customer Support
        1. Downloading the Example Code for the Book
        2. Errata
        3. Questions
    6. 1. Introduction
      1. Why this Book?
      2. Why Develop?
      3. Why a Client/Server based Web Application?
      4. But why Ruby on Rails?
        1. Rails Handles Menial Tasks
        2. Clear Code
        3. Text Based File
        4. Open Source
        5. Plentiful Documentation
        6. Built-in Safe Test Environment
      5. Ruby on Rails in Detail
      6. Summary
    7. 2. The Initial Problem
      1. A Normal Day in the Office
      2. Examining the Data
      3. Data Objects
        1. Database Table Design Rules
      4. Separating the Data
      5. Naming Conventions
        1. Use Meaningful Names
        2. Use a Consistent Naming Convention
        3. Ruby on Rails Naming Conventions
          1. Constants and Classes
          2. Variables
          3. Methods and Properties
          4. Special Method and Property Suffixes
          5. Reserved Words
      6. Back to the Data
      7. Review the Result
        1. Project Preparation Steps
      8. How Good is the Source Data?
      9. Tracking Who does What
        1. No Log-On and No Authentication
        2. Simple Password Access
        3. User Log-On
        4. Recording Access History
        5. Access Control for Rory's Application
      10. Data Validation
        1. The Minimum Required Data is Entered
        2. Each Record can be Uniquely Identified
        3. Identify Fields that Need to Have a Particular Format
        4. References to Data in Other Tables Point to Actual Data
      11. Rory's Data
        1. Person
        2. Company
        3. Address
      12. Summary
    8. 3. Laying the Foundations
      1. Supporting Rails Development
      2. Addressing the Challenges
      3. Setting Up a Rails Stack
        1. Installing a Rails Stack Using a Bundle
      4. Installing a Custom Rails Stack
        1. Installing Ruby and Rubygems
          1. Ruby on Windows
          2. Ruby on Linux
          3. Ruby on Mac OS X
        2. Installing Rails
          1. A Note on Rails Documentation
        3. Other Libraries
          1. Capistrano for Easier Deployment
          2. Mongrel: A Better Way to Run Rails Applications
        4. Choosing a Database Platform
          1. Installing MySQL
            1. MySQL on Windows
            2. MySQL on Linux
            3. MySQL on Mac OS X
          2. Checking Your MySQL Installation
          3. MySQL GUI Tools
          4. Ruby-MySQL: Making Ruby and MySQL Work Better Together
            1. Ruby-MySQL on Linux
            2. Ruby-MySQL on Mac OS X
        5. Installing an IDE
          1. Eclipse
            1. Pros and Cons of Eclipse
          2. EasyEclipse
            1. Installing EasyEclipse on Windows
            2. Installing EasyEclipse on Linux
            3. Installing EasyEclipse on Mac OS X
        6. Instructions for Masochists
        7. In the Back Rooms at Acme…
      5. Setting Up a Team Server
          1. Quick Gem Installation
        1. Remote Access via SSH
          1. Adding Users
        2. Version Control with Subversion
          1. Installing Subversion
          2. Subversion Standard Practices
            1. Revisions and Working Copies
          3. Setting Up a Subversion Repository
          4. Setting Up a Project in Subversion
          5. Browsing Subversion from Eclipse
          6. Other Subversion Clients
        3. Using Other People's Servers
        4. Back at Acme
      6. Summary
    9. 4. Working with Rails
      1. The World According to Rails
        1. Model-View-Controller Architecture
        2. Convention over Configuration
          1. Rails and MVC
      2. Setting Up a New Rails Application
        1. Using Mongrel to Serve Your Application
      3. Connecting Rails to a Database
        1. Creating a Database and System Account
        2. Setting Up a Database Connection
          1. Configuring the Rails Environments
          2. Testing the Database Connection
          3. Troubleshooting a MySQL Connection
      4. ActiveRecord, Migrations, and Models
        1. Model == Table
        2. Which Comes First: The Model or The Table?
        3. Building a Model with Migrations
          1. Converting a Data Structure into a Migration
          2. Defining Columns in Migrations
          3. Other Operations Available in a Migration
        4. Running a Migration
        5. Rolling Back to a Previous Version of the Database
      5. The Scaffold
      6. Completing the Database
        1. The companies Table
        2. The addresses Table
        3. Generating the Remaining Tables
      7. Models in Detail
        1. Creating New Records in a Table via Models
        2. Finders
          1. Finding All of the Records in a Table
          2. Virtual Attributes
          3. Sorting Records
          4. Finding a Single Record
          5. Finding Records Matching Search Criteria
          6. Finding Records Using Attribute-Based Finders
          7. Finding Records by Raw SQL
          8. Writing a Custom Finder
          9. Viewing the SQL
          10. Viewing Logs in Eclipse
        3. Validation
          1. Validating People
            1. Checking for Empty Field Values
            2. Checking Against a Regular Expression
            3. Checking for Uniqueness
            4. Checking for Inclusion in a Range of Values
            5. Validating Related Records
            6. Summary
          2. Validating Companies
          3. Validating Addresses
          4. Other Types of Validation
        4. Testing
          1. Setting Up for Testing
          2. Anatomy of a Test Case
          3. What Should be Tested?
          4. Fixtures
            1. Transactional and Instantiated Fixtures
          5. Tests for the Person Model
            1. A person should have a valid email address
            2. No two People can have the Same email address
            3. A person without a first name is invalid
            4. A person without a last name is invalid
            5. A person's gender must be set to 'M' or 'F'
            6. The full_name method should produce a correctly-formatted string
            7. The find_all_ordered method should correctly sort people
          6. Other Types of Assertion
          7. Becoming Driven by Testing
        5. Associations between Models
          1. Parent to children (one-to-many): addresses to people
            1. Validating a Person's Address
          2. Parent to child (one-to-one): addresses to companies
            1. Validating a Company's Address
          3. Parent to children (one-to-many): companies to people
            1. Validating a Person's Company
          4. Many-to-many relationships
          5. Dependencies
          6. Testing Associations
      8. Putting the Project into Context
        1. Storing a Project in Subversion
          1. Ignoring Temporary Files
          2. Committing Code to the Repository
        2. Processing Data
          1. Exporting the Data from Outlook
          2. Mapping a Text File to Database Tables
          3. Coding the Script
      9. Summary
    10. 5. Building the User Interface
      1. Controllers and Views: A Recap
      2. Creating a Simple Controller and Its Views
        1. Views and Layouts
          1. Adding a View to the Application
          2. Displaying Model Instances in a View
          3. Pagination
          4. Linking to Another View
          5. Adding a Layout
            1. Page Titles in Layouts
          6. Adding a Stylesheet
      3. Adding a Controller for Companies
        1. Create the CompaniesController
        2. Create the Index View
        3. Test It!
        4. Summary
      4. Advanced View Techniques
        1. Custom Helpers
          1. Default Messages for Empty Fields
          2. Date Formatting
        2. Showing Associated Records
          1. Refining Using a Helper
          2. Showing an Address with a Partial
        3. Rendering Pagination Links with a Partial
        4. Adding a Menu
      5. C*UD (Create, Update, Delete)
        1. Creating a Person
          1. Refining with a Helper
          2. Validation Errors
          3. The Flash
          4. Finishing Touches
        2. Updating a Person
        3. Opportunities for Refactoring
          1. Using Filters
          2. Creating Application-Level Controller Methods
        4. Deleting a Person
        5. Adding Edit and Delete Links to a Person's Profile
      6. Editing Multiple Models Simultaneously
        1. Adding a New Address for a Person
          1. Using Functional Testing for Complex Actions
        2. Updating a Person and Their Address
        3. Summary
      7. Fleshing Out Companies and Addresses
        1. Managing Companies
          1. Stubbing Out the Navigation
          2. A Shared View to Confirm Deletions
          3. Attaching a Person to a Company
          4. Creating and Updating Companies
        2. Managing Addresses
          1. Adding a Callback to Company Deletions
            1. Unit Testing for Callbacks
          2. A Very Quick Interface for Addresses
      8. Summary
    11. 6. Into Production
      1. An Application Ready for Production
      2. The Application Server
        1. Memory
        2. Central Processor Unit CPU
        3. Hard Disks
        4. Network Interface Card NIC
        5. Don't Forget Backup
        6. Your First Production Server
      3. Setting up the Server
        1. Installing Ruby and Rails
        2. Copying the Files to the Server
        3. Using Subversion to Transfer the Application to the Production Environment
        4. Excluding Files from the Repository
        5. The Production Database
        6. Separating Development and Production Databases
          1. Localhost database single database.yml
          2. Separate Development and Production database.yml files
          3. Using Migration in Production
          4. The Rails Database User
        7. The Web Server
          1. Mongrel
          2. Mongrel Service on Windows
          3. Limitations of Mongrel
          4. Mongrel behind Apache
          5. Installing Apache
          6. Apache on Linux and Mac OS X
          7. Apache on Windows
          8. Domain Name System (DNS)
          9. Configuring Apache to Act as a Proxy for a Rails Application
      4. Rory's Production Installation
        1. Using Two Host Names to Simplify Routing
        2. Rory Puts his Intranet Application into Production
      5. Errors in Production
        1. Slow List Rendering due to Placement of Additional Data Processing in Loop
          1. Symptom
          2. Cause
          3. Fix
        2. Application Error Following the Transferring of New Code to Production
          1. Symptom
          2. Cause
          3. Solution
      6. Back Up Rails
        1. Backing Up the Code Repository
        2. Back Up the Database
        3. Combining Your Backup Scripts
      7. Summary
    12. 7. Improving the User Experience
      1. Easy Access to the Application
        1. Use Routes to Simplify the Entry Point URL
        2. Build a Fast, Clear Home Page
      2. Users Need to be Able to Find Items Easily
        1. Use the Index View as the Core of the Search View
        2. Search The First Attempt
        3. Do Not Trust User Input
        4. Handle Nothing
        5. Users Need to be Able to Search Without Knowing Exactly What They Are Looking for
        6. A Less Specific Search
        7. Case Insensitive Searches
      3. Adding AJAX to the Mix
        1. Make the AJAX Libraries Available to our Rails Application
          1. Enhancing Search with Auto-complete
          2. Auto-complete—Wow!, but...
        2. Use of AJAX—the Lessons Learned from Auto-Complete
      4. Show and Hide Company Address Using link_to_remote
        1. A Simple link_to_remote
          1. A DOM Object to Update
          2. Create a say_hello Action
        2. Increasing the Functionality of link_to_remote
          1. Show and Hide
          2. Alternating link_to_remote Elements
          3. Alternative Actions
          4. Debugging JavaScript
          5. Show/Hide within the Company Index List
        3. Using AJAX to Edit a Field in Line
          1. Render an AJAX Form via link_to_remote
        4. A Little script.aculo.us: Drag and Drop
          1. Make an Element Draggable
          2. A Place to Drop the Element
        5. Further AJAX
      5. Help!
        1. RDoc Documentation for the Developer
        2. Help for the User
          1. Instiki Wiki Help
          2. The Best User Help Systems
      6. Keep Talking to Users
      7. Summary
    13. 8. Extending the Application
      1. Dealing with User Feedback
      2. Adding a Search Facility
      3. Handling Errors
        1. Catching Missing Record Errors
        2. Catching UnknownAction and Routing Errors
        3. Catching General Application-Level Errors
        4. Catching "Rails has Fallen Over" Errors
      4. Adding an Authentication System
        1. Cookies and Sessions in Rails
        2. Building the Authentication System
          1. The User Model
          2. Displaying the Login Form
          3. Checking Submitted Credentials
          4. Logging Out
          5. Protecting Actions
      5. Adding Simple Task Tracking
        1. The Task Model
        2. The Tasks Controller
        3. Task Views
        4. Showing Tasks for a Person
        5. Redirecting to a Person after Adding or Editing a Task
        6. Redirecting after a Deletion
        7. Handling the Cancel Link
        8. Setting a Default Person for a New Task
        9. Summary
      6. Uploading and Attaching Files
        1. Using Plugins
        2. Using acts_as_attachment for File Uploads
        3. Managing File Attachments for a Task
          1. Adding a Form for Attaching a File to a Task
          2. Adding a File Attachment to a Task
          3. Listing File Attachments for a Task
          4. Deleting File Attachments for a Task
          5. Protecting File Attachment Actions
      7. Summary
    14. 9. Advanced Deployment
      1. Deployment with Capistrano
        1. Getting Started with Capistrano
          1. A Complete Deployment Recipe
        2. Preparing the Production Database
        3. First Deployment
        4. Migrating the Production Database
        5. Running Other Commands on the Server with invoke
        6. Managing Mongrel from Capistrano
        7. Centralizing File Uploads
          1. Upgrading the Application
          2. Cleaning Up Obsolete Releases
        8. Downgrading the Application
      2. Troubleshooting Deployment
        1. Incompatible Rails Versions
        2. Missing Libraries
        3. Incorrect Subversion Password or Repository Permissions
        4. User Doesn't Have SSH Access to the Server
        5. Inaccessible Application Server
        6. Inaccessible Database Server
        7. Dealing with the Inexplicable
        8. Getting Back to a Clean Slate
      3. Housekeeping
        1. Starting Mongrel Automatically
        2. Clearing Out Stale Sessions
        3. Keeping Log Files Manageable
          1. Reducing Log Detail
      4. Optimizing a Rails Application
        1. Finding Bottlenecks
          1. Controller Action Profiling Using around_filter
          2. Profiling Everything
          3. The Rails Profiler
        2. Improving Application Performance with Caching
          1. How Cache Elements are Named
          2. Deciding What to Cache
          3. Preparing for Caching
          4. Page Caching
          5. Action Caching
          6. Fragment Caching
          7. Fragment Caching for Actions
          8. Avoiding Database Calls for Cached Fragments
          9. Clearing out the Cache
        3. Optimizing How Rails Uses the Database
          1. Ordering for Eager Loading
        4. Scaling Your Rails Infrastructure
          1. Using Apache to Serve Static Assets
            1. Tweaking Our Basic mod_proxy Configuration
            2. Adding Rewrite Rules to Serve Static Files
          2. Proxying to a Mongrel Cluster
            1. Setting up the Mongrel Cluster
            2. Load Balancing from Apache to the Mongrel Cluster
        5. Advanced Scaling
      5. Summary
    15. 10. Down the Track
      1. Going off the Rails
      2. SQL
        1. Gathering Data from a Daughter Object's Daughter
          1. Using a model’s ActiveRecord connection
          2. Using GROUP BY to Summarize Data
          3. A Deeper Look at Aggregate Functions
      3. Business Processes
        1. To Be Successful, Build Successful Business Applications
          1. Automate Simple Repetitive Jobs
          2. Rapid and Detailed Reporting
          3. Ensure Customers Pay for the Goods and Services that the Business Provides
          4. Review of Business Activity Examples
        2. Dealing with Success
          1. Just Because You Can, Doesn't Mean You Should
          2. Bought in solutions Provide their Own Opportunities
          3. Ensure There is Time to Complete Each Task
      4. The Final Destination
    16. A. Running Your Own Gem Server
      1. Serving Installed Gems
        1. Setting Your Gem Server as the Default
      2. Creating a Gem Server Manually
    17. Index