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.
23 lines
461 B
Ruby
23 lines
461 B
Ruby
module Normalizable
|
|
extend ActiveSupport::Concern
|
|
|
|
class_methods do
|
|
def normalize(attribute, method_name)
|
|
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
|
|
|
|
def normalize_text(text)
|
|
text.unicode_normalize(:nfc).normalize_whitespace.strip
|
|
end
|
|
end
|
|
end
|