Sha256: 36d078ea423ff9fd794f8b61fe1926a1626fd0ce1f28d15ab486a58904c07cb3

Contents?: true

Size: 1.56 KB

Versions: 1

Compression:

Stored size: 1.56 KB

Contents

module ActiveRecord #:nodoc:
  module Acts #:nodoc:
    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
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
seamusabshere-has_timestamps-1.0 lib/has_timestamps.rb