/
timestamps.rb
35 lines (29 loc) · 1.13 KB
/
timestamps.rb
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 Recliner
# Recliner automatically timestamps create and update operations if the document has properties
# named created_at/created_on or updated_at/updated_on.
module Timestamps#:nodoc:
extend ActiveSupport::Concern
included do
alias_method_chain :save, :timestamps
alias_method_chain :create, :timestamps
end
module ClassMethods
# Defines timestamp properties created_at and updated_at.
# When the document is created or updated, these properties will be respectively updated.
def timestamps!
property :created_at, Time
property :updated_at, Time
end
end
def save_with_timestamps(*args)
write_attribute(:updated_at, Time.now) if properties.include?(:updated_at)
write_attribute(:updated_on, Time.now) if properties.include?(:updated_on)
save_without_timestamps(*args)
end
def create_with_timestamps
write_attribute(:created_at, Time.now) if properties.include?(:created_at)
write_attribute(:created_on, Time.now) if properties.include?(:created_on)
create_without_timestamps
end
end
end