##// END OF EJS Templates
Adds pagination to forum messages (#4664)....
Jean-Philippe Lang -
r3259:8fb29d4d211a
parent child
Show More
@@ -1,132 +1,146
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 MessagesController < ApplicationController
19 19 menu_item :boards
20 20 default_search_scope :messages
21 21 before_filter :find_board, :only => [:new, :preview]
22 22 before_filter :find_message, :except => [:new, :preview]
23 23 before_filter :authorize, :except => [:preview, :edit, :destroy]
24 24
25 25 verify :method => :post, :only => [ :reply, :destroy ], :redirect_to => { :action => :show }
26 26 verify :xhr => true, :only => :quote
27 27
28 28 helper :watchers
29 29 helper :attachments
30 30 include AttachmentsHelper
31 31
32 REPLIES_PER_PAGE = 25 unless const_defined?(:REPLIES_PER_PAGE)
33
32 34 # Show a topic and its replies
33 35 def show
34 @replies = @topic.children.find(:all, :include => [:author, :attachments, {:board => :project}])
35 @replies.reverse! if User.current.wants_comments_in_reverse_order?
36 page = params[:page]
37 # Find the page of the requested reply
38 if params[:r] && page.nil?
39 offset = @topic.children.count(:conditions => ["#{Message.table_name}.id < ?", params[:r].to_i])
40 page = 1 + offset / REPLIES_PER_PAGE
41 end
42
43 @reply_count = @topic.children.count
44 @reply_pages = Paginator.new self, @reply_count, REPLIES_PER_PAGE, page
45 @replies = @topic.children.find(:all, :include => [:author, :attachments, {:board => :project}],
46 :order => "#{Message.table_name}.created_on ASC",
47 :limit => @reply_pages.items_per_page,
48 :offset => @reply_pages.current.offset)
49
36 50 @reply = Message.new(:subject => "RE: #{@message.subject}")
37 51 render :action => "show", :layout => false if request.xhr?
38 52 end
39 53
40 54 # Create a new topic
41 55 def new
42 56 @message = Message.new(params[:message])
43 57 @message.author = User.current
44 58 @message.board = @board
45 59 if params[:message] && User.current.allowed_to?(:edit_messages, @project)
46 60 @message.locked = params[:message]['locked']
47 61 @message.sticky = params[:message]['sticky']
48 62 end
49 63 if request.post? && @message.save
50 64 call_hook(:controller_messages_new_after_save, { :params => params, :message => @message})
51 65 attach_files(@message, params[:attachments])
52 66 redirect_to :action => 'show', :id => @message
53 67 end
54 68 end
55 69
56 70 # Reply to a topic
57 71 def reply
58 72 @reply = Message.new(params[:reply])
59 73 @reply.author = User.current
60 74 @reply.board = @board
61 75 @topic.children << @reply
62 76 if !@reply.new_record?
63 77 call_hook(:controller_messages_reply_after_save, { :params => params, :message => @reply})
64 78 attach_files(@reply, params[:attachments])
65 79 end
66 redirect_to :action => 'show', :id => @topic
80 redirect_to :action => 'show', :id => @topic, :r => @reply
67 81 end
68 82
69 83 # Edit a message
70 84 def edit
71 85 (render_403; return false) unless @message.editable_by?(User.current)
72 86 if params[:message]
73 87 @message.locked = params[:message]['locked']
74 88 @message.sticky = params[:message]['sticky']
75 89 end
76 90 if request.post? && @message.update_attributes(params[:message])
77 91 attach_files(@message, params[:attachments])
78 92 flash[:notice] = l(:notice_successful_update)
79 93 @message.reload
80 redirect_to :action => 'show', :board_id => @message.board, :id => @message.root
94 redirect_to :action => 'show', :board_id => @message.board, :id => @message.root, :r => (@message.parent_id && @message.id)
81 95 end
82 96 end
83 97
84 98 # Delete a messages
85 99 def destroy
86 100 (render_403; return false) unless @message.destroyable_by?(User.current)
87 101 @message.destroy
88 102 redirect_to @message.parent.nil? ?
89 103 { :controller => 'boards', :action => 'show', :project_id => @project, :id => @board } :
90 { :action => 'show', :id => @message.parent }
104 { :action => 'show', :id => @message.parent, :r => @message }
91 105 end
92 106
93 107 def quote
94 108 user = @message.author
95 109 text = @message.content
96 110 subject = @message.subject.gsub('"', '\"')
97 111 subject = "RE: #{subject}" unless subject.starts_with?('RE:')
98 112 content = "#{ll(Setting.default_language, :text_user_wrote, user)}\\n> "
99 113 content << text.to_s.strip.gsub(%r{<pre>((.|\s)*?)</pre>}m, '[...]').gsub('"', '\"').gsub(/(\r?\n|\r\n?)/, "\\n> ") + "\\n\\n"
100 114 render(:update) { |page|
101 115 page << "$('reply_subject').value = \"#{subject}\";"
102 116 page.<< "$('message_content').value = \"#{content}\";"
103 117 page.show 'reply'
104 118 page << "Form.Element.focus('message_content');"
105 119 page << "Element.scrollTo('reply');"
106 120 page << "$('message_content').scrollTop = $('message_content').scrollHeight - $('message_content').clientHeight;"
107 121 }
108 122 end
109 123
110 124 def preview
111 125 message = @board.messages.find_by_id(params[:id])
112 126 @attachements = message.attachments if message
113 127 @text = (params[:message] || params[:reply])[:content]
114 128 render :partial => 'common/preview'
115 129 end
116 130
117 131 private
118 132 def find_message
119 133 find_board
120 134 @message = @board.messages.find(params[:id], :include => :parent)
121 135 @topic = @message.root
122 136 rescue ActiveRecord::RecordNotFound
123 137 render_404
124 138 end
125 139
126 140 def find_board
127 141 @board = Board.find(params[:board_id], :include => :project)
128 142 @project = @board.project
129 143 rescue ActiveRecord::RecordNotFound
130 144 render_404
131 145 end
132 146 end
@@ -1,733 +1,734
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 require 'coderay'
19 19 require 'coderay/helpers/file_type'
20 20 require 'forwardable'
21 21 require 'cgi'
22 22
23 23 module ApplicationHelper
24 24 include Redmine::WikiFormatting::Macros::Definitions
25 25 include Redmine::I18n
26 26 include GravatarHelper::PublicMethods
27 27
28 28 extend Forwardable
29 29 def_delegators :wiki_helper, :wikitoolbar_for, :heads_for_wiki_formatter
30 30
31 31 # Return true if user is authorized for controller/action, otherwise false
32 32 def authorize_for(controller, action)
33 33 User.current.allowed_to?({:controller => controller, :action => action}, @project)
34 34 end
35 35
36 36 # Display a link if user is authorized
37 37 def link_to_if_authorized(name, options = {}, html_options = nil, *parameters_for_method_reference)
38 38 link_to(name, options, html_options, *parameters_for_method_reference) if authorize_for(options[:controller] || params[:controller], options[:action])
39 39 end
40 40
41 41 # Display a link to remote if user is authorized
42 42 def link_to_remote_if_authorized(name, options = {}, html_options = nil)
43 43 url = options[:url] || {}
44 44 link_to_remote(name, options, html_options) if authorize_for(url[:controller] || params[:controller], url[:action])
45 45 end
46 46
47 47 # Displays a link to user's account page if active
48 48 def link_to_user(user, options={})
49 49 if user.is_a?(User)
50 50 name = h(user.name(options[:format]))
51 51 if user.active?
52 52 link_to name, :controller => 'users', :action => 'show', :id => user
53 53 else
54 54 name
55 55 end
56 56 else
57 57 h(user.to_s)
58 58 end
59 59 end
60 60
61 61 # Displays a link to +issue+ with its subject.
62 62 # Examples:
63 63 #
64 64 # link_to_issue(issue) # => Defect #6: This is the subject
65 65 # link_to_issue(issue, :truncate => 6) # => Defect #6: This i...
66 66 # link_to_issue(issue, :subject => false) # => Defect #6
67 67 # link_to_issue(issue, :project => true) # => Foo - Defect #6
68 68 #
69 69 def link_to_issue(issue, options={})
70 70 title = nil
71 71 subject = nil
72 72 if options[:subject] == false
73 73 title = truncate(issue.subject, :length => 60)
74 74 else
75 75 subject = issue.subject
76 76 if options[:truncate]
77 77 subject = truncate(subject, :length => options[:truncate])
78 78 end
79 79 end
80 80 s = link_to "#{issue.tracker} ##{issue.id}", {:controller => "issues", :action => "show", :id => issue},
81 81 :class => issue.css_classes,
82 82 :title => title
83 83 s << ": #{h subject}" if subject
84 84 s = "#{h issue.project} - " + s if options[:project]
85 85 s
86 86 end
87 87
88 88 # Generates a link to an attachment.
89 89 # Options:
90 90 # * :text - Link text (default to attachment filename)
91 91 # * :download - Force download (default: false)
92 92 def link_to_attachment(attachment, options={})
93 93 text = options.delete(:text) || attachment.filename
94 94 action = options.delete(:download) ? 'download' : 'show'
95 95
96 96 link_to(h(text), {:controller => 'attachments', :action => action, :id => attachment, :filename => attachment.filename }, options)
97 97 end
98 98
99 99 # Generates a link to a SCM revision
100 100 # Options:
101 101 # * :text - Link text (default to the formatted revision)
102 102 def link_to_revision(revision, project, options={})
103 103 text = options.delete(:text) || format_revision(revision)
104 104
105 105 link_to(text, {:controller => 'repositories', :action => 'revision', :id => project, :rev => revision}, :title => l(:label_revision_id, revision))
106 106 end
107 107
108 108 def toggle_link(name, id, options={})
109 109 onclick = "Element.toggle('#{id}'); "
110 110 onclick << (options[:focus] ? "Form.Element.focus('#{options[:focus]}'); " : "this.blur(); ")
111 111 onclick << "return false;"
112 112 link_to(name, "#", :onclick => onclick)
113 113 end
114 114
115 115 def image_to_function(name, function, html_options = {})
116 116 html_options.symbolize_keys!
117 117 tag(:input, html_options.merge({
118 118 :type => "image", :src => image_path(name),
119 119 :onclick => (html_options[:onclick] ? "#{html_options[:onclick]}; " : "") + "#{function};"
120 120 }))
121 121 end
122 122
123 123 def prompt_to_remote(name, text, param, url, html_options = {})
124 124 html_options[:onclick] = "promptToRemote('#{text}', '#{param}', '#{url_for(url)}'); return false;"
125 125 link_to name, {}, html_options
126 126 end
127 127
128 128 def format_activity_title(text)
129 129 h(truncate_single_line(text, :length => 100))
130 130 end
131 131
132 132 def format_activity_day(date)
133 133 date == Date.today ? l(:label_today).titleize : format_date(date)
134 134 end
135 135
136 136 def format_activity_description(text)
137 137 h(truncate(text.to_s, :length => 120).gsub(%r{[\r\n]*<(pre|code)>.*$}m, '...')).gsub(/[\r\n]+/, "<br />")
138 138 end
139 139
140 140 def format_version_name(version)
141 141 if version.project == @project
142 142 h(version)
143 143 else
144 144 h("#{version.project} - #{version}")
145 145 end
146 146 end
147 147
148 148 def due_date_distance_in_words(date)
149 149 if date
150 150 l((date < Date.today ? :label_roadmap_overdue : :label_roadmap_due_in), distance_of_date_in_words(Date.today, date))
151 151 end
152 152 end
153 153
154 154 def render_page_hierarchy(pages, node=nil)
155 155 content = ''
156 156 if pages[node]
157 157 content << "<ul class=\"pages-hierarchy\">\n"
158 158 pages[node].each do |page|
159 159 content << "<li>"
160 160 content << link_to(h(page.pretty_title), {:controller => 'wiki', :action => 'index', :id => page.project, :page => page.title},
161 161 :title => (page.respond_to?(:updated_on) ? l(:label_updated_time, distance_of_time_in_words(Time.now, page.updated_on)) : nil))
162 162 content << "\n" + render_page_hierarchy(pages, page.id) if pages[page.id]
163 163 content << "</li>\n"
164 164 end
165 165 content << "</ul>\n"
166 166 end
167 167 content
168 168 end
169 169
170 170 # Renders flash messages
171 171 def render_flash_messages
172 172 s = ''
173 173 flash.each do |k,v|
174 174 s << content_tag('div', v, :class => "flash #{k}")
175 175 end
176 176 s
177 177 end
178 178
179 179 # Renders tabs and their content
180 180 def render_tabs(tabs)
181 181 if tabs.any?
182 182 render :partial => 'common/tabs', :locals => {:tabs => tabs}
183 183 else
184 184 content_tag 'p', l(:label_no_data), :class => "nodata"
185 185 end
186 186 end
187 187
188 188 # Renders the project quick-jump box
189 189 def render_project_jump_box
190 190 # Retrieve them now to avoid a COUNT query
191 191 projects = User.current.projects.all
192 192 if projects.any?
193 193 s = '<select onchange="if (this.value != \'\') { window.location = this.value; }">' +
194 194 "<option value=''>#{ l(:label_jump_to_a_project) }</option>" +
195 195 '<option value="" disabled="disabled">---</option>'
196 196 s << project_tree_options_for_select(projects, :selected => @project) do |p|
197 197 { :value => url_for(:controller => 'projects', :action => 'show', :id => p, :jump => current_menu_item) }
198 198 end
199 199 s << '</select>'
200 200 s
201 201 end
202 202 end
203 203
204 204 def project_tree_options_for_select(projects, options = {})
205 205 s = ''
206 206 project_tree(projects) do |project, level|
207 207 name_prefix = (level > 0 ? ('&nbsp;' * 2 * level + '&#187; ') : '')
208 208 tag_options = {:value => project.id, :selected => ((project == options[:selected]) ? 'selected' : nil)}
209 209 tag_options.merge!(yield(project)) if block_given?
210 210 s << content_tag('option', name_prefix + h(project), tag_options)
211 211 end
212 212 s
213 213 end
214 214
215 215 # Yields the given block for each project with its level in the tree
216 216 def project_tree(projects, &block)
217 217 ancestors = []
218 218 projects.sort_by(&:lft).each do |project|
219 219 while (ancestors.any? && !project.is_descendant_of?(ancestors.last))
220 220 ancestors.pop
221 221 end
222 222 yield project, ancestors.size
223 223 ancestors << project
224 224 end
225 225 end
226 226
227 227 def project_nested_ul(projects, &block)
228 228 s = ''
229 229 if projects.any?
230 230 ancestors = []
231 231 projects.sort_by(&:lft).each do |project|
232 232 if (ancestors.empty? || project.is_descendant_of?(ancestors.last))
233 233 s << "<ul>\n"
234 234 else
235 235 ancestors.pop
236 236 s << "</li>"
237 237 while (ancestors.any? && !project.is_descendant_of?(ancestors.last))
238 238 ancestors.pop
239 239 s << "</ul></li>\n"
240 240 end
241 241 end
242 242 s << "<li>"
243 243 s << yield(project).to_s
244 244 ancestors << project
245 245 end
246 246 s << ("</li></ul>\n" * ancestors.size)
247 247 end
248 248 s
249 249 end
250 250
251 251 def principals_check_box_tags(name, principals)
252 252 s = ''
253 253 principals.sort.each do |principal|
254 254 s << "<label>#{ check_box_tag name, principal.id, false } #{h principal}</label>\n"
255 255 end
256 256 s
257 257 end
258 258
259 259 # Truncates and returns the string as a single line
260 260 def truncate_single_line(string, *args)
261 261 truncate(string.to_s, *args).gsub(%r{[\r\n]+}m, ' ')
262 262 end
263 263
264 264 def html_hours(text)
265 265 text.gsub(%r{(\d+)\.(\d+)}, '<span class="hours hours-int">\1</span><span class="hours hours-dec">.\2</span>')
266 266 end
267 267
268 268 def authoring(created, author, options={})
269 269 l(options[:label] || :label_added_time_by, :author => link_to_user(author), :age => time_tag(created))
270 270 end
271 271
272 272 def time_tag(time)
273 273 text = distance_of_time_in_words(Time.now, time)
274 274 if @project
275 275 link_to(text, {:controller => 'projects', :action => 'activity', :id => @project, :from => time.to_date}, :title => format_time(time))
276 276 else
277 277 content_tag('acronym', text, :title => format_time(time))
278 278 end
279 279 end
280 280
281 281 def syntax_highlight(name, content)
282 282 type = CodeRay::FileType[name]
283 283 type ? CodeRay.scan(content, type).html : h(content)
284 284 end
285 285
286 286 def to_path_param(path)
287 287 path.to_s.split(%r{[/\\]}).select {|p| !p.blank?}
288 288 end
289 289
290 290 def pagination_links_full(paginator, count=nil, options={})
291 291 page_param = options.delete(:page_param) || :page
292 per_page_links = options.delete(:per_page_links)
292 293 url_param = params.dup
293 294 # don't reuse query params if filters are present
294 295 url_param.merge!(:fields => nil, :values => nil, :operators => nil) if url_param.delete(:set_filter)
295 296
296 297 html = ''
297 298 if paginator.current.previous
298 299 html << link_to_remote_content_update('&#171; ' + l(:label_previous), url_param.merge(page_param => paginator.current.previous)) + ' '
299 300 end
300 301
301 302 html << (pagination_links_each(paginator, options) do |n|
302 303 link_to_remote_content_update(n.to_s, url_param.merge(page_param => n))
303 304 end || '')
304 305
305 306 if paginator.current.next
306 307 html << ' ' + link_to_remote_content_update((l(:label_next) + ' &#187;'), url_param.merge(page_param => paginator.current.next))
307 308 end
308 309
309 310 unless count.nil?
310 html << [
311 " (#{paginator.current.first_item}-#{paginator.current.last_item}/#{count})",
312 per_page_links(paginator.items_per_page)
313 ].compact.join(' | ')
311 html << " (#{paginator.current.first_item}-#{paginator.current.last_item}/#{count})"
312 if per_page_links != false && links = per_page_links(paginator.items_per_page)
313 html << " | #{links}"
314 end
314 315 end
315 316
316 317 html
317 318 end
318 319
319 320 def per_page_links(selected=nil)
320 321 url_param = params.dup
321 322 url_param.clear if url_param.has_key?(:set_filter)
322 323
323 324 links = Setting.per_page_options_array.collect do |n|
324 325 n == selected ? n : link_to_remote(n, {:update => "content",
325 326 :url => params.dup.merge(:per_page => n),
326 327 :method => :get},
327 328 {:href => url_for(url_param.merge(:per_page => n))})
328 329 end
329 330 links.size > 1 ? l(:label_display_per_page, links.join(', ')) : nil
330 331 end
331 332
332 333 def reorder_links(name, url)
333 334 link_to(image_tag('2uparrow.png', :alt => l(:label_sort_highest)), url.merge({"#{name}[move_to]" => 'highest'}), :method => :post, :title => l(:label_sort_highest)) +
334 335 link_to(image_tag('1uparrow.png', :alt => l(:label_sort_higher)), url.merge({"#{name}[move_to]" => 'higher'}), :method => :post, :title => l(:label_sort_higher)) +
335 336 link_to(image_tag('1downarrow.png', :alt => l(:label_sort_lower)), url.merge({"#{name}[move_to]" => 'lower'}), :method => :post, :title => l(:label_sort_lower)) +
336 337 link_to(image_tag('2downarrow.png', :alt => l(:label_sort_lowest)), url.merge({"#{name}[move_to]" => 'lowest'}), :method => :post, :title => l(:label_sort_lowest))
337 338 end
338 339
339 340 def breadcrumb(*args)
340 341 elements = args.flatten
341 342 elements.any? ? content_tag('p', args.join(' &#187; ') + ' &#187; ', :class => 'breadcrumb') : nil
342 343 end
343 344
344 345 def other_formats_links(&block)
345 346 concat('<p class="other-formats">' + l(:label_export_to))
346 347 yield Redmine::Views::OtherFormatsBuilder.new(self)
347 348 concat('</p>')
348 349 end
349 350
350 351 def page_header_title
351 352 if @project.nil? || @project.new_record?
352 353 h(Setting.app_title)
353 354 else
354 355 b = []
355 356 ancestors = (@project.root? ? [] : @project.ancestors.visible)
356 357 if ancestors.any?
357 358 root = ancestors.shift
358 359 b << link_to(h(root), {:controller => 'projects', :action => 'show', :id => root, :jump => current_menu_item}, :class => 'root')
359 360 if ancestors.size > 2
360 361 b << '&#8230;'
361 362 ancestors = ancestors[-2, 2]
362 363 end
363 364 b += ancestors.collect {|p| link_to(h(p), {:controller => 'projects', :action => 'show', :id => p, :jump => current_menu_item}, :class => 'ancestor') }
364 365 end
365 366 b << h(@project)
366 367 b.join(' &#187; ')
367 368 end
368 369 end
369 370
370 371 def html_title(*args)
371 372 if args.empty?
372 373 title = []
373 374 title << @project.name if @project
374 375 title += @html_title if @html_title
375 376 title << Setting.app_title
376 377 title.select {|t| !t.blank? }.join(' - ')
377 378 else
378 379 @html_title ||= []
379 380 @html_title += args
380 381 end
381 382 end
382 383
383 384 def accesskey(s)
384 385 Redmine::AccessKeys.key_for s
385 386 end
386 387
387 388 # Formats text according to system settings.
388 389 # 2 ways to call this method:
389 390 # * with a String: textilizable(text, options)
390 391 # * with an object and one of its attribute: textilizable(issue, :description, options)
391 392 def textilizable(*args)
392 393 options = args.last.is_a?(Hash) ? args.pop : {}
393 394 case args.size
394 395 when 1
395 396 obj = options[:object]
396 397 text = args.shift
397 398 when 2
398 399 obj = args.shift
399 400 attr = args.shift
400 401 text = obj.send(attr).to_s
401 402 else
402 403 raise ArgumentError, 'invalid arguments to textilizable'
403 404 end
404 405 return '' if text.blank?
405 406
406 407 text = Redmine::WikiFormatting.to_html(Setting.text_formatting, text, :object => obj, :attribute => attr) { |macro, args| exec_macro(macro, obj, args) }
407 408
408 409 only_path = options.delete(:only_path) == false ? false : true
409 410
410 411 # when using an image link, try to use an attachment, if possible
411 412 attachments = options[:attachments] || (obj && obj.respond_to?(:attachments) ? obj.attachments : nil)
412 413
413 414 if attachments
414 415 attachments = attachments.sort_by(&:created_on).reverse
415 416 text.gsub!(/src="([^\/"]+\.(bmp|gif|jpg|jpeg|png))"(\s+alt="([^"]*)")?/i) do |m|
416 417 filename, ext, alt, alttext = $1.downcase, $2, $3, $4
417 418
418 419 # search for the picture in attachments
419 420 if found = attachments.detect { |att| att.filename.downcase == filename }
420 421 image_url = url_for :only_path => only_path, :controller => 'attachments', :action => 'download', :id => found
421 422 desc = found.description.to_s.gsub('"', '')
422 423 if !desc.blank? && alttext.blank?
423 424 alt = " title=\"#{desc}\" alt=\"#{desc}\""
424 425 end
425 426 "src=\"#{image_url}\"#{alt}"
426 427 else
427 428 m
428 429 end
429 430 end
430 431 end
431 432
432 433
433 434 # different methods for formatting wiki links
434 435 case options[:wiki_links]
435 436 when :local
436 437 # used for local links to html files
437 438 format_wiki_link = Proc.new {|project, title, anchor| "#{title}.html" }
438 439 when :anchor
439 440 # used for single-file wiki export
440 441 format_wiki_link = Proc.new {|project, title, anchor| "##{title}" }
441 442 else
442 443 format_wiki_link = Proc.new {|project, title, anchor| url_for(:only_path => only_path, :controller => 'wiki', :action => 'index', :id => project, :page => title, :anchor => anchor) }
443 444 end
444 445
445 446 project = options[:project] || @project || (obj && obj.respond_to?(:project) ? obj.project : nil)
446 447
447 448 # Wiki links
448 449 #
449 450 # Examples:
450 451 # [[mypage]]
451 452 # [[mypage|mytext]]
452 453 # wiki links can refer other project wikis, using project name or identifier:
453 454 # [[project:]] -> wiki starting page
454 455 # [[project:|mytext]]
455 456 # [[project:mypage]]
456 457 # [[project:mypage|mytext]]
457 458 text = text.gsub(/(!)?(\[\[([^\]\n\|]+)(\|([^\]\n\|]+))?\]\])/) do |m|
458 459 link_project = project
459 460 esc, all, page, title = $1, $2, $3, $5
460 461 if esc.nil?
461 462 if page =~ /^([^\:]+)\:(.*)$/
462 463 link_project = Project.find_by_name($1) || Project.find_by_identifier($1)
463 464 page = $2
464 465 title ||= $1 if page.blank?
465 466 end
466 467
467 468 if link_project && link_project.wiki
468 469 # extract anchor
469 470 anchor = nil
470 471 if page =~ /^(.+?)\#(.+)$/
471 472 page, anchor = $1, $2
472 473 end
473 474 # check if page exists
474 475 wiki_page = link_project.wiki.find_page(page)
475 476 link_to((title || page), format_wiki_link.call(link_project, Wiki.titleize(page), anchor),
476 477 :class => ('wiki-page' + (wiki_page ? '' : ' new')))
477 478 else
478 479 # project or wiki doesn't exist
479 480 all
480 481 end
481 482 else
482 483 all
483 484 end
484 485 end
485 486
486 487 # Redmine links
487 488 #
488 489 # Examples:
489 490 # Issues:
490 491 # #52 -> Link to issue #52
491 492 # Changesets:
492 493 # r52 -> Link to revision 52
493 494 # commit:a85130f -> Link to scmid starting with a85130f
494 495 # Documents:
495 496 # document#17 -> Link to document with id 17
496 497 # document:Greetings -> Link to the document with title "Greetings"
497 498 # document:"Some document" -> Link to the document with title "Some document"
498 499 # Versions:
499 500 # version#3 -> Link to version with id 3
500 501 # version:1.0.0 -> Link to version named "1.0.0"
501 502 # version:"1.0 beta 2" -> Link to version named "1.0 beta 2"
502 503 # Attachments:
503 504 # attachment:file.zip -> Link to the attachment of the current object named file.zip
504 505 # Source files:
505 506 # source:some/file -> Link to the file located at /some/file in the project's repository
506 507 # source:some/file@52 -> Link to the file's revision 52
507 508 # source:some/file#L120 -> Link to line 120 of the file
508 509 # source:some/file@52#L120 -> Link to line 120 of the file's revision 52
509 510 # export:some/file -> Force the download of the file
510 511 # Forum messages:
511 512 # message#1218 -> Link to message with id 1218
512 513 text = text.gsub(%r{([\s\(,\-\>]|^)(!)?(attachment|document|version|commit|source|export|message)?((#|r)(\d+)|(:)([^"\s<>][^\s<>]*?|"[^"]+?"))(?=(?=[[:punct:]]\W)|,|\s|<|$)}) do |m|
513 514 leading, esc, prefix, sep, oid = $1, $2, $3, $5 || $7, $6 || $8
514 515 link = nil
515 516 if esc.nil?
516 517 if prefix.nil? && sep == 'r'
517 518 if project && (changeset = project.changesets.find_by_revision(oid))
518 519 link = link_to("r#{oid}", {:only_path => only_path, :controller => 'repositories', :action => 'revision', :id => project, :rev => oid},
519 520 :class => 'changeset',
520 521 :title => truncate_single_line(changeset.comments, :length => 100))
521 522 end
522 523 elsif sep == '#'
523 524 oid = oid.to_i
524 525 case prefix
525 526 when nil
526 527 if issue = Issue.visible.find_by_id(oid, :include => :status)
527 528 link = link_to("##{oid}", {:only_path => only_path, :controller => 'issues', :action => 'show', :id => oid},
528 529 :class => issue.css_classes,
529 530 :title => "#{truncate(issue.subject, :length => 100)} (#{issue.status.name})")
530 531 end
531 532 when 'document'
532 533 if document = Document.find_by_id(oid, :include => [:project], :conditions => Project.visible_by(User.current))
533 534 link = link_to h(document.title), {:only_path => only_path, :controller => 'documents', :action => 'show', :id => document},
534 535 :class => 'document'
535 536 end
536 537 when 'version'
537 538 if version = Version.find_by_id(oid, :include => [:project], :conditions => Project.visible_by(User.current))
538 539 link = link_to h(version.name), {:only_path => only_path, :controller => 'versions', :action => 'show', :id => version},
539 540 :class => 'version'
540 541 end
541 542 when 'message'
542 543 if message = Message.find_by_id(oid, :include => [:parent, {:board => :project}], :conditions => Project.visible_by(User.current))
543 544 link = link_to h(truncate(message.subject, :length => 60)), {:only_path => only_path,
544 545 :controller => 'messages',
545 546 :action => 'show',
546 547 :board_id => message.board,
547 548 :id => message.root,
548 549 :anchor => (message.parent ? "message-#{message.id}" : nil)},
549 550 :class => 'message'
550 551 end
551 552 end
552 553 elsif sep == ':'
553 554 # removes the double quotes if any
554 555 name = oid.gsub(%r{^"(.*)"$}, "\\1")
555 556 case prefix
556 557 when 'document'
557 558 if project && document = project.documents.find_by_title(name)
558 559 link = link_to h(document.title), {:only_path => only_path, :controller => 'documents', :action => 'show', :id => document},
559 560 :class => 'document'
560 561 end
561 562 when 'version'
562 563 if project && version = project.versions.find_by_name(name)
563 564 link = link_to h(version.name), {:only_path => only_path, :controller => 'versions', :action => 'show', :id => version},
564 565 :class => 'version'
565 566 end
566 567 when 'commit'
567 568 if project && (changeset = project.changesets.find(:first, :conditions => ["scmid LIKE ?", "#{name}%"]))
568 569 link = link_to h("#{name}"), {:only_path => only_path, :controller => 'repositories', :action => 'revision', :id => project, :rev => changeset.revision},
569 570 :class => 'changeset',
570 571 :title => truncate_single_line(changeset.comments, :length => 100)
571 572 end
572 573 when 'source', 'export'
573 574 if project && project.repository
574 575 name =~ %r{^[/\\]*(.*?)(@([0-9a-f]+))?(#(L\d+))?$}
575 576 path, rev, anchor = $1, $3, $5
576 577 link = link_to h("#{prefix}:#{name}"), {:controller => 'repositories', :action => 'entry', :id => project,
577 578 :path => to_path_param(path),
578 579 :rev => rev,
579 580 :anchor => anchor,
580 581 :format => (prefix == 'export' ? 'raw' : nil)},
581 582 :class => (prefix == 'export' ? 'source download' : 'source')
582 583 end
583 584 when 'attachment'
584 585 if attachments && attachment = attachments.detect {|a| a.filename == name }
585 586 link = link_to h(attachment.filename), {:only_path => only_path, :controller => 'attachments', :action => 'download', :id => attachment},
586 587 :class => 'attachment'
587 588 end
588 589 end
589 590 end
590 591 end
591 592 leading + (link || "#{prefix}#{sep}#{oid}")
592 593 end
593 594
594 595 text
595 596 end
596 597
597 598 # Same as Rails' simple_format helper without using paragraphs
598 599 def simple_format_without_paragraph(text)
599 600 text.to_s.
600 601 gsub(/\r\n?/, "\n"). # \r\n and \r -> \n
601 602 gsub(/\n\n+/, "<br /><br />"). # 2+ newline -> 2 br
602 603 gsub(/([^\n]\n)(?=[^\n])/, '\1<br />') # 1 newline -> br
603 604 end
604 605
605 606 def lang_options_for_select(blank=true)
606 607 (blank ? [["(auto)", ""]] : []) +
607 608 valid_languages.collect{|lang| [ ll(lang.to_s, :general_lang_name), lang.to_s]}.sort{|x,y| x.last <=> y.last }
608 609 end
609 610
610 611 def label_tag_for(name, option_tags = nil, options = {})
611 612 label_text = l(("field_"+field.to_s.gsub(/\_id$/, "")).to_sym) + (options.delete(:required) ? @template.content_tag("span", " *", :class => "required"): "")
612 613 content_tag("label", label_text)
613 614 end
614 615
615 616 def labelled_tabular_form_for(name, object, options, &proc)
616 617 options[:html] ||= {}
617 618 options[:html][:class] = 'tabular' unless options[:html].has_key?(:class)
618 619 form_for(name, object, options.merge({ :builder => TabularFormBuilder, :lang => current_language}), &proc)
619 620 end
620 621
621 622 def back_url_hidden_field_tag
622 623 back_url = params[:back_url] || request.env['HTTP_REFERER']
623 624 back_url = CGI.unescape(back_url.to_s)
624 625 hidden_field_tag('back_url', CGI.escape(back_url)) unless back_url.blank?
625 626 end
626 627
627 628 def check_all_links(form_name)
628 629 link_to_function(l(:button_check_all), "checkAll('#{form_name}', true)") +
629 630 " | " +
630 631 link_to_function(l(:button_uncheck_all), "checkAll('#{form_name}', false)")
631 632 end
632 633
633 634 def progress_bar(pcts, options={})
634 635 pcts = [pcts, pcts] unless pcts.is_a?(Array)
635 636 pcts = pcts.collect(&:round)
636 637 pcts[1] = pcts[1] - pcts[0]
637 638 pcts << (100 - pcts[1] - pcts[0])
638 639 width = options[:width] || '100px;'
639 640 legend = options[:legend] || ''
640 641 content_tag('table',
641 642 content_tag('tr',
642 643 (pcts[0] > 0 ? content_tag('td', '', :style => "width: #{pcts[0]}%;", :class => 'closed') : '') +
643 644 (pcts[1] > 0 ? content_tag('td', '', :style => "width: #{pcts[1]}%;", :class => 'done') : '') +
644 645 (pcts[2] > 0 ? content_tag('td', '', :style => "width: #{pcts[2]}%;", :class => 'todo') : '')
645 646 ), :class => 'progress', :style => "width: #{width};") +
646 647 content_tag('p', legend, :class => 'pourcent')
647 648 end
648 649
649 650 def context_menu_link(name, url, options={})
650 651 options[:class] ||= ''
651 652 if options.delete(:selected)
652 653 options[:class] << ' icon-checked disabled'
653 654 options[:disabled] = true
654 655 end
655 656 if options.delete(:disabled)
656 657 options.delete(:method)
657 658 options.delete(:confirm)
658 659 options.delete(:onclick)
659 660 options[:class] << ' disabled'
660 661 url = '#'
661 662 end
662 663 link_to name, url, options
663 664 end
664 665
665 666 def calendar_for(field_id)
666 667 include_calendar_headers_tags
667 668 image_tag("calendar.png", {:id => "#{field_id}_trigger",:class => "calendar-trigger"}) +
668 669 javascript_tag("Calendar.setup({inputField : '#{field_id}', ifFormat : '%Y-%m-%d', button : '#{field_id}_trigger' });")
669 670 end
670 671
671 672 def include_calendar_headers_tags
672 673 unless @calendar_headers_tags_included
673 674 @calendar_headers_tags_included = true
674 675 content_for :header_tags do
675 676 start_of_week = case Setting.start_of_week.to_i
676 677 when 1
677 678 'Calendar._FD = 1;' # Monday
678 679 when 7
679 680 'Calendar._FD = 0;' # Sunday
680 681 else
681 682 '' # use language
682 683 end
683 684
684 685 javascript_include_tag('calendar/calendar') +
685 686 javascript_include_tag("calendar/lang/calendar-#{current_language.to_s.downcase}.js") +
686 687 javascript_tag(start_of_week) +
687 688 javascript_include_tag('calendar/calendar-setup') +
688 689 stylesheet_link_tag('calendar')
689 690 end
690 691 end
691 692 end
692 693
693 694 def content_for(name, content = nil, &block)
694 695 @has_content ||= {}
695 696 @has_content[name] = true
696 697 super(name, content, &block)
697 698 end
698 699
699 700 def has_content?(name)
700 701 (@has_content && @has_content[name]) || false
701 702 end
702 703
703 704 # Returns the avatar image tag for the given +user+ if avatars are enabled
704 705 # +user+ can be a User or a string that will be scanned for an email address (eg. 'joe <joe@foo.bar>')
705 706 def avatar(user, options = { })
706 707 if Setting.gravatar_enabled?
707 708 options.merge!({:ssl => Setting.protocol == 'https', :default => Setting.gravatar_default})
708 709 email = nil
709 710 if user.respond_to?(:mail)
710 711 email = user.mail
711 712 elsif user.to_s =~ %r{<(.+?)>}
712 713 email = $1
713 714 end
714 715 return gravatar(email.to_s.downcase, options) unless email.blank? rescue nil
715 716 end
716 717 end
717 718
718 719 private
719 720
720 721 def wiki_helper
721 722 helper = Redmine::WikiFormatting.helper_for(Setting.text_formatting)
722 723 extend helper
723 724 return self
724 725 end
725 726
726 727 def link_to_remote_content_update(text, url_params)
727 728 link_to_remote(text,
728 729 {:url => url_params, :method => :get, :update => 'content', :complete => 'window.scrollTo(0,0)'},
729 730 {:href => url_for(:params => url_params)}
730 731 )
731 732 end
732 733
733 734 end
@@ -1,28 +1,29
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 module MessagesHelper
19 19
20 20 def link_to_message(message)
21 21 return '' unless message
22 22 link_to h(truncate(message.subject, :length => 60)), :controller => 'messages',
23 23 :action => 'show',
24 24 :board_id => message.board_id,
25 25 :id => message.root,
26 :r => (message.parent_id && message.id),
26 27 :anchor => (message.parent_id ? "message-#{message.id}" : nil)
27 28 end
28 29 end
@@ -1,98 +1,98
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 Message < ActiveRecord::Base
19 19 belongs_to :board
20 20 belongs_to :author, :class_name => 'User', :foreign_key => 'author_id'
21 21 acts_as_tree :counter_cache => :replies_count, :order => "#{Message.table_name}.created_on ASC"
22 22 acts_as_attachable
23 23 belongs_to :last_reply, :class_name => 'Message', :foreign_key => 'last_reply_id'
24 24
25 25 acts_as_searchable :columns => ['subject', 'content'],
26 26 :include => {:board => :project},
27 27 :project_key => 'project_id',
28 28 :date_column => "#{table_name}.created_on"
29 29 acts_as_event :title => Proc.new {|o| "#{o.board.name}: #{o.subject}"},
30 30 :description => :content,
31 31 :type => Proc.new {|o| o.parent_id.nil? ? 'message' : 'reply'},
32 32 :url => Proc.new {|o| {:controller => 'messages', :action => 'show', :board_id => o.board_id}.merge(o.parent_id.nil? ? {:id => o.id} :
33 {:id => o.parent_id, :anchor => "message-#{o.id}"})}
33 {:id => o.parent_id, :r => o.id, :anchor => "message-#{o.id}"})}
34 34
35 35 acts_as_activity_provider :find_options => {:include => [{:board => :project}, :author]},
36 36 :author_key => :author_id
37 37 acts_as_watchable
38 38
39 39 attr_protected :locked, :sticky
40 40 validates_presence_of :board, :subject, :content
41 41 validates_length_of :subject, :maximum => 255
42 42
43 43 after_create :add_author_as_watcher
44 44
45 45 def visible?(user=User.current)
46 46 !user.nil? && user.allowed_to?(:view_messages, project)
47 47 end
48 48
49 49 def validate_on_create
50 50 # Can not reply to a locked topic
51 51 errors.add_to_base 'Topic is locked' if root.locked? && self != root
52 52 end
53 53
54 54 def after_create
55 55 if parent
56 56 parent.reload.update_attribute(:last_reply_id, self.id)
57 57 end
58 58 board.reset_counters!
59 59 end
60 60
61 61 def after_update
62 62 if board_id_changed?
63 63 Message.update_all("board_id = #{board_id}", ["id = ? OR parent_id = ?", root.id, root.id])
64 64 Board.reset_counters!(board_id_was)
65 65 Board.reset_counters!(board_id)
66 66 end
67 67 end
68 68
69 69 def after_destroy
70 70 board.reset_counters!
71 71 end
72 72
73 73 def sticky=(arg)
74 74 write_attribute :sticky, (arg == true || arg.to_s == '1' ? 1 : 0)
75 75 end
76 76
77 77 def sticky?
78 78 sticky == 1
79 79 end
80 80
81 81 def project
82 82 board.project
83 83 end
84 84
85 85 def editable_by?(usr)
86 86 usr && usr.logged? && (usr.allowed_to?(:edit_messages, project) || (self.author == usr && usr.allowed_to?(:edit_own_messages, project)))
87 87 end
88 88
89 89 def destroyable_by?(usr)
90 90 usr && usr.logged? && (usr.allowed_to?(:delete_messages, project) || (self.author == usr && usr.allowed_to?(:delete_own_messages, project)))
91 91 end
92 92
93 93 private
94 94
95 95 def add_author_as_watcher
96 96 Watcher.create(:watchable => self.root, :user => author)
97 97 end
98 98 end
@@ -1,65 +1,66
1 1 <%= breadcrumb link_to(l(:label_board_plural), {:controller => 'boards', :action => 'index', :project_id => @project}),
2 2 link_to(h(@board.name), {:controller => 'boards', :action => 'show', :project_id => @project, :id => @board}) %>
3 3
4 4 <div class="contextual">
5 5 <%= watcher_tag(@topic, User.current) %>
6 6 <%= link_to_remote_if_authorized l(:button_quote), { :url => {:action => 'quote', :id => @topic} }, :class => 'icon icon-comment' %>
7 7 <%= link_to(l(:button_edit), {:action => 'edit', :id => @topic}, :class => 'icon icon-edit') if @message.editable_by?(User.current) %>
8 8 <%= link_to(l(:button_delete), {:action => 'destroy', :id => @topic}, :method => :post, :confirm => l(:text_are_you_sure), :class => 'icon icon-del') if @message.destroyable_by?(User.current) %>
9 9 </div>
10 10
11 11 <h2><%= avatar(@topic.author, :size => "24") %><%=h @topic.subject %></h2>
12 12
13 13 <div class="message">
14 14 <p><span class="author"><%= authoring @topic.created_on, @topic.author %></span></p>
15 15 <div class="wiki">
16 16 <%= textilizable(@topic.content, :attachments => @topic.attachments) %>
17 17 </div>
18 18 <%= link_to_attachments @topic, :author => false %>
19 19 </div>
20 20 <br />
21 21
22 22 <% unless @replies.empty? %>
23 <h3 class="comments"><%= l(:label_reply_plural) %></h3>
23 <h3 class="comments"><%= l(:label_reply_plural) %> (<%= @reply_count %>)</h3>
24 24 <% @replies.each do |message| %>
25 25 <div class="message reply" id="<%= "message-#{message.id}" %>">
26 26 <div class="contextual">
27 27 <%= link_to_remote_if_authorized image_tag('comment.png'), { :url => {:action => 'quote', :id => message} }, :title => l(:button_quote) %>
28 28 <%= link_to(image_tag('edit.png'), {:action => 'edit', :id => message}, :title => l(:button_edit)) if message.editable_by?(User.current) %>
29 29 <%= link_to(image_tag('delete.png'), {:action => 'destroy', :id => message}, :method => :post, :confirm => l(:text_are_you_sure), :title => l(:button_delete)) if message.destroyable_by?(User.current) %>
30 30 </div>
31 31 <h4>
32 32 <%= avatar(message.author, :size => "24") %>
33 33 <%= link_to h(message.subject), { :controller => 'messages', :action => 'show', :board_id => @board, :id => @topic, :anchor => "message-#{message.id}" } %>
34 34 -
35 35 <%= authoring message.created_on, message.author %>
36 36 </h4>
37 37 <div class="wiki"><%= textilizable message, :content, :attachments => message.attachments %></div>
38 38 <%= link_to_attachments message, :author => false %>
39 39 </div>
40 40 <% end %>
41 <p class="pagination"><%= pagination_links_full @reply_pages, @reply_count, :per_page_links => false %></p>
41 42 <% end %>
42 43
43 44 <% if !@topic.locked? && authorize_for('messages', 'reply') %>
44 45 <p><%= toggle_link l(:button_reply), "reply", :focus => 'message_content' %></p>
45 46 <div id="reply" style="display:none;">
46 47 <% form_for :reply, @reply, :url => {:action => 'reply', :id => @topic}, :html => {:multipart => true, :id => 'message-form'} do |f| %>
47 48 <%= render :partial => 'form', :locals => {:f => f, :replying => true} %>
48 49 <%= submit_tag l(:button_submit) %>
49 50 <%= link_to_remote l(:label_preview),
50 51 { :url => { :controller => 'messages', :action => 'preview', :board_id => @board },
51 52 :method => 'post',
52 53 :update => 'preview',
53 54 :with => "Form.serialize('message-form')",
54 55 :complete => "Element.scrollTo('preview')"
55 56 }, :accesskey => accesskey(:preview) %>
56 57 <% end %>
57 58 <div id="preview" class="wiki"></div>
58 59 </div>
59 60 <% end %>
60 61
61 62 <% content_for :header_tags do %>
62 63 <%= stylesheet_link_tag 'scm' %>
63 64 <% end %>
64 65
65 66 <% html_title h(@topic.subject) %>
@@ -1,170 +1,187
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 require File.dirname(__FILE__) + '/../test_helper'
19 19 require 'messages_controller'
20 20
21 21 # Re-raise errors caught by the controller.
22 22 class MessagesController; def rescue_action(e) raise e end; end
23 23
24 24 class MessagesControllerTest < ActionController::TestCase
25 25 fixtures :projects, :users, :members, :member_roles, :roles, :boards, :messages, :enabled_modules
26 26
27 27 def setup
28 28 @controller = MessagesController.new
29 29 @request = ActionController::TestRequest.new
30 30 @response = ActionController::TestResponse.new
31 31 User.current = nil
32 32 end
33 33
34 34 def test_show_routing
35 35 assert_routing(
36 36 {:method => :get, :path => '/boards/22/topics/2'},
37 37 :controller => 'messages', :action => 'show', :id => '2', :board_id => '22'
38 38 )
39 39 end
40 40
41 41 def test_show
42 42 get :show, :board_id => 1, :id => 1
43 43 assert_response :success
44 44 assert_template 'show'
45 45 assert_not_nil assigns(:board)
46 46 assert_not_nil assigns(:project)
47 47 assert_not_nil assigns(:topic)
48 48 end
49 49
50 def test_show_with_pagination
51 message = Message.find(1)
52 assert_difference 'Message.count', 30 do
53 30.times do
54 message.children << Message.new(:subject => 'Reply', :content => 'Reply body', :author_id => 2, :board_id => 1)
55 end
56 end
57 get :show, :board_id => 1, :id => 1, :r => message.children.last(:order => 'id').id
58 assert_response :success
59 assert_template 'show'
60 replies = assigns(:replies)
61 assert_not_nil replies
62 assert !replies.include?(message.children.first(:order => 'id'))
63 assert replies.include?(message.children.last(:order => 'id'))
64 end
65
50 66 def test_show_with_reply_permission
51 67 @request.session[:user_id] = 2
52 68 get :show, :board_id => 1, :id => 1
53 69 assert_response :success
54 70 assert_template 'show'
55 71 assert_tag :div, :attributes => { :id => 'reply' },
56 72 :descendant => { :tag => 'textarea', :attributes => { :id => 'message_content' } }
57 73 end
58 74
59 75 def test_show_message_not_found
60 76 get :show, :board_id => 1, :id => 99999
61 77 assert_response 404
62 78 end
63 79
64 80 def test_new_routing
65 81 assert_routing(
66 82 {:method => :get, :path => '/boards/lala/topics/new'},
67 83 :controller => 'messages', :action => 'new', :board_id => 'lala'
68 84 )
69 85 assert_recognizes(#TODO: POST to collection, need to adjust form accordingly
70 86 {:controller => 'messages', :action => 'new', :board_id => 'lala'},
71 87 {:method => :post, :path => '/boards/lala/topics/new'}
72 88 )
73 89 end
74 90
75 91 def test_get_new
76 92 @request.session[:user_id] = 2
77 93 get :new, :board_id => 1
78 94 assert_response :success
79 95 assert_template 'new'
80 96 end
81 97
82 98 def test_post_new
83 99 @request.session[:user_id] = 2
84 100 ActionMailer::Base.deliveries.clear
85 101 Setting.notified_events = ['message_posted']
86 102
87 103 post :new, :board_id => 1,
88 104 :message => { :subject => 'Test created message',
89 105 :content => 'Message body'}
90 106 message = Message.find_by_subject('Test created message')
91 107 assert_not_nil message
92 108 assert_redirected_to "boards/1/topics/#{message.to_param}"
93 109 assert_equal 'Message body', message.content
94 110 assert_equal 2, message.author_id
95 111 assert_equal 1, message.board_id
96 112
97 113 mail = ActionMailer::Base.deliveries.last
98 114 assert_kind_of TMail::Mail, mail
99 115 assert_equal "[#{message.board.project.name} - #{message.board.name} - msg#{message.root.id}] Test created message", mail.subject
100 116 assert mail.body.include?('Message body')
101 117 # author
102 118 assert mail.bcc.include?('jsmith@somenet.foo')
103 119 # project member
104 120 assert mail.bcc.include?('dlopper@somenet.foo')
105 121 end
106 122
107 123 def test_edit_routing
108 124 assert_routing(
109 125 {:method => :get, :path => '/boards/lala/topics/22/edit'},
110 126 :controller => 'messages', :action => 'edit', :board_id => 'lala', :id => '22'
111 127 )
112 128 assert_recognizes( #TODO: use PUT to topic_path, modify form accordingly
113 129 {:controller => 'messages', :action => 'edit', :board_id => 'lala', :id => '22'},
114 130 {:method => :post, :path => '/boards/lala/topics/22/edit'}
115 131 )
116 132 end
117 133
118 134 def test_get_edit
119 135 @request.session[:user_id] = 2
120 136 get :edit, :board_id => 1, :id => 1
121 137 assert_response :success
122 138 assert_template 'edit'
123 139 end
124 140
125 141 def test_post_edit
126 142 @request.session[:user_id] = 2
127 143 post :edit, :board_id => 1, :id => 1,
128 144 :message => { :subject => 'New subject',
129 145 :content => 'New body'}
130 146 assert_redirected_to 'boards/1/topics/1'
131 147 message = Message.find(1)
132 148 assert_equal 'New subject', message.subject
133 149 assert_equal 'New body', message.content
134 150 end
135 151
136 152 def test_reply_routing
137 153 assert_recognizes(
138 154 {:controller => 'messages', :action => 'reply', :board_id => '22', :id => '555'},
139 155 {:method => :post, :path => '/boards/22/topics/555/replies'}
140 156 )
141 157 end
142 158
143 159 def test_reply
144 160 @request.session[:user_id] = 2
145 161 post :reply, :board_id => 1, :id => 1, :reply => { :content => 'This is a test reply', :subject => 'Test reply' }
146 assert_redirected_to 'boards/1/topics/1'
162 reply = Message.find(:first, :order => 'id DESC')
163 assert_redirected_to "boards/1/topics/1?r=#{reply.id}"
147 164 assert Message.find_by_subject('Test reply')
148 165 end
149 166
150 167 def test_destroy_routing
151 168 assert_recognizes(#TODO: use DELETE to topic_path, adjust form accordingly
152 169 {:controller => 'messages', :action => 'destroy', :board_id => '22', :id => '555'},
153 170 {:method => :post, :path => '/boards/22/topics/555/destroy'}
154 171 )
155 172 end
156 173
157 174 def test_destroy_topic
158 175 @request.session[:user_id] = 2
159 176 post :destroy, :board_id => 1, :id => 1
160 177 assert_redirected_to 'projects/ecookbook/boards/1'
161 178 assert_nil Message.find_by_id(1)
162 179 end
163 180
164 181 def test_quote
165 182 @request.session[:user_id] = 2
166 183 xhr :get, :quote, :board_id => 1, :id => 3
167 184 assert_response :success
168 185 assert_select_rjs :show, 'reply'
169 186 end
170 187 end
General Comments 0
You need to be logged in to leave comments. Login now