Sha256: 025af8e5badf1deb5364693c7c1df48e235857177215c40ac9649aeffd24eb65
Contents?: true
Size: 1.38 KB
Versions: 3
Compression:
Stored size: 1.38 KB
Contents
module HasTimestamps def self.included(base) #:nodoc: base.extend(ClassMethods) end module ClassMethods def has_timestamps(opts = {}) class_eval do def save_or_destroy_timestamps timestamps.each do |timestamp| if timestamp.stamped_at.acts_like?(:time) or timestamp.stamped_at.is_a?(Date) or timestamp.stamped_at.is_a?(DateTime) timestamp.save elsif !timestamp.new_record? timestamp.destroy end end end after_save :save_or_destroy_timestamps def timestamp!(key) timestamps[key.to_s] = Time.now end def timestamped?(key) !timestamps[key.to_s].blank? end end has_many :timestamps, opts.merge(:as => :timestampable) do def [](key) fetch_timestamp(key).stamped_at end def []=(key, stamped_at) fetch_timestamp(key).stamped_at = stamped_at end def find_by_key(key) proxy_owner.timestamps.to_a.find { |timestamp| timestamp.key == key.to_s } end private def fetch_timestamp(key) find_by_key(key) || build_timestamp(key) end def build_timestamp(key) build(:key => key.to_s) end end end end end ActiveRecord::Base.class_eval { include HasTimestamps }
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
seamusabshere-has_timestamps-1.1 | lib/has_timestamps.rb |
seamusabshere-has_timestamps-1.2 | lib/has_timestamps.rb |
seamusabshere-has_timestamps-1.3 | lib/has_timestamps.rb |