1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Moved update_record logic to relation.rb

This commit is contained in:
Noemj 2013-04-30 14:27:18 +03:00
parent a834b57b6e
commit 9693df61f0
2 changed files with 28 additions and 26 deletions

View file

@ -428,23 +428,11 @@ module ActiveRecord
# Updates the associated record with values matching those of the instance attributes.
# Returns the number of affected rows.
def update_record(attribute_names = @attributes.keys)
attributes_with_values = arel_attributes_with_values_for_update(attribute_names)
if attributes_with_values.empty?
attributes_values = arel_attributes_with_values_for_update(attribute_names)
if attributes_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]
[real_column, value]
}
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
self.class.unscoped.update_record attributes_values, id, id_was
end
end

View file

@ -56,16 +56,7 @@ module ActiveRecord
im = arel.create_insert
im.into @table
conn = @klass.connection
substitutes = values.sort_by { |arel_attr,_| arel_attr.name }
binds = substitutes.map do |arel_attr, value|
[@klass.columns_hash[arel_attr.name], value]
end
substitutes.each_with_index do |tuple, i|
tuple[1] = conn.substitute_at(binds[i][0], i)
end
substitutes, binds = substitute_values values
if values.empty? # empty insert
im.values = Arel.sql(connection.empty_insert_statement_value)
@ -73,7 +64,7 @@ module ActiveRecord
im.insert substitutes
end
conn.insert(
@klass.connection.insert(
im,
'SQL',
primary_key,
@ -82,6 +73,29 @@ module ActiveRecord
binds)
end
def update_record(values, id, id_was)
substitutes, binds = substitute_values values
um = @klass.unscoped.where(@klass.arel_table[@klass.primary_key].eq(id_was || id)).arel.compile_update(substitutes)
@klass.connection.update(
um,
'SQL',
binds)
end
def substitute_values(values)
substitutes = values.sort_by { |arel_attr,_| arel_attr.name }
binds = substitutes.map do |arel_attr, value|
[@klass.columns_hash[arel_attr.name], value]
end
substitutes.each_with_index do |tuple, i|
tuple[1] = @klass.connection.substitute_at(binds[i][0], i)
end
[substitutes, binds]
end
# Initializes new record from relation while maintaining the current
# scope.
#