module ActiveRecord::Persistence

Active Record Persistence

Active Record Persistence

Active Record Persistence

Public Instance Methods

excluded_deltas() click to toggle source
# File lib/delta_attributes3_2/main.rb, line 31
def excluded_deltas
  @_excluded_deltas ||= Set.new
end
force_clobber(attr_name, value) click to toggle source
# File lib/delta_attributes3_2/main.rb, line 35
def force_clobber(attr_name, value)
  self.excluded_deltas.add(attr_name.to_s.intern)
  send("#{attr_name}=", value)
end
update(attribute_names = @attributes.keys) click to toggle source
Calls superclass method
# File lib/delta_attributes3_2/main.rb, line 13
def update(attribute_names = @attributes.keys)
  return super(attribute_names) if self.new_record?

  attributes_with_values = arel_attributes_values(false, false, attribute_names)
  return 0 if attributes_with_values.empty?

  attributes = { }
  @changed_attributes.each do |attribute_name, value|
    if self.class.delta_attributes.include?(attribute_name.intern) && !self.excluded_deltas.include?(attribute_name.intern)
      attributes[attribute_name] = value
    end
  end

  klass = self.class
  stmt = klass.unscoped.where(klass.arel_table[klass.primary_key].eq(id)).arel.compile_full_update(attributes_with_values, attributes)
  klass.connection.update stmt
end
update_record(attribute_names = @attributes.keys) click to toggle source

Updates the associated record with values matching those of the instance attributes. Returns the number of affected rows.

# File lib/delta_attributes4/persistence.rb, line 7
def update_record(attribute_names = @attributes.keys)
  attributes_with_values = arel_attributes_with_values_for_update(attribute_names)
  if attributes_with_values.empty?
    0
  else
    klass = self.class
    column_hash = klass.connection.schema_cache.columns_hash klass.table_name
    db_columns_with_values = attributes_with_values.map { |attr,value|
      real_column = column_hash[attr.name]

      v = value
      if self.class.respond_to?(:delta_attributes) && self.class.delta_attributes.include?(attr.name)
        if @changed_attributes.include?(attr.name)
          v = value - @changed_attributes[attr.name]
        end
      end

      [real_column, v]
    }
    bind_attrs = attributes_with_values.dup
    bind_attrs.keys.each_with_index do |column, i|
      real_column = db_columns_with_values[i].first
      bind_attrs[column] = klass.connection.substitute_at(real_column, i)
    end
    stmt = klass.unscoped.where(klass.arel_table[klass.primary_key].eq(id_was || id)).arel.compile_update(bind_attrs)
    klass.connection.update stmt, 'SQL', db_columns_with_values
  end
end