Prev | Current Page 253 | Next

Kevin Marshall, Chad Pytel, and Jon Yurek

"Pro Active Record: Databases with Ruby and Rails"


Extending Active Record
One of the most wondrous parts of Active Record is the fact that it was built using Ruby. As
a result, any part of it can be modified, replaced, tinkered with, and enhanced. Ruby classes are
extremely malleable, so it??™s very easy to change the behavior of classes even while your application
is running. Where code writes other code, this practice is called metaprogramming. You can
use it to add declarations (the same way has_many, validates_presence_of, and acts_as_list
do), or you can use it to modify how pieces of Active Record work.
According to DHH the original code for the plug-in system for Ruby on Rails was written
in five lines. It??™s grown a bit since then, but the basis of it is still grounded around the principle
that Ruby classes and objects are always open to modification. Since you can inject your own
code wherever you want, the plug-in system doesn??™t have to worry about making sure there
are hooks for you; it can just get on with business.
?– Note While Rails has the concept of plug-ins, they only work automatically with Rails as a whole.


Pages:
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265