Advanced Rails - Building Industrial-Strength Web Apps in Record Time

(Tuis.) #1
Incorporating Rails Components | 287

printf "%-20s%5.2f", client.name, hours
end
end

Other Ruby applications


ActiveRecord can be used as a library in any Ruby application, and it is great for rap-
idly prototyping simple interfaces to a database. The database interface can be self-
contained, which makes it easy to integrate with existing applications; it will coexist
with other libraries such as Ruby-DBI.


The rapid prototyping aspect is key; ActiveRecord provides a consistent interface to
many database management systems, and you can use this interface to abstract away
the database details while building an application. An application can theoretically
be developed on a laptop with SQLite and deployed on a big-iron server running
Oracle (in practice, this is not a perfect transition, but it is somewhat easier than
working with the individual database libraries).


Gregory Brown wrote an article that walks through the process of building a to-do
list console application from the ground up with Ruby and ActiveRecord, without
using code generation. The article is available fromhttp://www.oreillynet.com/pub/a/
ruby/2007/06/21/how-to-build-simple-console-apps-with-ruby-and-activerecord.html.


ActionMailer


Using ActionMailer to send emails from outside of Rails is a simple process as well. It
requires slightly more configuration, but not by much. First, load the framework,
either using RubyGems or a newer Subversion checkout:


# gem version
require 'rubygems'
require 'action_mailer'

# or edge version
require 'vendor/actionmailer/lib/action_mailer'

Next, set the outgoing mail server settings. All settings are optional.


# Default is :smtp; also accepts :sendmail or :test
ActionMailer::Base.delivery_method = :smtp

ActionMailer::Base.server_settings = {
:address => 'localhost',
:port => 25,
:domain => 'example.com', # HELO example.com
:authentication => :cram_md5,
:user_name => 'me',
:password => 'secret'
}

ActionMailer needs a template directory in which to look for email templates:


ActionMailer::Base.template_root = 'views'
Free download pdf