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

@@ -10,12 +10,13 @@ class WikiPage < ApplicationRecord
normalize :body, :normalize_text
normalize :other_names, :normalize_other_names
array_attribute :other_names # XXX must come after `normalize :other_names`
validates :title, tag_name: true, presence: true, uniqueness: true, if: :title_changed?
validates :body, presence: true, unless: -> { is_deleted? || other_names.present? }
validate :validate_rename
validate :validate_other_names
array_attribute :other_names
has_one :tag, :foreign_key => "name", :primary_key => "title"
has_one :artist, -> { active }, foreign_key: "name", primary_key: "title"
has_many :versions, -> {order("wiki_page_versions.id ASC")}, :class_name => "WikiPageVersion", :dependent => :destroy