Sha256: bfe8f5542d9eb996fbd95ccc56a7e02705723caabc596ae55cfbd883e834198b

Contents?: true

Size: 1 KB

Versions: 1

Compression:

Stored size: 1 KB

Contents

require 'ixtlan/optimistic/object_stale_exception'
module Ixtlan
  module Optimistic
    module ActiveRecord

      def self.included(base)
        base.class_eval do

          def self.optimistic_find(updated_at, *args)
            if updated_at  
              dummy = self.new
              dummy.updated_at = updated_at
              updated_at_date = dummy.updated_at
              # try different ways to use the date
              # TODO maybe there is a nicer way ??
              # TODO make it work with different PKs
              result = first(:conditions => ["id = ? and updated_at <= ? and updated_at >= ?", args[0], updated_at_date + 0.0005, updated_at_date - 0.0005])
p result
              raise ObjectStaleException.new "#{self.class} with ID=#{args[0]} is stale" unless result
              result
            else
              raise ObjectStaleException.new "no 'updated_at' given. could not dind #{self.class} with ID=#{args[0]}."
            end
          end

        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ixtlan-optimistic-0.1.0 lib/ixtlan/optimistic/active_record.rb