Module: ActiveRecord::Persistence

Defined in:
lib/delta_attributes4/main.rb,
lib/delta_attributes3_2/main.rb,
lib/delta_attributes4/persistence.rb

Overview

Active Record Persistence

Defined Under Namespace

Modules: ClassMethods Classes: InvalidDeltaColumn

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

:nodoc:



11
12
13
# File 'lib/delta_attributes4/main.rb', line 11

def self.included(base) #:nodoc:
  base.extend ClassMethods
end

Instance Method Details

#excluded_deltasObject



31
32
33
# File 'lib/delta_attributes3_2/main.rb', line 31

def excluded_deltas
  @_excluded_deltas ||= Set.new
end

#force_clobber(attr_name, value) ⇒ Object



35
36
37
38
# 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) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 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) ⇒ Object

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



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 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