Fix conflict between normalize and array_attribute macros.

Fix the `normalize` and `array_attribute` macros conflicting with each
other on the WikiPage model. This meant code like
`wiki_page.other_names = "foo bar"` didn't work. Both macros defined a
`other_names=` method, but one method overrode the other.

The fix is to use anonymous modules and prepend so we can chain method
calls with super.
This commit is contained in:
evazion
2021-01-10 00:10:59 -06:00
parent 5962152ee3
commit 0899194f6b
5 changed files with 50 additions and 35 deletions

View File

@@ -3,10 +3,14 @@ module Normalizable
class_methods do
def normalize(attribute, method_name)
define_method("#{attribute}=") do |value|
normalized_value = self.class.send(method_name, value)
super(normalized_value)
mod = Module.new do
define_method("#{attribute}=") do |value|
normalized_value = self.class.send(method_name, value)
super(normalized_value)
end
end
prepend mod
end
private