Sha256: baedc04af5e1e5f0efbbbe6685d801935327d692220209e5d30a4d4e8c9ed8cd

Contents?: true

Size: 1.48 KB

Versions: 1

Compression:

Stored size: 1.48 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)
          t = fetch_timestamp(key, false)
          t.stamped_at unless t.nil?
        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, build_if_nil = true)
          t = find_by_key(key)
          t = build_timestamp(key) if t.nil? and build_if_nil
          t
        end

        def build_timestamp(key)
          build(:key => key.to_s)
        end
      end
    end
  end
end

ActiveRecord::Base.class_eval { include HasTimestamps }

Version data entries

1 entries across 1 versions & 1 rubygems

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