@@ -1,307 +1,307 | |||
|
1 | 1 | # redMine - project management software |
|
2 | 2 | # Copyright (C) 2006-2007 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 | class Mailer < ActionMailer::Base |
|
19 | 19 | helper :application |
|
20 | 20 | helper :issues |
|
21 | 21 | helper :custom_fields |
|
22 | 22 | |
|
23 | 23 | include ActionController::UrlWriter |
|
24 | 24 | include Redmine::I18n |
|
25 | 25 | |
|
26 | def self.default_url_options | |
|
27 | h = Setting.host_name | |
|
28 | h = h.to_s.gsub(%r{\/.*$}, '') unless Redmine::Utils.relative_url_root.blank? | |
|
29 | { :host => h, :protocol => Setting.protocol } | |
|
30 | end | |
|
31 | ||
|
26 | 32 | def issue_add(issue) |
|
27 | 33 | redmine_headers 'Project' => issue.project.identifier, |
|
28 | 34 | 'Issue-Id' => issue.id, |
|
29 | 35 | 'Issue-Author' => issue.author.login |
|
30 | 36 | redmine_headers 'Issue-Assignee' => issue.assigned_to.login if issue.assigned_to |
|
31 | 37 | message_id issue |
|
32 | 38 | recipients issue.recipients |
|
33 | 39 | cc(issue.watcher_recipients - @recipients) |
|
34 | 40 | subject "[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}] (#{issue.status.name}) #{issue.subject}" |
|
35 | 41 | body :issue => issue, |
|
36 | 42 | :issue_url => url_for(:controller => 'issues', :action => 'show', :id => issue) |
|
37 | 43 | end |
|
38 | 44 | |
|
39 | 45 | def issue_edit(journal) |
|
40 | 46 | issue = journal.journalized |
|
41 | 47 | redmine_headers 'Project' => issue.project.identifier, |
|
42 | 48 | 'Issue-Id' => issue.id, |
|
43 | 49 | 'Issue-Author' => issue.author.login |
|
44 | 50 | redmine_headers 'Issue-Assignee' => issue.assigned_to.login if issue.assigned_to |
|
45 | 51 | message_id journal |
|
46 | 52 | references issue |
|
47 | 53 | @author = journal.user |
|
48 | 54 | recipients issue.recipients |
|
49 | 55 | # Watchers in cc |
|
50 | 56 | cc(issue.watcher_recipients - @recipients) |
|
51 | 57 | s = "[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}] " |
|
52 | 58 | s << "(#{issue.status.name}) " if journal.new_value_for('status_id') |
|
53 | 59 | s << issue.subject |
|
54 | 60 | subject s |
|
55 | 61 | body :issue => issue, |
|
56 | 62 | :journal => journal, |
|
57 | 63 | :issue_url => url_for(:controller => 'issues', :action => 'show', :id => issue) |
|
58 | 64 | end |
|
59 | 65 | |
|
60 | 66 | def reminder(user, issues, days) |
|
61 | 67 | set_language_if_valid user.language |
|
62 | 68 | recipients user.mail |
|
63 | 69 | subject l(:mail_subject_reminder, issues.size) |
|
64 | 70 | body :issues => issues, |
|
65 | 71 | :days => days, |
|
66 | 72 | :issues_url => url_for(:controller => 'issues', :action => 'index', :set_filter => 1, :assigned_to_id => user.id, :sort_key => 'due_date', :sort_order => 'asc') |
|
67 | 73 | end |
|
68 | 74 | |
|
69 | 75 | def document_added(document) |
|
70 | 76 | redmine_headers 'Project' => document.project.identifier |
|
71 | 77 | recipients document.project.recipients |
|
72 | 78 | subject "[#{document.project.name}] #{l(:label_document_new)}: #{document.title}" |
|
73 | 79 | body :document => document, |
|
74 | 80 | :document_url => url_for(:controller => 'documents', :action => 'show', :id => document) |
|
75 | 81 | end |
|
76 | 82 | |
|
77 | 83 | def attachments_added(attachments) |
|
78 | 84 | container = attachments.first.container |
|
79 | 85 | added_to = '' |
|
80 | 86 | added_to_url = '' |
|
81 | 87 | case container.class.name |
|
82 | 88 | when 'Project' |
|
83 | 89 | added_to_url = url_for(:controller => 'projects', :action => 'list_files', :id => container) |
|
84 | 90 | added_to = "#{l(:label_project)}: #{container}" |
|
85 | 91 | when 'Version' |
|
86 | 92 | added_to_url = url_for(:controller => 'projects', :action => 'list_files', :id => container.project_id) |
|
87 | 93 | added_to = "#{l(:label_version)}: #{container.name}" |
|
88 | 94 | when 'Document' |
|
89 | 95 | added_to_url = url_for(:controller => 'documents', :action => 'show', :id => container.id) |
|
90 | 96 | added_to = "#{l(:label_document)}: #{container.title}" |
|
91 | 97 | end |
|
92 | 98 | redmine_headers 'Project' => container.project.identifier |
|
93 | 99 | recipients container.project.recipients |
|
94 | 100 | subject "[#{container.project.name}] #{l(:label_attachment_new)}" |
|
95 | 101 | body :attachments => attachments, |
|
96 | 102 | :added_to => added_to, |
|
97 | 103 | :added_to_url => added_to_url |
|
98 | 104 | end |
|
99 | 105 | |
|
100 | 106 | def news_added(news) |
|
101 | 107 | redmine_headers 'Project' => news.project.identifier |
|
102 | 108 | message_id news |
|
103 | 109 | recipients news.project.recipients |
|
104 | 110 | subject "[#{news.project.name}] #{l(:label_news)}: #{news.title}" |
|
105 | 111 | body :news => news, |
|
106 | 112 | :news_url => url_for(:controller => 'news', :action => 'show', :id => news) |
|
107 | 113 | end |
|
108 | 114 | |
|
109 | 115 | def message_posted(message, recipients) |
|
110 | 116 | redmine_headers 'Project' => message.project.identifier, |
|
111 | 117 | 'Topic-Id' => (message.parent_id || message.id) |
|
112 | 118 | message_id message |
|
113 | 119 | references message.parent unless message.parent.nil? |
|
114 | 120 | recipients(recipients) |
|
115 | 121 | subject "[#{message.board.project.name} - #{message.board.name} - msg#{message.root.id}] #{message.subject}" |
|
116 | 122 | body :message => message, |
|
117 | 123 | :message_url => url_for(:controller => 'messages', :action => 'show', :board_id => message.board_id, :id => message.root) |
|
118 | 124 | end |
|
119 | 125 | |
|
120 | 126 | def account_information(user, password) |
|
121 | 127 | set_language_if_valid user.language |
|
122 | 128 | recipients user.mail |
|
123 | 129 | subject l(:mail_subject_register, Setting.app_title) |
|
124 | 130 | body :user => user, |
|
125 | 131 | :password => password, |
|
126 | 132 | :login_url => url_for(:controller => 'account', :action => 'login') |
|
127 | 133 | end |
|
128 | 134 | |
|
129 | 135 | def account_activation_request(user) |
|
130 | 136 | # Send the email to all active administrators |
|
131 | 137 | recipients User.active.find(:all, :conditions => {:admin => true}).collect { |u| u.mail }.compact |
|
132 | 138 | subject l(:mail_subject_account_activation_request, Setting.app_title) |
|
133 | 139 | body :user => user, |
|
134 | 140 | :url => url_for(:controller => 'users', :action => 'index', :status => User::STATUS_REGISTERED, :sort_key => 'created_on', :sort_order => 'desc') |
|
135 | 141 | end |
|
136 | 142 | |
|
137 | 143 | # A registered user's account was activated by an administrator |
|
138 | 144 | def account_activated(user) |
|
139 | 145 | set_language_if_valid user.language |
|
140 | 146 | recipients user.mail |
|
141 | 147 | subject l(:mail_subject_register, Setting.app_title) |
|
142 | 148 | body :user => user, |
|
143 | 149 | :login_url => url_for(:controller => 'account', :action => 'login') |
|
144 | 150 | end |
|
145 | 151 | |
|
146 | 152 | def lost_password(token) |
|
147 | 153 | set_language_if_valid(token.user.language) |
|
148 | 154 | recipients token.user.mail |
|
149 | 155 | subject l(:mail_subject_lost_password, Setting.app_title) |
|
150 | 156 | body :token => token, |
|
151 | 157 | :url => url_for(:controller => 'account', :action => 'lost_password', :token => token.value) |
|
152 | 158 | end |
|
153 | 159 | |
|
154 | 160 | def register(token) |
|
155 | 161 | set_language_if_valid(token.user.language) |
|
156 | 162 | recipients token.user.mail |
|
157 | 163 | subject l(:mail_subject_register, Setting.app_title) |
|
158 | 164 | body :token => token, |
|
159 | 165 | :url => url_for(:controller => 'account', :action => 'activate', :token => token.value) |
|
160 | 166 | end |
|
161 | 167 | |
|
162 | 168 | def test(user) |
|
163 | 169 | set_language_if_valid(user.language) |
|
164 | 170 | recipients user.mail |
|
165 | 171 | subject 'Redmine test' |
|
166 | 172 | body :url => url_for(:controller => 'welcome') |
|
167 | 173 | end |
|
168 | 174 | |
|
169 | 175 | # Overrides default deliver! method to prevent from sending an email |
|
170 | 176 | # with no recipient, cc or bcc |
|
171 | 177 | def deliver!(mail = @mail) |
|
172 | 178 | return false if (recipients.nil? || recipients.empty?) && |
|
173 | 179 | (cc.nil? || cc.empty?) && |
|
174 | 180 | (bcc.nil? || bcc.empty?) |
|
175 | 181 | |
|
176 | 182 | # Set Message-Id and References |
|
177 | 183 | if @message_id_object |
|
178 | 184 | mail.message_id = self.class.message_id_for(@message_id_object) |
|
179 | 185 | end |
|
180 | 186 | if @references_objects |
|
181 | 187 | mail.references = @references_objects.collect {|o| self.class.message_id_for(o)} |
|
182 | 188 | end |
|
183 | 189 | super(mail) |
|
184 | 190 | end |
|
185 | 191 | |
|
186 | 192 | # Sends reminders to issue assignees |
|
187 | 193 | # Available options: |
|
188 | 194 | # * :days => how many days in the future to remind about (defaults to 7) |
|
189 | 195 | # * :tracker => id of tracker for filtering issues (defaults to all trackers) |
|
190 | 196 | # * :project => id or identifier of project to process (defaults to all projects) |
|
191 | 197 | def self.reminders(options={}) |
|
192 | 198 | days = options[:days] || 7 |
|
193 | 199 | project = options[:project] ? Project.find(options[:project]) : nil |
|
194 | 200 | tracker = options[:tracker] ? Tracker.find(options[:tracker]) : nil |
|
195 | 201 | |
|
196 | 202 | s = ARCondition.new ["#{IssueStatus.table_name}.is_closed = ? AND #{Issue.table_name}.due_date <= ?", false, days.day.from_now.to_date] |
|
197 | 203 | s << "#{Issue.table_name}.assigned_to_id IS NOT NULL" |
|
198 | 204 | s << "#{Project.table_name}.status = #{Project::STATUS_ACTIVE}" |
|
199 | 205 | s << "#{Issue.table_name}.project_id = #{project.id}" if project |
|
200 | 206 | s << "#{Issue.table_name}.tracker_id = #{tracker.id}" if tracker |
|
201 | 207 | |
|
202 | 208 | issues_by_assignee = Issue.find(:all, :include => [:status, :assigned_to, :project, :tracker], |
|
203 | 209 | :conditions => s.conditions |
|
204 | 210 | ).group_by(&:assigned_to) |
|
205 | 211 | issues_by_assignee.each do |assignee, issues| |
|
206 | 212 | deliver_reminder(assignee, issues, days) unless assignee.nil? |
|
207 | 213 | end |
|
208 | 214 | end |
|
209 | 215 | |
|
210 | 216 | private |
|
211 | 217 | def initialize_defaults(method_name) |
|
212 | 218 | super |
|
213 | 219 | set_language_if_valid Setting.default_language |
|
214 | 220 | from Setting.mail_from |
|
215 | 221 | |
|
216 | # URL options | |
|
217 | h = Setting.host_name | |
|
218 | h = h.to_s.gsub(%r{\/.*$}, '') unless Redmine::Utils.relative_url_root.blank? | |
|
219 | default_url_options[:host] = h | |
|
220 | default_url_options[:protocol] = Setting.protocol | |
|
221 | ||
|
222 | 222 | # Common headers |
|
223 | 223 | headers 'X-Mailer' => 'Redmine', |
|
224 | 224 | 'X-Redmine-Host' => Setting.host_name, |
|
225 | 225 | 'X-Redmine-Site' => Setting.app_title |
|
226 | 226 | end |
|
227 | 227 | |
|
228 | 228 | # Appends a Redmine header field (name is prepended with 'X-Redmine-') |
|
229 | 229 | def redmine_headers(h) |
|
230 | 230 | h.each { |k,v| headers["X-Redmine-#{k}"] = v } |
|
231 | 231 | end |
|
232 | 232 | |
|
233 | 233 | # Overrides the create_mail method |
|
234 | 234 | def create_mail |
|
235 | 235 | # Removes the current user from the recipients and cc |
|
236 | 236 | # if he doesn't want to receive notifications about what he does |
|
237 | 237 | @author ||= User.current |
|
238 | 238 | if @author.pref[:no_self_notified] |
|
239 | 239 | recipients.delete(@author.mail) if recipients |
|
240 | 240 | cc.delete(@author.mail) if cc |
|
241 | 241 | end |
|
242 | 242 | # Blind carbon copy recipients |
|
243 | 243 | if Setting.bcc_recipients? |
|
244 | 244 | bcc([recipients, cc].flatten.compact.uniq) |
|
245 | 245 | recipients [] |
|
246 | 246 | cc [] |
|
247 | 247 | end |
|
248 | 248 | super |
|
249 | 249 | end |
|
250 | 250 | |
|
251 | 251 | # Renders a message with the corresponding layout |
|
252 | 252 | def render_message(method_name, body) |
|
253 | 253 | layout = method_name.to_s.match(%r{text\.html\.(rhtml|rxml)}) ? 'layout.text.html.rhtml' : 'layout.text.plain.rhtml' |
|
254 | 254 | body[:content_for_layout] = render(:file => method_name, :body => body) |
|
255 | 255 | ActionView::Base.new(template_root, body, self).render(:file => "mailer/#{layout}", :use_full_path => true) |
|
256 | 256 | end |
|
257 | 257 | |
|
258 | 258 | # for the case of plain text only |
|
259 | 259 | def body(*params) |
|
260 | 260 | value = super(*params) |
|
261 | 261 | if Setting.plain_text_mail? |
|
262 | 262 | templates = Dir.glob("#{template_path}/#{@template}.text.plain.{rhtml,erb}") |
|
263 | 263 | unless String === @body or templates.empty? |
|
264 | 264 | template = File.basename(templates.first) |
|
265 | 265 | @body[:content_for_layout] = render(:file => template, :body => @body) |
|
266 | 266 | @body = ActionView::Base.new(template_root, @body, self).render(:file => "mailer/layout.text.plain.rhtml", :use_full_path => true) |
|
267 | 267 | return @body |
|
268 | 268 | end |
|
269 | 269 | end |
|
270 | 270 | return value |
|
271 | 271 | end |
|
272 | 272 | |
|
273 | 273 | # Makes partial rendering work with Rails 1.2 (retro-compatibility) |
|
274 | 274 | def self.controller_path |
|
275 | 275 | '' |
|
276 | 276 | end unless respond_to?('controller_path') |
|
277 | 277 | |
|
278 | 278 | # Returns a predictable Message-Id for the given object |
|
279 | 279 | def self.message_id_for(object) |
|
280 | 280 | # id + timestamp should reduce the odds of a collision |
|
281 | 281 | # as far as we don't send multiple emails for the same object |
|
282 | 282 | hash = "redmine.#{object.class.name.demodulize.underscore}-#{object.id}.#{object.created_on.strftime("%Y%m%d%H%M%S")}" |
|
283 | 283 | host = Setting.mail_from.to_s.gsub(%r{^.*@}, '') |
|
284 | 284 | host = "#{::Socket.gethostname}.redmine" if host.empty? |
|
285 | 285 | "<#{hash}@#{host}>" |
|
286 | 286 | end |
|
287 | 287 | |
|
288 | 288 | private |
|
289 | 289 | |
|
290 | 290 | def message_id(object) |
|
291 | 291 | @message_id_object = object |
|
292 | 292 | end |
|
293 | 293 | |
|
294 | 294 | def references(object) |
|
295 | 295 | @references_objects ||= [] |
|
296 | 296 | @references_objects << object |
|
297 | 297 | end |
|
298 | 298 | end |
|
299 | 299 | |
|
300 | 300 | # Patch TMail so that message_id is not overwritten |
|
301 | 301 | module TMail |
|
302 | 302 | class Mail |
|
303 | 303 | def add_message_id( fqdn = nil ) |
|
304 | 304 | self.message_id ||= ::TMail::new_message_id(fqdn) |
|
305 | 305 | end |
|
306 | 306 | end |
|
307 | 307 | end |
@@ -1,146 +1,152 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2008 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 | module Redmine |
|
19 | 19 | module Hook |
|
20 | 20 | include ActionController::UrlWriter |
|
21 | 21 | |
|
22 | 22 | @@listener_classes = [] |
|
23 | 23 | @@listeners = nil |
|
24 | 24 | @@hook_listeners = {} |
|
25 | 25 | |
|
26 | 26 | class << self |
|
27 | 27 | # Adds a listener class. |
|
28 | 28 | # Automatically called when a class inherits from Redmine::Hook::Listener. |
|
29 | 29 | def add_listener(klass) |
|
30 | 30 | raise "Hooks must include Singleton module." unless klass.included_modules.include?(Singleton) |
|
31 | 31 | @@listener_classes << klass |
|
32 | 32 | clear_listeners_instances |
|
33 | 33 | end |
|
34 | 34 | |
|
35 | 35 | # Returns all the listerners instances. |
|
36 | 36 | def listeners |
|
37 | 37 | @@listeners ||= @@listener_classes.collect {|listener| listener.instance} |
|
38 | 38 | end |
|
39 | 39 | |
|
40 | 40 | # Returns the listeners instances for the given hook. |
|
41 | 41 | def hook_listeners(hook) |
|
42 | 42 | @@hook_listeners[hook] ||= listeners.select {|listener| listener.respond_to?(hook)} |
|
43 | 43 | end |
|
44 | 44 | |
|
45 | 45 | # Clears all the listeners. |
|
46 | 46 | def clear_listeners |
|
47 | 47 | @@listener_classes = [] |
|
48 | 48 | clear_listeners_instances |
|
49 | 49 | end |
|
50 | 50 | |
|
51 | 51 | # Clears all the listeners instances. |
|
52 | 52 | def clear_listeners_instances |
|
53 | 53 | @@listeners = nil |
|
54 | 54 | @@hook_listeners = {} |
|
55 | 55 | end |
|
56 | 56 | |
|
57 | 57 | # Calls a hook. |
|
58 | 58 | # Returns the listeners response. |
|
59 | 59 | def call_hook(hook, context={}) |
|
60 | 60 | returning [] do |response| |
|
61 | 61 | hls = hook_listeners(hook) |
|
62 | 62 | if hls.any? |
|
63 | default_url_options[:only_path] ||= true | |
|
64 | 63 | hls.each {|listener| response << listener.send(hook, context)} |
|
65 | 64 | end |
|
66 | 65 | end |
|
67 | 66 | end |
|
68 | 67 | end |
|
69 | 68 | |
|
70 | 69 | # Base class for hook listeners. |
|
71 | 70 | class Listener |
|
72 | 71 | include Singleton |
|
73 | 72 | include Redmine::I18n |
|
74 | 73 | |
|
75 | 74 | # Registers the listener |
|
76 | 75 | def self.inherited(child) |
|
77 | 76 | Redmine::Hook.add_listener(child) |
|
78 | 77 | super |
|
79 | 78 | end |
|
79 | ||
|
80 | 80 | end |
|
81 | 81 | |
|
82 | 82 | # Listener class used for views hooks. |
|
83 | 83 | # Listeners that inherit this class will include various helpers by default. |
|
84 | 84 | class ViewListener < Listener |
|
85 | 85 | include ERB::Util |
|
86 | 86 | include ActionView::Helpers::TagHelper |
|
87 | 87 | include ActionView::Helpers::FormHelper |
|
88 | 88 | include ActionView::Helpers::FormTagHelper |
|
89 | 89 | include ActionView::Helpers::FormOptionsHelper |
|
90 | 90 | include ActionView::Helpers::JavaScriptHelper |
|
91 | 91 | include ActionView::Helpers::PrototypeHelper |
|
92 | 92 | include ActionView::Helpers::NumberHelper |
|
93 | 93 | include ActionView::Helpers::UrlHelper |
|
94 | 94 | include ActionView::Helpers::AssetTagHelper |
|
95 | 95 | include ActionView::Helpers::TextHelper |
|
96 | 96 | include ActionController::UrlWriter |
|
97 | 97 | include ApplicationHelper |
|
98 | 98 | |
|
99 | # Default to creating links using only the path. Subclasses can | |
|
100 | # change this default as needed | |
|
101 | def self.default_url_options | |
|
102 | {:only_path => true } | |
|
103 | end | |
|
104 | ||
|
99 | 105 | # Helper method to directly render a partial using the context: |
|
100 | 106 | # |
|
101 | 107 | # class MyHook < Redmine::Hook::ViewListener |
|
102 | 108 | # render_on :view_issues_show_details_bottom, :partial => "show_more_data" |
|
103 | 109 | # end |
|
104 | 110 | # |
|
105 | 111 | def self.render_on(hook, options={}) |
|
106 | 112 | define_method hook do |context| |
|
107 | 113 | context[:controller].send(:render_to_string, {:locals => context}.merge(options)) |
|
108 | 114 | end |
|
109 | 115 | end |
|
110 | 116 | end |
|
111 | 117 | |
|
112 | 118 | # Helper module included in ApplicationHelper and ActionControllerso that |
|
113 | 119 | # hooks can be called in views like this: |
|
114 | 120 | # |
|
115 | 121 | # <%= call_hook(:some_hook) %> |
|
116 | 122 | # <%= call_hook(:another_hook, :foo => 'bar' %> |
|
117 | 123 | # |
|
118 | 124 | # Or in controllers like: |
|
119 | 125 | # call_hook(:some_hook) |
|
120 | 126 | # call_hook(:another_hook, :foo => 'bar' |
|
121 | 127 | # |
|
122 | 128 | # Hooks added to views will be concatenated into a string. Hooks added to |
|
123 | 129 | # controllers will return an array of results. |
|
124 | 130 | # |
|
125 | 131 | # Several objects are automatically added to the call context: |
|
126 | 132 | # |
|
127 | 133 | # * project => current project |
|
128 | 134 | # * request => Request instance |
|
129 | 135 | # * controller => current Controller instance |
|
130 | 136 | # |
|
131 | 137 | module Helper |
|
132 | 138 | def call_hook(hook, context={}) |
|
133 | 139 | if is_a?(ActionController::Base) |
|
134 | 140 | default_context = {:controller => self, :project => @project, :request => request} |
|
135 | 141 | Redmine::Hook.call_hook(hook, default_context.merge(context)) |
|
136 | 142 | else |
|
137 | 143 | default_context = {:controller => controller, :project => @project, :request => request} |
|
138 | 144 | Redmine::Hook.call_hook(hook, default_context.merge(context)).join(' ') |
|
139 | 145 | end |
|
140 | 146 | end |
|
141 | 147 | end |
|
142 | 148 | end |
|
143 | 149 | end |
|
144 | 150 | |
|
145 | 151 | ApplicationHelper.send(:include, Redmine::Hook::Helper) |
|
146 | 152 | ActionController::Base.send(:include, Redmine::Hook::Helper) |
@@ -1,148 +1,166 | |||
|
1 | 1 | # redMine - project management software |
|
2 | 2 | # Copyright (C) 2006-2008 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 File.dirname(__FILE__) + '/../../../test_helper' |
|
19 | 19 | |
|
20 | 20 | class Redmine::Hook::ManagerTest < Test::Unit::TestCase |
|
21 | 21 | |
|
22 | fixtures :issues | |
|
23 | ||
|
22 | 24 | # Some hooks that are manually registered in these tests |
|
23 | 25 | class TestHook < Redmine::Hook::ViewListener; end |
|
24 | 26 | |
|
25 | 27 | class TestHook1 < TestHook |
|
26 | 28 | def view_layouts_base_html_head(context) |
|
27 | 29 | 'Test hook 1 listener.' |
|
28 | 30 | end |
|
29 | 31 | end |
|
30 | 32 | |
|
31 | 33 | class TestHook2 < TestHook |
|
32 | 34 | def view_layouts_base_html_head(context) |
|
33 | 35 | 'Test hook 2 listener.' |
|
34 | 36 | end |
|
35 | 37 | end |
|
36 | 38 | |
|
37 | 39 | class TestHook3 < TestHook |
|
38 | 40 | def view_layouts_base_html_head(context) |
|
39 | 41 | "Context keys: #{context.keys.collect(&:to_s).sort.join(', ')}." |
|
40 | 42 | end |
|
41 | 43 | end |
|
42 | 44 | |
|
43 | 45 | class TestLinkToHook < TestHook |
|
44 | 46 | def view_layouts_base_html_head(context) |
|
45 | 47 | link_to('Issues', :controller => 'issues') |
|
46 | 48 | end |
|
47 | 49 | end |
|
48 | 50 | |
|
49 | 51 | class TestHookHelperController < ActionController::Base |
|
50 | 52 | include Redmine::Hook::Helper |
|
51 | 53 | end |
|
52 | 54 | |
|
53 | 55 | class TestHookHelperView < ActionView::Base |
|
54 | 56 | include Redmine::Hook::Helper |
|
55 | 57 | end |
|
56 | 58 | |
|
57 | 59 | Redmine::Hook.clear_listeners |
|
58 | 60 | |
|
59 | 61 | def setup |
|
60 | 62 | @hook_module = Redmine::Hook |
|
61 | 63 | @hook_helper = TestHookHelperController.new |
|
62 | 64 | @view_hook_helper = TestHookHelperView.new(RAILS_ROOT + '/app/views') |
|
63 | 65 | end |
|
64 | 66 | |
|
65 | 67 | def teardown |
|
66 | 68 | @hook_module.clear_listeners |
|
67 | @hook_module.default_url_options = { } | |
|
68 | 69 | end |
|
69 | 70 | |
|
70 | 71 | def test_clear_listeners |
|
71 | 72 | assert_equal 0, @hook_module.hook_listeners(:view_layouts_base_html_head).size |
|
72 | 73 | @hook_module.add_listener(TestHook1) |
|
73 | 74 | @hook_module.add_listener(TestHook2) |
|
74 | 75 | assert_equal 2, @hook_module.hook_listeners(:view_layouts_base_html_head).size |
|
75 | 76 | |
|
76 | 77 | @hook_module.clear_listeners |
|
77 | 78 | assert_equal 0, @hook_module.hook_listeners(:view_layouts_base_html_head).size |
|
78 | 79 | end |
|
79 | 80 | |
|
80 | 81 | def test_add_listener |
|
81 | 82 | assert_equal 0, @hook_module.hook_listeners(:view_layouts_base_html_head).size |
|
82 | 83 | @hook_module.add_listener(TestHook1) |
|
83 | 84 | assert_equal 1, @hook_module.hook_listeners(:view_layouts_base_html_head).size |
|
84 | 85 | end |
|
85 | 86 | |
|
86 | 87 | def test_call_hook |
|
87 | 88 | @hook_module.add_listener(TestHook1) |
|
88 | 89 | assert_equal ['Test hook 1 listener.'], @hook_helper.call_hook(:view_layouts_base_html_head) |
|
89 | 90 | end |
|
90 | 91 | |
|
91 | 92 | def test_call_hook_with_context |
|
92 | 93 | @hook_module.add_listener(TestHook3) |
|
93 | 94 | assert_equal ['Context keys: bar, controller, foo, project, request.'], |
|
94 | 95 | @hook_helper.call_hook(:view_layouts_base_html_head, :foo => 1, :bar => 'a') |
|
95 | 96 | end |
|
96 | 97 | |
|
97 | 98 | def test_call_hook_with_multiple_listeners |
|
98 | 99 | @hook_module.add_listener(TestHook1) |
|
99 | 100 | @hook_module.add_listener(TestHook2) |
|
100 | 101 | assert_equal ['Test hook 1 listener.', 'Test hook 2 listener.'], @hook_helper.call_hook(:view_layouts_base_html_head) |
|
101 | 102 | end |
|
102 | 103 | |
|
103 | 104 | # Context: Redmine::Hook::Helper.call_hook default_url |
|
104 | 105 | def test_call_hook_default_url_options |
|
105 | 106 | @hook_module.add_listener(TestLinkToHook) |
|
106 | 107 | |
|
107 | 108 | assert_equal ['<a href="/issues">Issues</a>'], @hook_helper.call_hook(:view_layouts_base_html_head) |
|
108 | 109 | end |
|
109 | 110 | |
|
110 | 111 | # Context: Redmine::Hook::Helper.call_hook |
|
111 | 112 | def test_call_hook_with_project_added_to_context |
|
112 | 113 | @hook_module.add_listener(TestHook3) |
|
113 | 114 | assert_match /project/i, @hook_helper.call_hook(:view_layouts_base_html_head)[0] |
|
114 | 115 | end |
|
115 | 116 | |
|
116 | 117 | def test_call_hook_from_controller_with_controller_added_to_context |
|
117 | 118 | @hook_module.add_listener(TestHook3) |
|
118 | 119 | assert_match /controller/i, @hook_helper.call_hook(:view_layouts_base_html_head)[0] |
|
119 | 120 | end |
|
120 | 121 | |
|
121 | 122 | def test_call_hook_from_controller_with_request_added_to_context |
|
122 | 123 | @hook_module.add_listener(TestHook3) |
|
123 | 124 | assert_match /request/i, @hook_helper.call_hook(:view_layouts_base_html_head)[0] |
|
124 | 125 | end |
|
125 | 126 | |
|
126 | 127 | def test_call_hook_from_view_with_project_added_to_context |
|
127 | 128 | @hook_module.add_listener(TestHook3) |
|
128 | 129 | assert_match /project/i, @view_hook_helper.call_hook(:view_layouts_base_html_head) |
|
129 | 130 | end |
|
130 | 131 | |
|
131 | 132 | def test_call_hook_from_view_with_controller_added_to_context |
|
132 | 133 | @hook_module.add_listener(TestHook3) |
|
133 | 134 | assert_match /controller/i, @view_hook_helper.call_hook(:view_layouts_base_html_head) |
|
134 | 135 | end |
|
135 | 136 | |
|
136 | 137 | def test_call_hook_from_view_with_request_added_to_context |
|
137 | 138 | @hook_module.add_listener(TestHook3) |
|
138 | 139 | assert_match /request/i, @view_hook_helper.call_hook(:view_layouts_base_html_head) |
|
139 | 140 | end |
|
140 | 141 | |
|
141 | 142 | def test_call_hook_from_view_should_join_responses_with_a_space |
|
142 | 143 | @hook_module.add_listener(TestHook1) |
|
143 | 144 | @hook_module.add_listener(TestHook2) |
|
144 | 145 | assert_equal 'Test hook 1 listener. Test hook 2 listener.', |
|
145 | 146 | @view_hook_helper.call_hook(:view_layouts_base_html_head) |
|
146 | 147 | end |
|
148 | ||
|
149 | def test_call_hook_should_not_change_the_default_url_for_email_notifications | |
|
150 | issue = Issue.find(1) | |
|
151 | ||
|
152 | ActionMailer::Base.deliveries.clear | |
|
153 | Mailer.deliver_issue_add(issue) | |
|
154 | mail = ActionMailer::Base.deliveries.last | |
|
155 | ||
|
156 | @hook_module.add_listener(TestLinkToHook) | |
|
157 | @hook_helper.call_hook(:view_layouts_base_html_head) | |
|
158 | ||
|
159 | ActionMailer::Base.deliveries.clear | |
|
160 | Mailer.deliver_issue_add(issue) | |
|
161 | mail2 = ActionMailer::Base.deliveries.last | |
|
162 | ||
|
163 | assert_equal mail.body, mail2.body | |
|
164 | end | |
|
147 | 165 | end |
|
148 | 166 |
General Comments 0
You need to be logged in to leave comments.
Login now