Sha256: 5efdc76de904dc6b72da4e312bd53bc9d048eb82e049aae906112fe29cb6529f

Contents?: true

Size: 1.73 KB

Versions: 14

Compression:

Stored size: 1.73 KB

Contents

module ActiveRecord
  module Locking
    module Optimistic

      private

      silence_warnings do
        def _update_record(attribute_names = @attributes.keys) #:nodoc:
          return super unless locking_enabled?
          return 0 if attribute_names.empty?

          lock_col = self.class.locking_column
          previous_lock_value = send(lock_col).to_i
          increment_lock

          attribute_names += [lock_col]
          attribute_names.uniq!

          begin
            relation = self.class.unscoped

            if self.composite?
              affected_rows = relation.where(
                  relation.cpk_id_predicate(relation.table, self.class.primary_key, id_was)
              ).where(
                  lock_col => previous_lock_value
              ).update_all(
                  Hash[attributes_for_update(attribute_names).map do |name|
                         [name, _read_attribute(name)]
                       end]
              )
            else
              affected_rows = relation.where(
                  self.class.primary_key => id,
                  lock_col => previous_lock_value,
              ).update_all(
                  Hash[attributes_for_update(attribute_names).map do |name|
                       [name, _read_attribute(name)]
                     end]
              )
            end

            unless affected_rows == 1
              raise ActiveRecord::StaleObjectError.new(self, "update")
            end

            affected_rows

          # If something went wrong, revert the version.
          rescue Exception
            send(lock_col + '=', previous_lock_value)
            raise
          end
        end
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
composite_primary_keys-9.0.9 lib/composite_primary_keys/locking/optimistic.rb
composite_primary_keys-10.0.3 lib/composite_primary_keys/locking/optimistic.rb
composite_primary_keys-10.0.2 lib/composite_primary_keys/locking/optimistic.rb
composite_primary_keys-10.0.1 lib/composite_primary_keys/locking/optimistic.rb
composite_primary_keys-9.0.8 lib/composite_primary_keys/locking/optimistic.rb
composite_primary_keys-10.0.0 lib/composite_primary_keys/locking/optimistic.rb
composite_primary_keys-9.0.7 lib/composite_primary_keys/locking/optimistic.rb
composite_primary_keys-9.0.6 lib/composite_primary_keys/locking/optimistic.rb
composite_primary_keys-9.0.5 lib/composite_primary_keys/locking/optimistic.rb
composite_primary_keys-9.0.4 lib/composite_primary_keys/locking/optimistic.rb
composite_primary_keys-9.0.2 lib/composite_primary_keys/locking/optimistic.rb
composite_primary_keys-9.0.1 lib/composite_primary_keys/locking/optimistic.rb
composite_primary_keys-9.0.0 lib/composite_primary_keys/locking/optimistic.rb
composite_primary_keys-9.0.0.beta1 lib/composite_primary_keys/locking/optimistic.rb