refactor how artist notes are updated

This commit is contained in:
r888888888
2017-04-18 15:57:20 -07:00
parent e639be60e5
commit 68f057e7ba
4 changed files with 104 additions and 82 deletions

View File

@@ -6,9 +6,10 @@ class Artist < ActiveRecord::Base
after_save :create_version
after_save :save_url_string
after_save :categorize_tag
after_save :update_wiki
validates_uniqueness_of :name
validate :name_is_valid
validate :wiki_is_empty, :on => :create
validate :validate_name
validate :validate_wiki, :on => :create
belongs_to :creator, :class_name => "User"
has_many :members, :class_name => "Artist", :foreign_key => "group_name", :primary_key => "name"
has_many :urls, :dependent => :destroy, :class_name => "ArtistUrl"
@@ -16,8 +17,7 @@ class Artist < ActiveRecord::Base
has_one :wiki_page, :foreign_key => "title", :primary_key => "name"
has_one :tag_alias, :foreign_key => "antecedent_name", :primary_key => "name"
has_one :tag, :foreign_key => "name", :primary_key => "name"
accepts_nested_attributes_for :wiki_page
attr_accessible :body, :name, :url_string, :other_names, :other_names_comma, :group_name, :wiki_page_attributes, :notes, :as => [:member, :gold, :builder, :platinum, :janitor, :moderator, :default, :admin]
attr_accessible :body, :notes, :name, :url_string, :other_names, :other_names_comma, :group_name, :notes, :as => [:member, :gold, :builder, :platinum, :janitor, :moderator, :default, :admin]
attr_accessible :is_active, :as => [:builder, :janitor, :moderator, :default, :admin]
attr_accessible :is_banned, :as => :admin
@@ -99,7 +99,7 @@ class Artist < ActiveRecord::Base
end
end
def name_is_valid
def validate_name
if name =~ /^[-~]/
errors[:name] << "cannot begin with - or ~"
false
@@ -219,47 +219,61 @@ class Artist < ActiveRecord::Base
end
module NoteMethods
extend ActiveSupport::Concern
def notes
if wiki_page
wiki_page.body
else
nil
@notes || wiki_page.try(:body)
end
def notes=(text)
if notes != text
notes_will_change!
@notes = text
end
end
def notes=(msg)
if name_changed? && name_was.present?
old_wiki_page = WikiPage.titled(name_was).first
end
if wiki_page
if name_changed? && name_was.present?
wiki_page.body = wiki_page.body + "\n\n" + msg
else
wiki_page.body = msg
end
if wiki_page.body_changed?
wiki_page.save
@notes_changed = true
end
elsif old_wiki_page
old_wiki_page.title = name
old_wiki_page.body = msg
if old_wiki_page.body_changed? || old_wiki_page.title_changed?
old_wiki_page.save
@notes_changed = true
end
elsif msg.present?
self.wiki_page = WikiPage.new(:title => name, :body => msg)
@notes_changed = true
def reload(options = nil)
if instance_variable_defined?(:@notes)
remove_instance_variable(:@notes)
end
super
end
def notes_changed?
!!@notes_changed
attribute_changed?("notes")
end
def wiki_is_empty
def notes_will_change!
attribute_will_change!("notes")
end
def update_wiki
if persisted? && name_changed? && name_was.present? && WikiPage.titled(name_was).exists?
# we're renaming the artist, so rename the corresponding wiki page
old_page = WikiPage.titled(name_was).first
if wiki_page.present?
# a wiki page with the new name already exists, so update the content
wiki_page.update(body: "#{wiki_page.body}\n\n#{@notes}")
else
# a wiki page doesn't already exist for the new name, so rename the old one
old_page.update(title: name, body: @notes)
end
elsif wiki_page.nil?
# if there are any notes, we need to create a new wiki page
if @notes.present?
create_wiki_page(body: @notes, title: name)
end
elsif wiki_page.body != @notes || wiki_page.title != name
# if anything changed, we need to update the wiki page
wiki_page.body = @notes unless @notes.nil?
wiki_page.title = name
wiki_page.save
end
end
def validate_wiki
if WikiPage.titled(name).exists?
errors.add(:name, "conflicts with a wiki page")
return false