aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard Schneeman <richard.schneeman+no-recruiters@gmail.com>2018-10-17 09:17:48 -0500
committerGitHub <noreply@github.com>2018-10-17 09:17:48 -0500
commitead868315f9b0fedb351c9b451aa1f66a2dc8038 (patch)
tree9dd9987ac203ce0ccb0d99a3cb970a53c6c1ce67
parent12c7b101f8cfa9ecb9d9483a02b0b6ebf792b77e (diff)
parentf45267bc423017109e442e5c35a5765dc482b12b (diff)
downloadrails-ead868315f9b0fedb351c9b451aa1f66a2dc8038.tar.gz
rails-ead868315f9b0fedb351c9b451aa1f66a2dc8038.tar.bz2
rails-ead868315f9b0fedb351c9b451aa1f66a2dc8038.zip
Merge pull request #34197 from schneems/schneems/symbol-hash-respond_to
ActiveRecord#respond_to? No longer allocates strings
-rw-r--r--activerecord/lib/active_record/attribute_methods.rb15
-rw-r--r--activerecord/lib/active_record/model_schema.rb6
-rw-r--r--activerecord/lib/active_record/nested_attributes.rb2
3 files changed, 11 insertions, 12 deletions
diff --git a/activerecord/lib/active_record/attribute_methods.rb b/activerecord/lib/active_record/attribute_methods.rb
index 3c785180e1..eeb88e4b7a 100644
--- a/activerecord/lib/active_record/attribute_methods.rb
+++ b/activerecord/lib/active_record/attribute_methods.rb
@@ -261,21 +261,14 @@ module ActiveRecord
def respond_to?(name, include_private = false)
return false unless super
- case name
- when :to_partial_path
- name = "to_partial_path"
- when :to_model
- name = "to_model"
- else
- name = name.to_s
- end
-
# If the result is true then check for the select case.
# For queries selecting a subset of columns, return false for unselected columns.
# We check defined?(@attributes) not to issue warnings if called on objects that
# have been allocated but not yet initialized.
- if defined?(@attributes) && self.class.column_names.include?(name)
- return has_attribute?(name)
+ if defined?(@attributes)
+ if name = self.class.symbol_column_to_string(name.to_sym)
+ return has_attribute?(name)
+ end
end
true
diff --git a/activerecord/lib/active_record/model_schema.rb b/activerecord/lib/active_record/model_schema.rb
index 5a4a1fb969..c50a420432 100644
--- a/activerecord/lib/active_record/model_schema.rb
+++ b/activerecord/lib/active_record/model_schema.rb
@@ -388,6 +388,11 @@ module ActiveRecord
@column_names ||= columns.map(&:name)
end
+ def symbol_column_to_string(name_symbol) # :nodoc:
+ @symbol_column_to_string_name_hash ||= column_names.index_by(&:to_sym)
+ @symbol_column_to_string_name_hash[name_symbol]
+ end
+
# Returns an array of column objects where the primary id, all columns ending in "_id" or "_count",
# and columns used for single table inheritance have been removed.
def content_columns
@@ -477,6 +482,7 @@ module ActiveRecord
def reload_schema_from_cache
@arel_table = nil
@column_names = nil
+ @symbol_column_to_string_name_hash = nil
@attribute_types = nil
@content_columns = nil
@default_attributes = nil
diff --git a/activerecord/lib/active_record/nested_attributes.rb b/activerecord/lib/active_record/nested_attributes.rb
index 50767ee93f..8b9098df6c 100644
--- a/activerecord/lib/active_record/nested_attributes.rb
+++ b/activerecord/lib/active_record/nested_attributes.rb
@@ -426,7 +426,7 @@ module ActiveRecord
existing_record.assign_attributes(assignable_attributes)
association(association_name).initialize_attributes(existing_record)
else
- method = "build_#{association_name}"
+ method = :"build_#{association_name}"
if respond_to?(method)
send(method, assignable_attributes)
else