diff --git a/activemodel/CHANGELOG.md b/activemodel/CHANGELOG.md index 0c5e096304d34..4cabd1bd5c9f4 100644 --- a/activemodel/CHANGELOG.md +++ b/activemodel/CHANGELOG.md @@ -1,3 +1,7 @@ +* `undefine_attribute_methods` undefines alias attribute methods along with attribute methods. + + *Nikita Vasilevsky* + * Error.full_message now strips ":base" from the message. *zzak* diff --git a/activemodel/test/cases/attribute_methods_test.rb b/activemodel/test/cases/attribute_methods_test.rb index b1aebecba0a4d..af4626a9daed7 100644 --- a/activemodel/test/cases/attribute_methods_test.rb +++ b/activemodel/test/cases/attribute_methods_test.rb @@ -215,6 +215,30 @@ def foo assert_raises(NoMethodError) { ModelWithAttributes.new.foo } end + test "#undefine_attribute_methods undefines alias attribute methods" do + topic_class = Class.new do + include ActiveModel::AttributeMethods + define_attribute_methods :title + alias_attribute :subject_to_be_undefined, :title + + def attributes + { title: "Active Model Topic" } + end + + private + def attribute(name) + attributes[name.to_sym] + end + end + + assert_equal("Active Model Topic", topic_class.new.subject_to_be_undefined) + topic_class.undefine_attribute_methods + + assert_raises(NoMethodError, match: /undefined method `subject_to_be_undefined'/) do + topic_class.new.subject_to_be_undefined + end + end + test "accessing a suffixed attribute" do m = ModelWithAttributes2.new m.attributes = { "foo" => "bar" } diff --git a/activerecord/test/cases/attribute_methods_test.rb b/activerecord/test/cases/attribute_methods_test.rb index b59a31d6e3d53..74d8cdd1f4f74 100644 --- a/activerecord/test/cases/attribute_methods_test.rb +++ b/activerecord/test/cases/attribute_methods_test.rb @@ -1022,6 +1022,22 @@ def name assert subklass.method_defined?(:id), "subklass is missing id method" end + test "#undefine_attribute_methods undefines alias attribute methods" do + topic_class = Class.new(ActiveRecord::Base) do + self.table_name = "topics" + + alias_attribute :subject_to_be_undefined, :title + end + + topic = topic_class.new(title: "New topic") + assert_equal("New topic", topic.subject_to_be_undefined) + topic_class.undefine_attribute_methods + + assert_raises(NoMethodError, match: /undefined method `subject_to_be_undefined'/) do + topic.subject_to_be_undefined + end + end + test "define_attribute_method works with both symbol and string" do klass = Class.new(ActiveRecord::Base) klass.table_name = "foo"