Skip to content

Commit e50fce0

Browse files
committed
Add new behavior of undefine_attribute_methods to CHANGELOG
In `1818beb3a3ea5fdb498095d4885f8a7e512f24ca` Rails changed the target where alias attribute methods are defined. It lead to `undefine_attribute_methods` to clean alias attribute methods along with the attribute methods. It was an intended behavior change but it wasn't properly documented and tested. This commit clarifies the new behavior in the Active Model changelog along with covering the behavior with tests.
1 parent ed5af00 commit e50fce0

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

activemodel/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* `undefine_attribute_methods` undefines alias attribute methods along with attribute methods.
2+
3+
*Nikita Vasilevsky*
4+
15
* Error.full_message now strips ":base" from the message.
26

37
*zzak*

activemodel/test/cases/attribute_methods_test.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,30 @@ def foo
215215
assert_raises(NoMethodError) { ModelWithAttributes.new.foo }
216216
end
217217

218+
test "#undefine_attribute_methods undefines alias attribute methods" do
219+
topic_class = Class.new do
220+
include ActiveModel::AttributeMethods
221+
define_attribute_methods :title
222+
alias_attribute :subject_to_be_undefined, :title
223+
224+
def attributes
225+
{ title: "Active Model Topic" }
226+
end
227+
228+
private
229+
def attribute(name)
230+
attributes[name.to_sym]
231+
end
232+
end
233+
234+
assert_equal("Active Model Topic", topic_class.new.subject_to_be_undefined)
235+
topic_class.undefine_attribute_methods
236+
237+
assert_raises(NoMethodError, match: /undefined method `subject_to_be_undefined'/) do
238+
topic_class.new.subject_to_be_undefined
239+
end
240+
end
241+
218242
test "accessing a suffixed attribute" do
219243
m = ModelWithAttributes2.new
220244
m.attributes = { "foo" => "bar" }

activerecord/test/cases/attribute_methods_test.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,6 +1022,22 @@ def name
10221022
assert subklass.method_defined?(:id), "subklass is missing id method"
10231023
end
10241024

1025+
test "#undefine_attribute_methods undefines alias attribute methods" do
1026+
topic_class = Class.new(ActiveRecord::Base) do
1027+
self.table_name = "topics"
1028+
1029+
alias_attribute :subject_to_be_undefined, :title
1030+
end
1031+
1032+
topic = topic_class.new(title: "New topic")
1033+
assert_equal("New topic", topic.subject_to_be_undefined)
1034+
topic_class.undefine_attribute_methods
1035+
1036+
assert_raises(NoMethodError, match: /undefined method `subject_to_be_undefined'/) do
1037+
topic.subject_to_be_undefined
1038+
end
1039+
end
1040+
10251041
test "define_attribute_method works with both symbol and string" do
10261042
klass = Class.new(ActiveRecord::Base)
10271043
klass.table_name = "foo"

0 commit comments

Comments
 (0)