|
Revision 9159, 1.6 kB
(checked in by bitsweat, 5 months ago)
|
Partial updates play nice with updated_at/on timestamps
|
| Line | |
|---|
| 1 |
module ActiveRecord |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
module Timestamp |
|---|
| 11 |
def self.included(base) |
|---|
| 12 |
base.alias_method_chain :create, :timestamps |
|---|
| 13 |
base.alias_method_chain :update, :timestamps |
|---|
| 14 |
|
|---|
| 15 |
base.class_inheritable_accessor :record_timestamps, :instance_writer => false |
|---|
| 16 |
base.record_timestamps = true |
|---|
| 17 |
end |
|---|
| 18 |
|
|---|
| 19 |
private |
|---|
| 20 |
def create_with_timestamps |
|---|
| 21 |
if record_timestamps |
|---|
| 22 |
t = self.class.default_timezone == :utc ? Time.now.utc : Time.now |
|---|
| 23 |
write_attribute('created_at', t) if respond_to?(:created_at) && created_at.nil? |
|---|
| 24 |
write_attribute('created_on', t) if respond_to?(:created_on) && created_on.nil? |
|---|
| 25 |
|
|---|
| 26 |
write_attribute('updated_at', t) if respond_to?(:updated_at) |
|---|
| 27 |
write_attribute('updated_on', t) if respond_to?(:updated_on) |
|---|
| 28 |
end |
|---|
| 29 |
create_without_timestamps |
|---|
| 30 |
end |
|---|
| 31 |
|
|---|
| 32 |
def update_with_timestamps(*args) |
|---|
| 33 |
if record_timestamps && (!partial_updates? || changed?) |
|---|
| 34 |
t = self.class.default_timezone == :utc ? Time.now.utc : Time.now |
|---|
| 35 |
write_attribute('updated_at', t) if respond_to?(:updated_at) |
|---|
| 36 |
write_attribute('updated_on', t) if respond_to?(:updated_on) |
|---|
| 37 |
end |
|---|
| 38 |
update_without_timestamps(*args) |
|---|
| 39 |
end |
|---|
| 40 |
end |
|---|
| 41 |
end |
|---|