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

(Tuis.) #1
271

Chapter 9 CHAPTER 9


Incorporating and Extending Rails 9


The best way to predict the future is to invent it.
—Alan Kay

Ruby on Rails was designed as a loosely coupled set of components (ActionPack,
ActiveRecord, ActiveResource, ActiveSupport, and ActionMailer) with some glue to
hold them together (RailTies). Although Rails is typically used as a framework (an
environment specialized to programming web applications), the components of Rails
can be replaced with other components more suitable to a project. Alternatively, the
components can be broken out and used apart from the rest of Rails. In this chapter,
we will see how these techniques can be used for maximum flexibility in application
development.


Replacing Rails Components

Replacing ActiveRecord


ActiveRecord, the Rails object-relational mapper, is one of the best-known parts of
the Rails framework. But it represents one of many valid ways to map objects to a
database. Martin Fowler identified and defined the Active Record pattern, along with
other data-source patterns, in his bookPatterns of Enterprise Application Architec-
ture. (The Active Record pattern should not be confused with the ActiveRecord
library, which is based on that pattern.) Several Ruby libraries have been developed
based on other patterns. We will look at DataMapper, based on the pattern of the
same name. We will also examine Ambition, an off-the-wall experimental library
that maps Ruby statements directly to SQL.


If you are not using ActiveRecord in a Rails application, you can disable it by remov-
ing it fromconfig.frameworks inconfig/environment.rb:


config.frameworks -= [ :active_record ]
Free download pdf