##// END OF EJS Templates
Adds attachments accessor to WikiContent::Version (#23242)....
Jean-Philippe Lang -
r15228:027fe7bb6622
parent child
Show More
@@ -1,170 +1,174
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2016 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'
23 23 belongs_to :author, :class_name => 'User'
24 24 validates_presence_of :text
25 25 validates_length_of :comments, :maximum => 1024, :allow_nil => true
26 26 attr_protected :id
27 27
28 28 acts_as_versioned
29 29
30 30 after_save :send_notification
31 31
32 32 scope :without_text, lambda {select(:id, :page_id, :version, :updated_on)}
33 33
34 34 def visible?(user=User.current)
35 35 page.visible?(user)
36 36 end
37 37
38 38 def project
39 39 page.project
40 40 end
41 41
42 42 def attachments
43 43 page.nil? ? [] : page.attachments
44 44 end
45 45
46 46 def notified_users
47 47 project.notified_users.reject {|user| !visible?(user)}
48 48 end
49 49
50 50 # Returns the mail addresses of users that should be notified
51 51 def recipients
52 52 notified_users.collect(&:mail)
53 53 end
54 54
55 55 # Return true if the content is the current page content
56 56 def current_version?
57 57 true
58 58 end
59 59
60 60 class Version
61 61 belongs_to :page, :class_name => '::WikiPage'
62 62 belongs_to :author, :class_name => '::User'
63 63 attr_protected :data
64 64
65 65 acts_as_event :title => Proc.new {|o| "#{l(:label_wiki_edit)}: #{o.page.title} (##{o.version})"},
66 66 :description => :comments,
67 67 :datetime => :updated_on,
68 68 :type => 'wiki-page',
69 69 :group => :page,
70 70 :url => Proc.new {|o| {:controller => 'wiki', :action => 'show', :project_id => o.page.wiki.project, :id => o.page.title, :version => o.version}}
71 71
72 72 acts_as_activity_provider :type => 'wiki_edits',
73 73 :timestamp => "#{WikiContent.versioned_table_name}.updated_on",
74 74 :author_key => "#{WikiContent.versioned_table_name}.author_id",
75 75 :permission => :view_wiki_edits,
76 76 :scope => select("#{WikiContent.versioned_table_name}.updated_on, #{WikiContent.versioned_table_name}.comments, " +
77 77 "#{WikiContent.versioned_table_name}.#{WikiContent.version_column}, #{WikiPage.table_name}.title, " +
78 78 "#{WikiContent.versioned_table_name}.page_id, #{WikiContent.versioned_table_name}.author_id, " +
79 79 "#{WikiContent.versioned_table_name}.id").
80 80 joins("LEFT JOIN #{WikiPage.table_name} ON #{WikiPage.table_name}.id = #{WikiContent.versioned_table_name}.page_id " +
81 81 "LEFT JOIN #{Wiki.table_name} ON #{Wiki.table_name}.id = #{WikiPage.table_name}.wiki_id " +
82 82 "LEFT JOIN #{Project.table_name} ON #{Project.table_name}.id = #{Wiki.table_name}.project_id")
83 83
84 84 after_destroy :page_update_after_destroy
85 85
86 86 def text=(plain)
87 87 case Setting.wiki_compression
88 88 when 'gzip'
89 89 begin
90 90 self.data = Zlib::Deflate.deflate(plain, Zlib::BEST_COMPRESSION)
91 91 self.compression = 'gzip'
92 92 rescue
93 93 self.data = plain
94 94 self.compression = ''
95 95 end
96 96 else
97 97 self.data = plain
98 98 self.compression = ''
99 99 end
100 100 plain
101 101 end
102 102
103 103 def text
104 104 @text ||= begin
105 105 str = case compression
106 106 when 'gzip'
107 107 Zlib::Inflate.inflate(data)
108 108 else
109 109 # uncompressed data
110 110 data
111 111 end
112 112 str.force_encoding("UTF-8")
113 113 str
114 114 end
115 115 end
116 116
117 117 def project
118 118 page.project
119 119 end
120 120
121 def attachments
122 page.nil? ? [] : page.attachments
123 end
124
121 125 # Return true if the content is the current page content
122 126 def current_version?
123 127 page.content.version == self.version
124 128 end
125 129
126 130 # Returns the previous version or nil
127 131 def previous
128 132 @previous ||= WikiContent::Version.
129 133 reorder('version DESC').
130 134 includes(:author).
131 135 where("wiki_content_id = ? AND version < ?", wiki_content_id, version).first
132 136 end
133 137
134 138 # Returns the next version or nil
135 139 def next
136 140 @next ||= WikiContent::Version.
137 141 reorder('version ASC').
138 142 includes(:author).
139 143 where("wiki_content_id = ? AND version > ?", wiki_content_id, version).first
140 144 end
141 145
142 146 private
143 147
144 148 # Updates page's content if the latest version is removed
145 149 # or destroys the page if it was the only version
146 150 def page_update_after_destroy
147 151 latest = page.content.versions.reorder("#{self.class.table_name}.version DESC").first
148 152 if latest && page.content.version != latest.version
149 153 raise ActiveRecord::Rollback unless page.content.revert_to!(latest)
150 154 elsif latest.nil?
151 155 raise ActiveRecord::Rollback unless page.destroy
152 156 end
153 157 end
154 158 end
155 159
156 160 private
157 161
158 162 def send_notification
159 163 # new_record? returns false in after_save callbacks
160 164 if id_changed?
161 165 if Setting.notified_events.include?('wiki_content_added')
162 166 Mailer.wiki_content_added(self).deliver
163 167 end
164 168 elsif text_changed?
165 169 if Setting.notified_events.include?('wiki_content_updated')
166 170 Mailer.wiki_content_updated(self).deliver
167 171 end
168 172 end
169 173 end
170 174 end
General Comments 0
You need to be logged in to leave comments. Login now