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