Prev | Current Page 277 | Next

Kevin Marshall, Chad Pytel, and Jon Yurek

"Pro Active Record: Databases with Ruby and Rails"

included method. It??™s called on a module when
that module is included in another class. In this case, when the Auditable module is included
in a class it gives that class access to all the methods in the ClassMethods module. That makes
the audits method available to your models but doesn??™t add any other code until you actually
want it. For example, we can include our Auditable module like so:
class ActiveRecord::Base
include Auditable
end
which will add the audits method to all models. Once we have done that, we can call it like so:
class Cow < ActiveRecord::Base
audits :name, :gallons_per_day, :farmer_id
end
When you call audits inside the class, two things happen: your class then has access to
the methods in the SingletonMethods module (which means, in this case, that the Cow class has
access to methods that other models don??™t), and all of your instances have access to the methods
in the InstanceMethods module (so all of your individual Cow objects have extra functionality).
Now that you know where everything goes, we can start implementing our methods.


Pages:
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289