aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/mixins/touch.rb
blob: 74a1737caba83b60764852a774268b72ff9d0b26 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
module ActiveRecord
  module Mixins
    # Including this mixins will record when objects of the class are created in a datetime column called "created_at"
    # and when its updated in another datetime column called "updated_at".
    #
    #   class Bill < ActiveRecord::Base
    #     include ActiveRecord::Mixins::Touch
    #   end
    #
    #   bill = Bill.create("amount" => 100)
    #   bill.created_at # => Time.now at the moment of Bill.create
    #   bill.updated_at # => Time.now at the moment of Bill.create
    #
    #   bill.update_attribute("amount", 150)
    #   bill.created_at # => Time.now at the moment of Bill.create
    #   bill.updated_at # => Time.now at the moment of bill.update_attribute
    module Touch
      def self.append_features(base)
        super
        base.class_eval do
          before_create :touch_on_create
          before_update :touch_on_update

          def touch_on_create
            self.created_at = self.updated_at = Time.now
          end

          def touch_on_update
            self.updated_at = Time.now
          end
        end
      end  
    end
  end
end