Module: FastTimestamp
- Defined in:
- lib/fast_timestamp.rb
Overview
Expects a ::Timestamp ActiveRecord class with timestampable_type, timestampable_id, key:string, and stamped_at:datetime.
Class Method Summary collapse
Instance Method Summary collapse
- #fast_destroy_timestamps ⇒ Object
- #set_timestamp!(key, time) ⇒ Object
- #timestamp!(key) ⇒ Object
-
#timestamp_for(key) ⇒ Object
returns a Time object.
- #timestamped?(key) ⇒ Boolean
- #untimestamp!(key) ⇒ Object
Class Method Details
.included(base) ⇒ Object
3 4 5 |
# File 'lib/fast_timestamp.rb', line 3 def self.included(base) base.class_eval { after_destroy :fast_destroy_timestamps } end |
Instance Method Details
#fast_destroy_timestamps ⇒ Object
37 38 39 40 |
# File 'lib/fast_timestamp.rb', line 37 def ::Timestamp.destroy_all :timestampable_type => self.class.base_class.name, :timestampable_id => id true end |
#set_timestamp!(key, time) ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/fast_timestamp.rb', line 29 def (key, time) raise "Can't timestamp new records" if new_record? transaction do t = ::Timestamp.(self.class.base_class.name, id, key.to_s) t.update_attribute :stamped_at, time end end |
#timestamp!(key) ⇒ Object
7 8 9 |
# File 'lib/fast_timestamp.rb', line 7 def (key) key, Time.zone.now end |
#timestamp_for(key) ⇒ Object
returns a Time object
23 24 25 26 27 |
# File 'lib/fast_timestamp.rb', line 23 def (key) if t = ::Timestamp.(self.class.base_class.name, id, key.to_s) t.read_attribute :stamped_at end end |
#timestamped?(key) ⇒ Boolean
17 18 19 20 |
# File 'lib/fast_timestamp.rb', line 17 def (key) raise "Can't check timestamps on new record" if new_record? ::Timestamp.exists? :timestampable_type => self.class.base_class.name, :timestampable_id => id, :key => key.to_s end |
#untimestamp!(key) ⇒ Object
11 12 13 14 15 |
# File 'lib/fast_timestamp.rb', line 11 def (key) if t = ::Timestamp.(self.class.base_class.name, id, key.to_s) t.destroy end end |