From 4331478996d6086bb089c1e33956a1c0fa9000b1 Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Fri, 2 Mar 2018 20:49:19 +0900 Subject: Refactor `_substitute_values` to be passed attribute names and values --- activerecord/lib/active_record/attribute_methods.rb | 20 +++++++------------- activerecord/lib/active_record/locking/optimistic.rb | 4 +--- activerecord/lib/active_record/persistence.rb | 14 +++++++------- 3 files changed, 15 insertions(+), 23 deletions(-) diff --git a/activerecord/lib/active_record/attribute_methods.rb b/activerecord/lib/active_record/attribute_methods.rb index 842f407517..0ca0e5dcdb 100644 --- a/activerecord/lib/active_record/attribute_methods.rb +++ b/activerecord/lib/active_record/attribute_methods.rb @@ -438,24 +438,18 @@ module ActiveRecord defined?(@attributes) && @attributes.key?(attr_name) end - def arel_attributes_with_values_for_create(attribute_names) - arel_attributes_with_values(attributes_for_create(attribute_names)) + def attributes_with_values_for_create(attribute_names) + attributes_with_values(attributes_for_create(attribute_names)) end - def arel_attributes_with_values_for_update(attribute_names) - arel_attributes_with_values(attributes_for_update(attribute_names)) + def attributes_with_values_for_update(attribute_names) + attributes_with_values(attributes_for_update(attribute_names)) end - # Returns a Hash of the Arel::Attributes and attribute values that have been - # typecasted for use in an Arel insert/update method. - def arel_attributes_with_values(attribute_names) - attrs = {} - arel_table = self.class.arel_table - - attribute_names.each do |name| - attrs[arel_table[name]] = _read_attribute(name) + def attributes_with_values(attribute_names) + attribute_names.each_with_object({}) do |name, attrs| + attrs[name] = _read_attribute(name) end - attrs end # Filters the primary keys and readonly attributes from the attribute names. diff --git a/activerecord/lib/active_record/locking/optimistic.rb b/activerecord/lib/active_record/locking/optimistic.rb index 052b5d23aa..cae7a66fcc 100644 --- a/activerecord/lib/active_record/locking/optimistic.rb +++ b/activerecord/lib/active_record/locking/optimistic.rb @@ -97,9 +97,7 @@ module ActiveRecord self.class.primary_key => id_in_database, lock_col => previous_lock_value ).update_all( - attributes_for_update(attribute_names).map do |name| - [name, _read_attribute(name)] - end.to_h + attributes_with_values_for_update(attribute_names) ) unless affected_rows == 1 diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb index 8f9cb85a90..28308da96d 100644 --- a/activerecord/lib/active_record/persistence.rb +++ b/activerecord/lib/active_record/persistence.rb @@ -169,12 +169,11 @@ module ActiveRecord primary_key_value = nil if primary_key && Hash === values - arel_primary_key = arel_attribute(primary_key) - primary_key_value = values[arel_primary_key] + primary_key_value = values[primary_key] if !primary_key_value && prefetch_primary_key? primary_key_value = next_sequence_value - values[arel_primary_key] = primary_key_value + values[primary_key] = primary_key_value end end @@ -208,8 +207,9 @@ module ActiveRecord end def _substitute_values(values) - values.map do |attr, value| - bind = predicate_builder.build_bind_attribute(attr.name, value) + values.map do |name, value| + attr = arel_attribute(name) + bind = predicate_builder.build_bind_attribute(name, value) [attr, bind] end end @@ -705,7 +705,7 @@ module ActiveRecord # Returns the number of affected rows. def _update_record(attribute_names = self.attribute_names) attribute_names &= self.class.column_names - attributes_values = arel_attributes_with_values_for_update(attribute_names) + attributes_values = attributes_with_values_for_update(attribute_names) if attributes_values.empty? rows_affected = 0 @_trigger_update_callback = true @@ -723,7 +723,7 @@ module ActiveRecord # and returns its id. def _create_record(attribute_names = self.attribute_names) attribute_names &= self.class.column_names - attributes_values = arel_attributes_with_values_for_create(attribute_names) + attributes_values = attributes_with_values_for_create(attribute_names) new_id = self.class._insert_record(attributes_values) self.id ||= new_id if self.class.primary_key -- cgit v1.2.3