annotate .svn/pristine/c6/c670b46ea2087eed9b8431a525368aa41fa88c54.svn-base @ 1298:4f746d8966dd redmine_2.3_integration

Merge from redmine-2.3 branch to create new branch redmine-2.3-integration
author Chris Cannam
date Fri, 14 Jun 2013 09:28:30 +0100
parents 622f24f53b42
children
rev   line source
Chris@1295 1 # Redmine - project management software
Chris@1295 2 # Copyright (C) 2006-2013 Jean-Philippe Lang
Chris@1295 3 #
Chris@1295 4 # This program is free software; you can redistribute it and/or
Chris@1295 5 # modify it under the terms of the GNU General Public License
Chris@1295 6 # as published by the Free Software Foundation; either version 2
Chris@1295 7 # of the License, or (at your option) any later version.
Chris@1295 8 #
Chris@1295 9 # This program is distributed in the hope that it will be useful,
Chris@1295 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1295 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1295 12 # GNU General Public License for more details.
Chris@1295 13 #
Chris@1295 14 # You should have received a copy of the GNU General Public License
Chris@1295 15 # along with this program; if not, write to the Free Software
Chris@1295 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1295 17
Chris@1295 18 require 'zlib'
Chris@1295 19
Chris@1295 20 class WikiContent < ActiveRecord::Base
Chris@1295 21 self.locking_column = 'version'
Chris@1295 22 belongs_to :page, :class_name => 'WikiPage', :foreign_key => 'page_id'
Chris@1295 23 belongs_to :author, :class_name => 'User', :foreign_key => 'author_id'
Chris@1295 24 validates_presence_of :text
Chris@1295 25 validates_length_of :comments, :maximum => 255, :allow_nil => true
Chris@1295 26
Chris@1295 27 acts_as_versioned
Chris@1295 28
Chris@1295 29 def visible?(user=User.current)
Chris@1295 30 page.visible?(user)
Chris@1295 31 end
Chris@1295 32
Chris@1295 33 def project
Chris@1295 34 page.project
Chris@1295 35 end
Chris@1295 36
Chris@1295 37 def attachments
Chris@1295 38 page.nil? ? [] : page.attachments
Chris@1295 39 end
Chris@1295 40
Chris@1295 41 # Returns the mail adresses of users that should be notified
Chris@1295 42 def recipients
Chris@1295 43 notified = project.notified_users
Chris@1295 44 notified.reject! {|user| !visible?(user)}
Chris@1295 45 notified.collect(&:mail)
Chris@1295 46 end
Chris@1295 47
Chris@1295 48 # Return true if the content is the current page content
Chris@1295 49 def current_version?
Chris@1295 50 true
Chris@1295 51 end
Chris@1295 52
Chris@1295 53 class Version
Chris@1295 54 belongs_to :page, :class_name => '::WikiPage', :foreign_key => 'page_id'
Chris@1295 55 belongs_to :author, :class_name => '::User', :foreign_key => 'author_id'
Chris@1295 56 attr_protected :data
Chris@1295 57
Chris@1295 58 acts_as_event :title => Proc.new {|o| "#{l(:label_wiki_edit)}: #{o.page.title} (##{o.version})"},
Chris@1295 59 :description => :comments,
Chris@1295 60 :datetime => :updated_on,
Chris@1295 61 :type => 'wiki-page',
Chris@1295 62 :group => :page,
Chris@1295 63 :url => Proc.new {|o| {:controller => 'wiki', :action => 'show', :project_id => o.page.wiki.project, :id => o.page.title, :version => o.version}}
Chris@1295 64
Chris@1295 65 acts_as_activity_provider :type => 'wiki_edits',
Chris@1295 66 :timestamp => "#{WikiContent.versioned_table_name}.updated_on",
Chris@1295 67 :author_key => "#{WikiContent.versioned_table_name}.author_id",
Chris@1295 68 :permission => :view_wiki_edits,
Chris@1295 69 :find_options => {:select => "#{WikiContent.versioned_table_name}.updated_on, #{WikiContent.versioned_table_name}.comments, " +
Chris@1295 70 "#{WikiContent.versioned_table_name}.#{WikiContent.version_column}, #{WikiPage.table_name}.title, " +
Chris@1295 71 "#{WikiContent.versioned_table_name}.page_id, #{WikiContent.versioned_table_name}.author_id, " +
Chris@1295 72 "#{WikiContent.versioned_table_name}.id",
Chris@1295 73 :joins => "LEFT JOIN #{WikiPage.table_name} ON #{WikiPage.table_name}.id = #{WikiContent.versioned_table_name}.page_id " +
Chris@1295 74 "LEFT JOIN #{Wiki.table_name} ON #{Wiki.table_name}.id = #{WikiPage.table_name}.wiki_id " +
Chris@1295 75 "LEFT JOIN #{Project.table_name} ON #{Project.table_name}.id = #{Wiki.table_name}.project_id"}
Chris@1295 76
Chris@1295 77 after_destroy :page_update_after_destroy
Chris@1295 78
Chris@1295 79 def text=(plain)
Chris@1295 80 case Setting.wiki_compression
Chris@1295 81 when 'gzip'
Chris@1295 82 begin
Chris@1295 83 self.data = Zlib::Deflate.deflate(plain, Zlib::BEST_COMPRESSION)
Chris@1295 84 self.compression = 'gzip'
Chris@1295 85 rescue
Chris@1295 86 self.data = plain
Chris@1295 87 self.compression = ''
Chris@1295 88 end
Chris@1295 89 else
Chris@1295 90 self.data = plain
Chris@1295 91 self.compression = ''
Chris@1295 92 end
Chris@1295 93 plain
Chris@1295 94 end
Chris@1295 95
Chris@1295 96 def text
Chris@1295 97 @text ||= begin
Chris@1295 98 str = case compression
Chris@1295 99 when 'gzip'
Chris@1295 100 Zlib::Inflate.inflate(data)
Chris@1295 101 else
Chris@1295 102 # uncompressed data
Chris@1295 103 data
Chris@1295 104 end
Chris@1295 105 str.force_encoding("UTF-8") if str.respond_to?(:force_encoding)
Chris@1295 106 str
Chris@1295 107 end
Chris@1295 108 end
Chris@1295 109
Chris@1295 110 def project
Chris@1295 111 page.project
Chris@1295 112 end
Chris@1295 113
Chris@1295 114 # Return true if the content is the current page content
Chris@1295 115 def current_version?
Chris@1295 116 page.content.version == self.version
Chris@1295 117 end
Chris@1295 118
Chris@1295 119 # Returns the previous version or nil
Chris@1295 120 def previous
Chris@1295 121 @previous ||= WikiContent::Version.
Chris@1295 122 reorder('version DESC').
Chris@1295 123 includes(:author).
Chris@1295 124 where("wiki_content_id = ? AND version < ?", wiki_content_id, version).first
Chris@1295 125 end
Chris@1295 126
Chris@1295 127 # Returns the next version or nil
Chris@1295 128 def next
Chris@1295 129 @next ||= WikiContent::Version.
Chris@1295 130 reorder('version ASC').
Chris@1295 131 includes(:author).
Chris@1295 132 where("wiki_content_id = ? AND version > ?", wiki_content_id, version).first
Chris@1295 133 end
Chris@1295 134
Chris@1295 135 private
Chris@1295 136
Chris@1295 137 # Updates page's content if the latest version is removed
Chris@1295 138 # or destroys the page if it was the only version
Chris@1295 139 def page_update_after_destroy
Chris@1295 140 latest = page.content.versions.reorder("#{self.class.table_name}.version DESC").first
Chris@1295 141 if latest && page.content.version != latest.version
Chris@1295 142 raise ActiveRecord::Rollback unless page.content.revert_to!(latest)
Chris@1295 143 elsif latest.nil?
Chris@1295 144 raise ActiveRecord::Rollback unless page.destroy
Chris@1295 145 end
Chris@1295 146 end
Chris@1295 147 end
Chris@1295 148 end