##// END OF EJS Templates
Adds pagination to forum messages (#4664)....
Jean-Philippe Lang -
r3259:8fb29d4d211a
parent child
Show More
@@ -29,10 +29,24 class MessagesController < ApplicationController
29 helper :attachments
29 helper :attachments
30 include AttachmentsHelper
30 include AttachmentsHelper
31
31
32 REPLIES_PER_PAGE = 25 unless const_defined?(:REPLIES_PER_PAGE)
33
32 # Show a topic and its replies
34 # Show a topic and its replies
33 def show
35 def show
34 @replies = @topic.children.find(:all, :include => [:author, :attachments, {:board => :project}])
36 page = params[:page]
35 @replies.reverse! if User.current.wants_comments_in_reverse_order?
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 @reply = Message.new(:subject => "RE: #{@message.subject}")
50 @reply = Message.new(:subject => "RE: #{@message.subject}")
37 render :action => "show", :layout => false if request.xhr?
51 render :action => "show", :layout => false if request.xhr?
38 end
52 end
@@ -63,7 +77,7 class MessagesController < ApplicationController
63 call_hook(:controller_messages_reply_after_save, { :params => params, :message => @reply})
77 call_hook(:controller_messages_reply_after_save, { :params => params, :message => @reply})
64 attach_files(@reply, params[:attachments])
78 attach_files(@reply, params[:attachments])
65 end
79 end
66 redirect_to :action => 'show', :id => @topic
80 redirect_to :action => 'show', :id => @topic, :r => @reply
67 end
81 end
68
82
69 # Edit a message
83 # Edit a message
@@ -77,7 +91,7 class MessagesController < ApplicationController
77 attach_files(@message, params[:attachments])
91 attach_files(@message, params[:attachments])
78 flash[:notice] = l(:notice_successful_update)
92 flash[:notice] = l(:notice_successful_update)
79 @message.reload
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 end
95 end
82 end
96 end
83
97
@@ -87,7 +101,7 class MessagesController < ApplicationController
87 @message.destroy
101 @message.destroy
88 redirect_to @message.parent.nil? ?
102 redirect_to @message.parent.nil? ?
89 { :controller => 'boards', :action => 'show', :project_id => @project, :id => @board } :
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 end
105 end
92
106
93 def quote
107 def quote
@@ -289,6 +289,7 module ApplicationHelper
289
289
290 def pagination_links_full(paginator, count=nil, options={})
290 def pagination_links_full(paginator, count=nil, options={})
291 page_param = options.delete(:page_param) || :page
291 page_param = options.delete(:page_param) || :page
292 per_page_links = options.delete(:per_page_links)
292 url_param = params.dup
293 url_param = params.dup
293 # don't reuse query params if filters are present
294 # don't reuse query params if filters are present
294 url_param.merge!(:fields => nil, :values => nil, :operators => nil) if url_param.delete(:set_filter)
295 url_param.merge!(:fields => nil, :values => nil, :operators => nil) if url_param.delete(:set_filter)
@@ -307,10 +308,10 module ApplicationHelper
307 end
308 end
308
309
309 unless count.nil?
310 unless count.nil?
310 html << [
311 html << " (#{paginator.current.first_item}-#{paginator.current.last_item}/#{count})"
311 " (#{paginator.current.first_item}-#{paginator.current.last_item}/#{count})",
312 if per_page_links != false && links = per_page_links(paginator.items_per_page)
312 per_page_links(paginator.items_per_page)
313 html << " | #{links}"
313 ].compact.join(' | ')
314 end
314 end
315 end
315
316
316 html
317 html
@@ -23,6 +23,7 module MessagesHelper
23 :action => 'show',
23 :action => 'show',
24 :board_id => message.board_id,
24 :board_id => message.board_id,
25 :id => message.root,
25 :id => message.root,
26 :r => (message.parent_id && message.id),
26 :anchor => (message.parent_id ? "message-#{message.id}" : nil)
27 :anchor => (message.parent_id ? "message-#{message.id}" : nil)
27 end
28 end
28 end
29 end
@@ -30,7 +30,7 class Message < ActiveRecord::Base
30 :description => :content,
30 :description => :content,
31 :type => Proc.new {|o| o.parent_id.nil? ? 'message' : 'reply'},
31 :type => Proc.new {|o| o.parent_id.nil? ? 'message' : 'reply'},
32 :url => Proc.new {|o| {:controller => 'messages', :action => 'show', :board_id => o.board_id}.merge(o.parent_id.nil? ? {:id => o.id} :
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 acts_as_activity_provider :find_options => {:include => [{:board => :project}, :author]},
35 acts_as_activity_provider :find_options => {:include => [{:board => :project}, :author]},
36 :author_key => :author_id
36 :author_key => :author_id
@@ -20,7 +20,7
20 <br />
20 <br />
21
21
22 <% unless @replies.empty? %>
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 <% @replies.each do |message| %>
24 <% @replies.each do |message| %>
25 <div class="message reply" id="<%= "message-#{message.id}" %>">
25 <div class="message reply" id="<%= "message-#{message.id}" %>">
26 <div class="contextual">
26 <div class="contextual">
@@ -38,6 +38,7
38 <%= link_to_attachments message, :author => false %>
38 <%= link_to_attachments message, :author => false %>
39 </div>
39 </div>
40 <% end %>
40 <% end %>
41 <p class="pagination"><%= pagination_links_full @reply_pages, @reply_count, :per_page_links => false %></p>
41 <% end %>
42 <% end %>
42
43
43 <% if !@topic.locked? && authorize_for('messages', 'reply') %>
44 <% if !@topic.locked? && authorize_for('messages', 'reply') %>
@@ -47,6 +47,22 class MessagesControllerTest < ActionController::TestCase
47 assert_not_nil assigns(:topic)
47 assert_not_nil assigns(:topic)
48 end
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 def test_show_with_reply_permission
66 def test_show_with_reply_permission
51 @request.session[:user_id] = 2
67 @request.session[:user_id] = 2
52 get :show, :board_id => 1, :id => 1
68 get :show, :board_id => 1, :id => 1
@@ -143,7 +159,8 class MessagesControllerTest < ActionController::TestCase
143 def test_reply
159 def test_reply
144 @request.session[:user_id] = 2
160 @request.session[:user_id] = 2
145 post :reply, :board_id => 1, :id => 1, :reply => { :content => 'This is a test reply', :subject => 'Test reply' }
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 assert Message.find_by_subject('Test reply')
164 assert Message.find_by_subject('Test reply')
148 end
165 end
149
166
General Comments 0
You need to be logged in to leave comments. Login now