From 25b93471c1861b77190f15dac17b526619710858 Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Tue, 4 Feb 2014 22:31:44 +0000 Subject: [PATCH] Preserve message details over validation errors If a validation error occurs while saving a message then make sure the values are preserved in the new form. --- app/controllers/message_controller.rb | 11 +++++-- app/views/message/new.html.erb | 8 ++--- test/functional/message_controller_test.rb | 38 ++++++++++++++++++++++ 3 files changed, 50 insertions(+), 7 deletions(-) diff --git a/app/controllers/message_controller.rb b/app/controllers/message_controller.rb index 38c9b2f3d..fd638e4e7 100644 --- a/app/controllers/message_controller.rb +++ b/app/controllers/message_controller.rb @@ -29,6 +29,7 @@ class MessageController < ApplicationController end end else + @message = Message.new(:recipient => @this_user) @title = t 'message.new.title' end end @@ -40,9 +41,13 @@ class MessageController < ApplicationController if message.to_user_id == @user.id then message.update_attribute(:message_read, true) - @body = "On #{message.sent_on} #{message.sender.display_name} wrote:\n\n#{message.body.gsub(/^/, '> ')}" - @title = @subject = "Re: #{message.title.sub(/^Re:\s*/, '')}" - @this_user = User.find(message.from_user_id) + @message = Message.new( + :recipient => message.sender, + :title => "Re: #{message.title.sub(/^Re:\s*/, '')}", + :body => "On #{message.sent_on} #{message.sender.display_name} wrote:\n\n#{message.body.gsub(/^/, '> ')}", + ) + + @title = @message.title render :action => 'new' else diff --git a/app/views/message/new.html.erb b/app/views/message/new.html.erb index b8af5aac9..35f5b4a04 100644 --- a/app/views/message/new.html.erb +++ b/app/views/message/new.html.erb @@ -1,18 +1,18 @@ <% content_for :heading do %> -

<%= raw(t'message.new.send_message_to', :name => link_to(h(@this_user.display_name), {:controller => 'user', :action => 'view', :display_name => @this_user.display_name})) %>

+

<%= raw(t'message.new.send_message_to', :name => link_to(h(@message.recipient.display_name), {:controller => 'user', :action => 'view', :display_name => @message.recipient.display_name})) %>

<% end %> <%= error_messages_for 'message' %> -<%= form_for :message, :html => { :class => 'standard-form' }, :url => { :action => "new", :display_name => @this_user.display_name } do |f| %> +<%= form_for :message, :html => { :class => 'standard-form' }, :url => { :action => "new", :display_name => @message.recipient.display_name } do |f| %>
- <%= f.text_field :title, :size => 60, :value => @subject, :class => "richtext_title" %> + <%= f.text_field :title, :size => 60, :class => "richtext_title" %>
- <%= richtext_area :message, :body, :cols => 80, :rows => 20, :value => @body %> + <%= richtext_area :message, :body, :cols => 80, :rows => 20 %>
<%= submit_tag t('message.new.send_button') %> diff --git a/test/functional/message_controller_test.rb b/test/functional/message_controller_test.rb index 45d0a267c..66ca8bc70 100644 --- a/test/functional/message_controller_test.rb +++ b/test/functional/message_controller_test.rb @@ -65,6 +65,44 @@ class MessageControllerTest < ActionController::TestCase assert_select "input[type='submit'][value='Send']", :count => 1 end + # Check that the subject is preserved over errors + assert_difference "ActionMailer::Base.deliveries.size", 0 do + assert_difference "Message.count", 0 do + post :new, + :display_name => users(:public_user).display_name, + :message => { :title => "Test Message", :body => "" } + end + end + assert_response :success + assert_template "new" + assert_select "title", "OpenStreetMap | Send message" + assert_select "form[action='#{new_message_path(:display_name => users(:public_user).display_name)}']", :count => 1 do + assert_select "input#message_title", :count => 1 do + assert_select "[value=Test Message]" + end + assert_select "textarea#message_body", :text => "", :count => 1 + assert_select "input[type='submit'][value='Send']", :count => 1 + end + + # Check that the body text is preserved over errors + assert_difference "ActionMailer::Base.deliveries.size", 0 do + assert_difference "Message.count", 0 do + post :new, + :display_name => users(:public_user).display_name, + :message => { :title => "", :body => "Test message body" } + end + end + assert_response :success + assert_template "new" + assert_select "title", "OpenStreetMap | Send message" + assert_select "form[action='#{new_message_path(:display_name => users(:public_user).display_name)}']", :count => 1 do + assert_select "input#message_title", :count => 1 do + assert_select "[value=]" + end + assert_select "textarea#message_body", :text => "Test message body", :count => 1 + assert_select "input[type='submit'][value='Send']", :count => 1 + end + # Check that sending a message works assert_difference "ActionMailer::Base.deliveries.size", 1 do assert_difference "Message.count", 1 do -- 2.39.5