summaryrefslogtreecommitdiffstats
path: root/app/models/wiki_page.rb
diff options
context:
space:
mode:
authorJean-Philippe Lang <jp_lang@yahoo.fr>2007-09-09 17:05:38 +0000
committerJean-Philippe Lang <jp_lang@yahoo.fr>2007-09-09 17:05:38 +0000
commitb4d9ca8875898500ca198bff501dfc7d1c28bd71 (patch)
tree8d9ebcf80cd56b427211b6b769514ab8d66f33a2 /app/models/wiki_page.rb
parentf6fe15716e4714c5f6d4d88149927b7dfa5c6fce (diff)
downloadredmine-b4d9ca8875898500ca198bff501dfc7d1c28bd71.tar.gz
redmine-b4d9ca8875898500ca198bff501dfc7d1c28bd71.zip
Added the ability to rename wiki pages (specific permission required).
Existing links that point to the old page are preserved and automatically redirected to the new page (this behaviour can be disabled when renaming the page). git-svn-id: http://redmine.rubyforge.org/svn/trunk@720 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'app/models/wiki_page.rb')
-rw-r--r--app/models/wiki_page.rb30
1 files changed, 28 insertions, 2 deletions
diff --git a/app/models/wiki_page.rb b/app/models/wiki_page.rb
index 074d36daa..1ef8b7db4 100644
--- a/app/models/wiki_page.rb
+++ b/app/models/wiki_page.rb
@@ -22,13 +22,39 @@ class WikiPage < ActiveRecord::Base
has_one :content, :class_name => 'WikiContent', :foreign_key => 'page_id', :dependent => :destroy
has_many :attachments, :as => :container, :dependent => :destroy
+ attr_accessor :redirect_existing_links
+
validates_presence_of :title
validates_format_of :title, :with => /^[^,\.\/\?\;\|\s]*$/
validates_uniqueness_of :title, :scope => :wiki_id, :case_sensitive => false
validates_associated :content
-
+
+ def title=(value)
+ value = Wiki.titleize(value)
+ @previous_title = read_attribute(:title) if @previous_title.blank?
+ write_attribute(:title, value)
+ end
+
def before_save
- self.title = Wiki.titleize(title)
+ self.title = Wiki.titleize(title)
+ # Manage redirects if the title has changed
+ if !@previous_title.blank? && (@previous_title != title) && !new_record?
+ # Update redirects that point to the old title
+ wiki.redirects.find_all_by_redirects_to(@previous_title).each do |r|
+ r.redirects_to = title
+ r.title == r.redirects_to ? r.destroy : r.save
+ end
+ # Remove redirects for the new title
+ wiki.redirects.find_all_by_title(title).each(&:destroy)
+ # Create a redirect to the new title
+ wiki.redirects << WikiRedirect.new(:title => @previous_title, :redirects_to => title) unless redirect_existing_links == "0"
+ @previous_title = nil
+ end
+ end
+
+ def before_destroy
+ # Remove redirects to this page
+ wiki.redirects.find_all_by_redirects_to(title).each(&:destroy)
end
def pretty_title