##// END OF EJS Templates
Removes RJS from MessagesController....
Jean-Philippe Lang -
r9870:e8469e2c5b41
parent child
Show More
@@ -0,0 +1,6
1 $('message_subject').value = "<%= raw escape_javascript(@subject) %>";
2 $('message_content').value = "<%= raw escape_javascript(@content) %>";
3 Element.show('reply');
4 Form.Element.focus('message_content');
5 Element.scrollTo('reply');
6 $('message_content').scrollTop = $('message_content').scrollHeight - $('message_content').clientHeight;
@@ -1,146 +1,137
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2012 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 helper :watchers
26 26 helper :attachments
27 27 include AttachmentsHelper
28 28
29 29 REPLIES_PER_PAGE = 25 unless const_defined?(:REPLIES_PER_PAGE)
30 30
31 31 # Show a topic and its replies
32 32 def show
33 33 page = params[:page]
34 34 # Find the page of the requested reply
35 35 if params[:r] && page.nil?
36 36 offset = @topic.children.count(:conditions => ["#{Message.table_name}.id < ?", params[:r].to_i])
37 37 page = 1 + offset / REPLIES_PER_PAGE
38 38 end
39 39
40 40 @reply_count = @topic.children.count
41 41 @reply_pages = Paginator.new self, @reply_count, REPLIES_PER_PAGE, page
42 42 @replies = @topic.children.find(:all, :include => [:author, :attachments, {:board => :project}],
43 43 :order => "#{Message.table_name}.created_on ASC",
44 44 :limit => @reply_pages.items_per_page,
45 45 :offset => @reply_pages.current.offset)
46 46
47 47 @reply = Message.new(:subject => "RE: #{@message.subject}")
48 48 render :action => "show", :layout => false if request.xhr?
49 49 end
50 50
51 51 # Create a new topic
52 52 def new
53 53 @message = Message.new
54 54 @message.author = User.current
55 55 @message.board = @board
56 56 @message.safe_attributes = params[:message]
57 57 if request.post?
58 58 @message.save_attachments(params[:attachments])
59 59 if @message.save
60 60 call_hook(:controller_messages_new_after_save, { :params => params, :message => @message})
61 61 render_attachment_warning_if_needed(@message)
62 62 redirect_to board_message_path(@board, @message)
63 63 end
64 64 end
65 65 end
66 66
67 67 # Reply to a topic
68 68 def reply
69 69 @reply = Message.new
70 70 @reply.author = User.current
71 71 @reply.board = @board
72 72 @reply.safe_attributes = params[:reply]
73 73 @topic.children << @reply
74 74 if !@reply.new_record?
75 75 call_hook(:controller_messages_reply_after_save, { :params => params, :message => @reply})
76 76 attachments = Attachment.attach_files(@reply, params[:attachments])
77 77 render_attachment_warning_if_needed(@reply)
78 78 end
79 79 redirect_to board_message_path(@board, @topic, :r => @reply)
80 80 end
81 81
82 82 # Edit a message
83 83 def edit
84 84 (render_403; return false) unless @message.editable_by?(User.current)
85 85 @message.safe_attributes = params[:message]
86 86 if request.post? && @message.save
87 87 attachments = Attachment.attach_files(@message, params[:attachments])
88 88 render_attachment_warning_if_needed(@message)
89 89 flash[:notice] = l(:notice_successful_update)
90 90 @message.reload
91 91 redirect_to board_message_path(@message.board, @message.root, :r => (@message.parent_id && @message.id))
92 92 end
93 93 end
94 94
95 95 # Delete a messages
96 96 def destroy
97 97 (render_403; return false) unless @message.destroyable_by?(User.current)
98 98 r = @message.to_param
99 99 @message.destroy
100 100 if @message.parent
101 101 redirect_to board_message_path(@board, @message.parent, :r => r)
102 102 else
103 103 redirect_to project_board_path(@project, @board)
104 104 end
105 105 end
106 106
107 107 def quote
108 user = @message.author
109 text = @message.content
110 subject = @message.subject.gsub('"', '\"')
111 subject = "RE: #{subject}" unless subject.starts_with?('RE:')
112 content = "#{ll(Setting.default_language, :text_user_wrote, user)}\\n> "
113 content << text.to_s.strip.gsub(%r{<pre>((.|\s)*?)</pre>}m, '[...]').gsub('"', '\"').gsub(/(\r?\n|\r\n?)/, "\\n> ") + "\\n\\n"
114 render(:update) { |page|
115 page << "$('message_subject').value = \"#{subject}\";"
116 page.<< "$('message_content').value = \"#{content}\";"
117 page.show 'reply'
118 page << "Form.Element.focus('message_content');"
119 page << "Element.scrollTo('reply');"
120 page << "$('message_content').scrollTop = $('message_content').scrollHeight - $('message_content').clientHeight;"
121 }
108 @subject = @message.subject
109 @subject = "RE: #{@subject}" unless @subject.starts_with?('RE:')
110
111 @content = "#{ll(Setting.default_language, :text_user_wrote, @message.author)}\n> "
112 @content << @message.content.to_s.strip.gsub(%r{<pre>((.|\s)*?)</pre>}m, '[...]').gsub(/(\r?\n|\r\n?)/, "\n> ") + "\n\n"
122 113 end
123 114
124 115 def preview
125 116 message = @board.messages.find_by_id(params[:id])
126 117 @attachements = message.attachments if message
127 118 @text = (params[:message] || params[:reply])[:content]
128 119 render :partial => 'common/preview'
129 120 end
130 121
131 122 private
132 123 def find_message
133 124 find_board
134 125 @message = @board.messages.find(params[:id], :include => :parent)
135 126 @topic = @message.root
136 127 rescue ActiveRecord::RecordNotFound
137 128 render_404
138 129 end
139 130
140 131 def find_board
141 132 @board = Board.find(params[:board_id], :include => :project)
142 133 @project = @board.project
143 134 rescue ActiveRecord::RecordNotFound
144 135 render_404
145 136 end
146 137 end
@@ -1,210 +1,213
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2012 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.expand_path('../../test_helper', __FILE__)
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
35 35 get :show, :board_id => 1, :id => 1
36 36 assert_response :success
37 37 assert_template 'show'
38 38 assert_not_nil assigns(:board)
39 39 assert_not_nil assigns(:project)
40 40 assert_not_nil assigns(:topic)
41 41 end
42 42
43 43 def test_show_should_contain_reply_field_tags_for_quoting
44 44 @request.session[:user_id] = 2
45 45 get :show, :board_id => 1, :id => 1
46 46 assert_response :success
47 47
48 48 # tags required by MessagesController#quote
49 49 assert_tag 'input', :attributes => {:id => 'message_subject'}
50 50 assert_tag 'textarea', :attributes => {:id => 'message_content'}
51 51 assert_tag 'div', :attributes => {:id => 'reply'}
52 52 end
53 53
54 54 def test_show_with_pagination
55 55 message = Message.find(1)
56 56 assert_difference 'Message.count', 30 do
57 57 30.times do
58 58 message.children << Message.new(:subject => 'Reply', :content => 'Reply body', :author_id => 2, :board_id => 1)
59 59 end
60 60 end
61 61 get :show, :board_id => 1, :id => 1, :r => message.children.last(:order => 'id').id
62 62 assert_response :success
63 63 assert_template 'show'
64 64 replies = assigns(:replies)
65 65 assert_not_nil replies
66 66 assert !replies.include?(message.children.first(:order => 'id'))
67 67 assert replies.include?(message.children.last(:order => 'id'))
68 68 end
69 69
70 70 def test_show_with_reply_permission
71 71 @request.session[:user_id] = 2
72 72 get :show, :board_id => 1, :id => 1
73 73 assert_response :success
74 74 assert_template 'show'
75 75 assert_tag :div, :attributes => { :id => 'reply' },
76 76 :descendant => { :tag => 'textarea', :attributes => { :id => 'message_content' } }
77 77 end
78 78
79 79 def test_show_message_not_found
80 80 get :show, :board_id => 1, :id => 99999
81 81 assert_response 404
82 82 end
83 83
84 84 def test_get_new
85 85 @request.session[:user_id] = 2
86 86 get :new, :board_id => 1
87 87 assert_response :success
88 88 assert_template 'new'
89 89 end
90 90
91 91 def test_post_new
92 92 @request.session[:user_id] = 2
93 93 ActionMailer::Base.deliveries.clear
94 94
95 95 with_settings :notified_events => %w(message_posted) do
96 96 post :new, :board_id => 1,
97 97 :message => { :subject => 'Test created message',
98 98 :content => 'Message body'}
99 99 end
100 100 message = Message.find_by_subject('Test created message')
101 101 assert_not_nil message
102 102 assert_redirected_to "/boards/1/topics/#{message.to_param}"
103 103 assert_equal 'Message body', message.content
104 104 assert_equal 2, message.author_id
105 105 assert_equal 1, message.board_id
106 106
107 107 mail = ActionMailer::Base.deliveries.last
108 108 assert_not_nil mail
109 109 assert_equal "[#{message.board.project.name} - #{message.board.name} - msg#{message.root.id}] Test created message", mail.subject
110 110 assert_mail_body_match 'Message body', mail
111 111 # author
112 112 assert mail.bcc.include?('jsmith@somenet.foo')
113 113 # project member
114 114 assert mail.bcc.include?('dlopper@somenet.foo')
115 115 end
116 116
117 117 def test_get_edit
118 118 @request.session[:user_id] = 2
119 119 get :edit, :board_id => 1, :id => 1
120 120 assert_response :success
121 121 assert_template 'edit'
122 122 end
123 123
124 124 def test_post_edit
125 125 @request.session[:user_id] = 2
126 126 post :edit, :board_id => 1, :id => 1,
127 127 :message => { :subject => 'New subject',
128 128 :content => 'New body'}
129 129 assert_redirected_to '/boards/1/topics/1'
130 130 message = Message.find(1)
131 131 assert_equal 'New subject', message.subject
132 132 assert_equal 'New body', message.content
133 133 end
134 134
135 135 def test_post_edit_sticky_and_locked
136 136 @request.session[:user_id] = 2
137 137 post :edit, :board_id => 1, :id => 1,
138 138 :message => { :subject => 'New subject',
139 139 :content => 'New body',
140 140 :locked => '1',
141 141 :sticky => '1'}
142 142 assert_redirected_to '/boards/1/topics/1'
143 143 message = Message.find(1)
144 144 assert_equal true, message.sticky?
145 145 assert_equal true, message.locked?
146 146 end
147 147
148 148 def test_post_edit_should_allow_to_change_board
149 149 @request.session[:user_id] = 2
150 150 post :edit, :board_id => 1, :id => 1,
151 151 :message => { :subject => 'New subject',
152 152 :content => 'New body',
153 153 :board_id => 2}
154 154 assert_redirected_to '/boards/2/topics/1'
155 155 message = Message.find(1)
156 156 assert_equal Board.find(2), message.board
157 157 end
158 158
159 159 def test_reply
160 160 @request.session[:user_id] = 2
161 161 post :reply, :board_id => 1, :id => 1, :reply => { :content => 'This is a test reply', :subject => 'Test reply' }
162 162 reply = Message.find(:first, :order => 'id DESC')
163 163 assert_redirected_to "/boards/1/topics/1?r=#{reply.id}"
164 164 assert Message.find_by_subject('Test reply')
165 165 end
166 166
167 167 def test_destroy_topic
168 168 @request.session[:user_id] = 2
169 169 assert_difference 'Message.count', -3 do
170 170 post :destroy, :board_id => 1, :id => 1
171 171 end
172 172 assert_redirected_to '/projects/ecookbook/boards/1'
173 173 assert_nil Message.find_by_id(1)
174 174 end
175 175
176 176 def test_destroy_reply
177 177 @request.session[:user_id] = 2
178 178 assert_difference 'Message.count', -1 do
179 179 post :destroy, :board_id => 1, :id => 2
180 180 end
181 181 assert_redirected_to '/boards/1/topics/1?r=2'
182 182 assert_nil Message.find_by_id(2)
183 183 end
184 184
185 185 def test_quote
186 186 @request.session[:user_id] = 2
187 187 xhr :get, :quote, :board_id => 1, :id => 3
188 188 assert_response :success
189 assert_select_rjs :show, 'reply'
189 assert_equal 'text/javascript', response.content_type
190 assert_template 'quote'
191 assert_include 'RE: First post', response.body
192 assert_include '> An other reply', response.body
190 193 end
191 194
192 195 def test_preview_new
193 196 @request.session[:user_id] = 2
194 197 post :preview,
195 198 :board_id => 1,
196 199 :message => {:subject => "", :content => "Previewed text"}
197 200 assert_response :success
198 201 assert_template 'common/_preview'
199 202 end
200 203
201 204 def test_preview_edit
202 205 @request.session[:user_id] = 2
203 206 post :preview,
204 207 :id => 4,
205 208 :board_id => 1,
206 209 :message => {:subject => "", :content => "Previewed text"}
207 210 assert_response :success
208 211 assert_template 'common/_preview'
209 212 end
210 213 end
General Comments 0
You need to be logged in to leave comments. Login now