annotate .svn/pristine/92/92d58d10b91b7c7dfd6de735e3b6cd28edd15f9e.svn-base @ 1327:287f201c2802 redmine-2.2-integration

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