diff options
-rw-r--r-- | activemodel/lib/active_model/attributes.rb | 15 | ||||
-rw-r--r-- | activerecord/lib/active_record/attribute_methods/dirty.rb | 8 | ||||
-rw-r--r-- | activerecord/lib/active_record/attribute_methods/read.rb | 4 | ||||
-rw-r--r-- | activerecord/lib/active_record/attribute_methods/write.rb | 4 | ||||
-rw-r--r-- | activerecord/lib/active_record/core.rb | 3 | ||||
-rw-r--r-- | activerecord/lib/active_record/persistence.rb | 9 | ||||
-rw-r--r-- | activerecord/lib/active_record/relation/query_methods.rb | 2 | ||||
-rw-r--r-- | activerecord/lib/active_record/table_metadata.rb | 6 | ||||
-rw-r--r-- | activerecord/test/models/post.rb | 4 |
9 files changed, 24 insertions, 31 deletions
diff --git a/activemodel/lib/active_model/attributes.rb b/activemodel/lib/active_model/attributes.rb index 6048ff0113..d176ea88d0 100644 --- a/activemodel/lib/active_model/attributes.rb +++ b/activemodel/lib/active_model/attributes.rb @@ -116,22 +116,17 @@ module ActiveModel private def write_attribute(attr_name, value) - name = if self.class.attribute_alias?(attr_name) - self.class.attribute_alias(attr_name).to_s - else - attr_name.to_s - end + name = attr_name.to_s + name = self.class.attribute_aliases[name] || name @attributes.write_from_user(name, value) value end def attribute(attr_name) - name = if self.class.attribute_alias?(attr_name) - self.class.attribute_alias(attr_name).to_s - else - attr_name.to_s - end + name = attr_name.to_s + name = self.class.attribute_aliases[name] || name + @attributes.fetch_value(name) end diff --git a/activerecord/lib/active_record/attribute_methods/dirty.rb b/activerecord/lib/active_record/attribute_methods/dirty.rb index 444568562b..a43ebdf60d 100644 --- a/activerecord/lib/active_record/attribute_methods/dirty.rb +++ b/activerecord/lib/active_record/attribute_methods/dirty.rb @@ -167,12 +167,8 @@ module ActiveRecord end def write_attribute_without_type_cast(attr_name, value) - name = attr_name.to_s - if self.class.attribute_alias?(name) - name = self.class.attribute_alias(name) - end - result = super(name, value) - clear_attribute_change(name) + result = super + clear_attribute_change(attr_name) result end diff --git a/activerecord/lib/active_record/attribute_methods/read.rb b/activerecord/lib/active_record/attribute_methods/read.rb index c787fb6a94..0562327a9a 100644 --- a/activerecord/lib/active_record/attribute_methods/read.rb +++ b/activerecord/lib/active_record/attribute_methods/read.rb @@ -27,9 +27,7 @@ module ActiveRecord # to a date object, like Date.new(2004, 12, 12)). def read_attribute(attr_name, &block) name = attr_name.to_s - if self.class.attribute_alias?(name) - name = self.class.attribute_alias(name) - end + name = self.class.attribute_aliases[name] || name name = @primary_key if name == "id" && @primary_key _read_attribute(name, &block) diff --git a/activerecord/lib/active_record/attribute_methods/write.rb b/activerecord/lib/active_record/attribute_methods/write.rb index 6a54bd2fc2..1c63b553d0 100644 --- a/activerecord/lib/active_record/attribute_methods/write.rb +++ b/activerecord/lib/active_record/attribute_methods/write.rb @@ -31,9 +31,7 @@ module ActiveRecord # turned into +nil+. def write_attribute(attr_name, value) name = attr_name.to_s - if self.class.attribute_alias?(name) - name = self.class.attribute_alias(name) - end + name = self.class.attribute_aliases[name] || name name = @primary_key if name == "id" && @primary_key _write_attribute(name, value) diff --git a/activerecord/lib/active_record/core.rb b/activerecord/lib/active_record/core.rb index 28bd7f8cdc..dfd33d3dd7 100644 --- a/activerecord/lib/active_record/core.rb +++ b/activerecord/lib/active_record/core.rb @@ -268,7 +268,8 @@ module ActiveRecord end def arel_attribute(name, table = arel_table) # :nodoc: - name = attribute_alias(name) if attribute_alias?(name) + name = name.to_s + name = attribute_aliases[name] || name table[name] end diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb index 1ff7ee4d89..adfd564695 100644 --- a/activerecord/lib/active_record/persistence.rb +++ b/activerecord/lib/active_record/persistence.rb @@ -664,8 +664,13 @@ module ActiveRecord raise ActiveRecordError, "cannot update a new record" if new_record? raise ActiveRecordError, "cannot update a destroyed record" if destroyed? + attributes = attributes.transform_keys do |key| + name = key.to_s + self.class.attribute_aliases[name] || name + end + attributes.each_key do |key| - verify_readonly_attribute(key.to_s) + verify_readonly_attribute(key) end id_in_database = self.id_in_database @@ -853,7 +858,7 @@ module ActiveRecord attribute_names = timestamp_attributes_for_update_in_model attribute_names |= names.map!(&:to_s).map! { |name| - self.class.attribute_alias?(name) ? self.class.attribute_alias(name) : name + self.class.attribute_aliases[name] || name } unless attribute_names.empty? diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index f30428d0a5..c03ca7f1e7 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -1178,7 +1178,7 @@ module ActiveRecord end def arel_column(field) - field = klass.attribute_alias(field) if klass.attribute_alias?(field) + field = klass.attribute_aliases[field] || field from = from_clause.name || from_clause.value if klass.columns_hash.key?(field) && (!from || table_name_matches?(from)) diff --git a/activerecord/lib/active_record/table_metadata.rb b/activerecord/lib/active_record/table_metadata.rb index b67479fb6a..073866b894 100644 --- a/activerecord/lib/active_record/table_metadata.rb +++ b/activerecord/lib/active_record/table_metadata.rb @@ -12,9 +12,9 @@ module ActiveRecord def resolve_column_aliases(hash) new_hash = hash.dup - hash.each do |key, _| - if (key.is_a?(Symbol)) && klass.attribute_alias?(key) - new_hash[klass.attribute_alias(key)] = new_hash.delete(key) + hash.each_key do |key| + if key.is_a?(Symbol) && new_key = klass.attribute_aliases[key.to_s] + new_hash[new_key] = new_hash.delete(key) end end new_hash diff --git a/activerecord/test/models/post.rb b/activerecord/test/models/post.rb index c34968590f..090b576a5d 100644 --- a/activerecord/test/models/post.rb +++ b/activerecord/test/models/post.rb @@ -323,8 +323,8 @@ class FakeKlass "posts" end - def attribute_alias?(name) - false + def attribute_aliases + {} end def sanitize_sql(sql) |