diff options
author | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2014-11-23 13:36:01 +0000 |
---|---|---|
committer | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2014-11-23 13:36:01 +0000 |
commit | e0a034164f3409336f686cfa381da8a5db15c40e (patch) | |
tree | e4bd9590116c03026375a3ae5c618e01cd7bddc3 /app/models/wiki_page.rb | |
parent | 1ada789a6ccbbbaa55adc3a661ea7cef917d4b71 (diff) | |
download | redmine-e0a034164f3409336f686cfa381da8a5db15c40e.tar.gz redmine-e0a034164f3409336f686cfa381da8a5db15c40e.zip |
Move wiki page to other project (#5450).
git-svn-id: http://svn.redmine.org/redmine/trunk@13643 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'app/models/wiki_page.rb')
-rw-r--r-- | app/models/wiki_page.rb | 76 |
1 files changed, 58 insertions, 18 deletions
diff --git a/app/models/wiki_page.rb b/app/models/wiki_page.rb index 8aed835ad..9d68de2b6 100644 --- a/app/models/wiki_page.rb +++ b/app/models/wiki_page.rb @@ -46,8 +46,9 @@ class WikiPage < ActiveRecord::Base attr_protected :id validate :validate_parent_title - before_destroy :remove_redirects - before_save :handle_redirects + before_destroy :delete_redirects + before_save :handle_rename_or_move + after_save :handle_children_move # eager load information about last updates, without loading text scope :with_updated_on, lambda { @@ -58,7 +59,7 @@ class WikiPage < ActiveRecord::Base # Wiki pages that are protected by default DEFAULT_PROTECTED_PAGES = %w(sidebar) - safe_attributes 'parent_id', 'parent_title', + safe_attributes 'parent_id', 'parent_title', 'title', 'redirect_existing_links', 'wiki_id', :if => lambda {|page, user| page.new_record? || user.allowed_to?(:rename_wiki_pages, page.project)} def initialize(attributes=nil, *args) @@ -74,30 +75,67 @@ class WikiPage < ActiveRecord::Base def title=(value) value = Wiki.titleize(value) - @previous_title = read_attribute(:title) if @previous_title.blank? write_attribute(:title, value) end - def handle_redirects - self.title = Wiki.titleize(title) - # Manage redirects if the title has changed - if !@previous_title.blank? && (@previous_title != title) && !new_record? + def safe_attributes=(attrs, user=User.current) + return unless attrs.is_a?(Hash) + attrs = attrs.deep_dup + + # Project and Tracker must be set before since new_statuses_allowed_to depends on it. + if (w_id = attrs.delete('wiki_id')) && safe_attribute?('wiki_id') + if (w = Wiki.find_by_id(w_id)) && w.project && user.allowed_to?(:rename_wiki_pages, w.project) + self.wiki = w + end + end + + super attrs, user + end + + # Manages redirects if page is renamed or moved + def handle_rename_or_move + if !new_record? && (title_changed? || wiki_id_changed?) # Update redirects that point to the old title - wiki.redirects.where(:redirects_to => @previous_title).each do |r| + WikiRedirect.where(:redirects_to => title_was, :redirects_to_wiki_id => wiki_id_was).each do |r| r.redirects_to = title - r.title == r.redirects_to ? r.destroy : r.save + r.redirects_to_wiki_id = wiki_id + (r.title == r.redirects_to && r.wiki_id == r.redirects_to_wiki_id) ? r.destroy : r.save end # Remove redirects for the new title - wiki.redirects.where(:title => title).each(&:destroy) + WikiRedirect.where(:wiki_id => wiki_id, :title => title).delete_all # 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 + unless redirect_existing_links == "0" + WikiRedirect.create( + :wiki_id => wiki_id_was, :title => title_was, + :redirects_to_wiki_id => wiki_id, :redirects_to => title + ) + end + end + if !new_record? && wiki_id_changed? && parent.present? + unless parent.wiki_id == wiki_id + self.parent_id = nil + end end end + private :handle_rename_or_move + + # Moves child pages if page was moved + def handle_children_move + if !new_record? && wiki_id_changed? + children.each do |child| + child.wiki_id = wiki_id + child.redirect_existing_links = redirect_existing_links + unless child.save + WikiPage.where(:id => child.id).update_all :parent_nil => nil + end + end + end + end + private :handle_children_move - def remove_redirects - # Remove redirects to this page - wiki.redirects.where(:redirects_to => title).each(&:destroy) + # Deletes redirects to this page + def delete_redirects + WikiRedirect.where(:redirects_to_wiki_id => wiki_id, :redirects_to => title).delete_all end def pretty_title @@ -136,7 +174,7 @@ class WikiPage < ActiveRecord::Base end def project - wiki.project + wiki.try(:project) end def text @@ -196,7 +234,9 @@ class WikiPage < ActiveRecord::Base def validate_parent_title errors.add(:parent_title, :invalid) if !@parent_title.blank? && parent.nil? errors.add(:parent_title, :circular_dependency) if parent && (parent == self || parent.ancestors.include?(self)) - errors.add(:parent_title, :not_same_project) if parent && (parent.wiki_id != wiki_id) + if parent_id_changed? && parent && (parent.wiki_id != wiki_id) + errors.add(:parent_title, :not_same_project) + end end end |