Implementing custom timestamps in AR


#1

I have a requirement to record the last accessed date and time on
several tables. I desire to do this in a local modification to AR using
an initializer. I am looking at timestamp.rb to see how Rails handles
the builtin timestamp attributes, but I am completely lost. Can some
one explain to me what this code is doing?

module ActiveRecord
module Timestamp
def self.included(base) #:nodoc:
base.alias_method_chain :create, :timestamps
base.alias_method_chain :update, :timestamps

  base.class_inheritable_accessor :record_timestamps,
                                  :instance_writer => false
  base.record_timestamps = true
end

private
  def create_with_timestamps #:nodoc:
    if record_timestamps

I can figure out what the private method in timestamp.rb does, and it is
pretty much what I want to do with my custom attributes. What I cannot
figure out is how it is invoked. As far as I can determine, the only
methods defined as :timestamps occur in the abstract connector class
while the attribute record_timestamps occurs, and is referenced, only
within timestamp.rb itself.

My interpretation of this is that the base.class_inheritable_accessor
creates a virtual attribute called record_timestamps, prohibits creation
of a setter, and sets the attribute value to true. But, I have no idea
how or where the create_with_timestamps method is called. And I cannot
fathom what the method_chain is doing since the only timestamps method
that I can find has to do with setting up columns in the schema via
migrations. Finally, I do not see where the configuration value of
config.record_timestamps is used.