diff app/models/wiki_page.rb @ 909:cbb26bc654de redmine-1.3

Update to Redmine 1.3-stable branch (Redmine SVN rev 8964)
author Chris Cannam
date Fri, 24 Feb 2012 19:09:32 +0000
parents 0c939c159af4
children 433d4f72a19b
line wrap: on
line diff
--- a/app/models/wiki_page.rb	Fri Feb 24 18:36:29 2012 +0000
+++ b/app/models/wiki_page.rb	Fri Feb 24 19:09:32 2012 +0000
@@ -5,12 +5,12 @@
 # modify it under the terms of the GNU General Public License
 # as published by the Free Software Foundation; either version 2
 # of the License, or (at your option) any later version.
-# 
+#
 # This program is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 # GNU General Public License for more details.
-# 
+#
 # You should have received a copy of the GNU General Public License
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
@@ -30,33 +30,37 @@
                 :datetime => :created_on,
                 :url => Proc.new {|o| {:controller => 'wiki', :action => 'show', :project_id => o.wiki.project, :id => o.title}}
 
-  acts_as_searchable :columns => ['title', 'text'],
+  acts_as_searchable :columns => ['title', "#{WikiContent.table_name}.text"],
                      :include => [{:wiki => :project}, :content],
                      :permission => :view_wiki_pages,
                      :project_key => "#{Wiki.table_name}.project_id"
 
   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
-  
+
+  validate :validate_parent_title
+  before_destroy :remove_redirects
+  before_save    :handle_redirects
+
   # eager load information about last updates, without loading text
   named_scope :with_updated_on, {
     :select => "#{WikiPage.table_name}.*, #{WikiContent.table_name}.updated_on",
     :joins => "LEFT JOIN #{WikiContent.table_name} ON #{WikiContent.table_name}.page_id = #{WikiPage.table_name}.id"
   }
-  
+
   # Wiki pages that are protected by default
   DEFAULT_PROTECTED_PAGES = %w(sidebar)
-  
+
   def after_initialize
     if new_record? && DEFAULT_PROTECTED_PAGES.include?(title.to_s.downcase)
       self.protected = true
     end
   end
-  
+
   def visible?(user=User.current)
     !user.nil? && user.allowed_to?(:view_wiki_pages, project)
   end
@@ -67,8 +71,8 @@
     write_attribute(:title, value)
   end
 
-  def before_save
-    self.title = Wiki.titleize(title)    
+  def handle_redirects
+    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
@@ -83,51 +87,51 @@
       @previous_title = nil
     end
   end
-  
-  def before_destroy
+
+  def remove_redirects
     # Remove redirects to this page
     wiki.redirects.find_all_by_redirects_to(title).each(&:destroy)
   end
-  
+
   def pretty_title
     WikiPage.pretty_title(title)
   end
-  
+
   def content_for_version(version=nil)
     result = content.versions.find_by_version(version.to_i) if version
     result ||= content
     result
   end
-  
+
   def diff(version_to=nil, version_from=nil)
     version_to = version_to ? version_to.to_i : self.content.version
     version_from = version_from ? version_from.to_i : version_to - 1
     version_to, version_from = version_from, version_to unless version_from < version_to
-    
+
     content_to = content.versions.find_by_version(version_to)
     content_from = content.versions.find_by_version(version_from)
-    
+
     (content_to && content_from) ? WikiDiff.new(content_to, content_from) : nil
   end
-  
+
   def annotate(version=nil)
     version = version ? version.to_i : self.content.version
     c = content.versions.find_by_version(version)
     c ? WikiAnnotate.new(c) : nil
   end
-  
+
   def self.pretty_title(str)
     (str && str.is_a?(String)) ? str.tr('_', ' ') : str
   end
-  
+
   def project
     wiki.project
   end
-  
+
   def text
     content.text if content
   end
-  
+
   def updated_on
     unless @updated_on
       if time = read_attribute(:updated_on)
@@ -139,20 +143,20 @@
     end
     @updated_on
   end
-  
+
   # Returns true if usr is allowed to edit the page, otherwise false
   def editable_by?(usr)
     !protected? || usr.allowed_to?(:protect_wiki_pages, wiki.project)
   end
-        
+
   def attachments_deletable?(usr=User.current)
     editable_by?(usr) && super(usr)
   end
-  
+
   def parent_title
     @parent_title || (self.parent && self.parent.pretty_title)
   end
-  
+
   def parent_title=(t)
     @parent_title = t
     parent_page = t.blank? ? nil : self.wiki.find_page(t)
@@ -160,8 +164,8 @@
   end
 
   protected
-  
-  def validate
+
+  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)
@@ -170,7 +174,7 @@
 
 class WikiDiff < Redmine::Helpers::Diff
   attr_reader :content_to, :content_from
-  
+
   def initialize(content_to, content_from)
     @content_to = content_to
     @content_from = content_from
@@ -180,7 +184,7 @@
 
 class WikiAnnotate
   attr_reader :lines, :content
-  
+
   def initialize(content)
     @content = content
     current = content
@@ -212,7 +216,7 @@
       break unless @lines.detect { |line| line[0].nil? }
       current = current.previous
     end
-    @lines.each { |line| 
+    @lines.each { |line|
       line[0] ||= current.version
       # if the last known version is > 1 (eg. history was cleared), we don't know the author
       line[1] ||= current.author if current.version == 1