From 6623e86a5a34de6ffa181966e61147bd97509d5f Mon Sep 17 00:00:00 2001 From: kevivmatrix Date: Sat, 15 Jun 2013 13:59:35 +0530 Subject: [PATCH] private message sending changed to ajax added specs for change in controller one form of hash used in single line of code rendering create response on sending message redirect user to conversation page if message sent successfully removed deprecated way of rendering file conversations create for mobile fixed minor code style change updated changelog --- Changelog.md | 1 + app/assets/javascripts/view.js | 2 +- app/controllers/conversations_controller.rb | 16 +++++++----- app/views/conversations/create.js.erb | 11 ++++++++ app/views/conversations/new.haml | 2 +- .../conversations_controller_spec.rb | 26 +++++++++++++++++++ 6 files changed, 49 insertions(+), 9 deletions(-) create mode 100644 app/views/conversations/create.js.erb diff --git a/Changelog.md b/Changelog.md index a98146806..3adaed5ba 100644 --- a/Changelog.md +++ b/Changelog.md @@ -44,6 +44,7 @@ * Leaving the `to` field blank when sending a private message causes a server error [#4227](https://github.com/diaspora/diaspora/issues/4227) * Fix hashtags that start a line when posting to Facebook or Twitter [#3768](https://github.com/diaspora/diaspora/issues/3768) [#4154](https://github.com/diaspora/diaspora/issues/4154) * Show avatar of recent user in conversation list [#4237](https://github.com/diaspora/diaspora/issues/4237) +* Private message fails if contact not entered correctly [#4210](https://github.com/diaspora/diaspora/issues/4210) ## Features diff --git a/app/assets/javascripts/view.js b/app/assets/javascripts/view.js index 108939594..6bc5cc3e7 100644 --- a/app/assets/javascripts/view.js +++ b/app/assets/javascripts/view.js @@ -29,7 +29,7 @@ var View = { /* Clear forms after successful submit, this is some legacy dan hanson stuff, do we still want it? */ $.fn.clearForm = function() { return this.each(function() { - if ($(this).is('form')) { + if ($(this).is('form') && !$(this).hasClass('form_do_not_clear')) { return $(':input', this).clearForm(); } if ($(this).hasClass('clear_on_submit') || $(this).is(':text') || $(this).is(':password') || $(this).is('textarea')) { diff --git a/app/controllers/conversations_controller.rb b/app/controllers/conversations_controller.rb index c566746fe..986bcc145 100644 --- a/app/controllers/conversations_controller.rb +++ b/app/controllers/conversations_controller.rb @@ -39,20 +39,22 @@ class ConversationsController < ApplicationController message_text = params[:conversation].delete(:text) params[:conversation][:messages_attributes] = [ {:author => current_user.person, :text => message_text }] + @response = {} @conversation = Conversation.new(params[:conversation]) if person_ids.present? && @conversation.save Postzord::Dispatcher.build(current_user, @conversation).post - flash[:notice] = I18n.t('conversations.create.sent') + @response[:success] = true + @response[:message] = I18n.t('conversations.create.sent') + @response[:conversation_id] = @conversation.id else - flash[:error] = I18n.t('conversations.create.fail') + @response[:success] = false + @response[:message] = I18n.t('conversations.create.fail') if person_ids.blank? - flash[:error] = I18n.t('conversations.create.no_contact') + @response[:message] = I18n.t('conversations.create.no_contact') end end - if params[:profile] - redirect_to person_path(params[:profile]) - else - redirect_to conversations_path(:conversation_id => @conversation.id) + respond_to do |format| + format.js end end diff --git a/app/views/conversations/create.js.erb b/app/views/conversations/create.js.erb new file mode 100644 index 000000000..21912cebc --- /dev/null +++ b/app/views/conversations/create.js.erb @@ -0,0 +1,11 @@ +var response = <%= raw @response.to_json %>; +<% if session[:mobile_view] %> + window.location.href = "<%= conversations_path(conversation_id: @conversation.id) %>"; +<% else %> + Diaspora.page.flashMessages.render({ 'success':response.success, 'notice':response.message }); + if(response.success){ + $("#new_conversation").removeClass('form_do_not_clear').clearForm(); + $.facebox.close(); + window.location.href = "<%= conversations_path(conversation_id: @conversation.id) %>"; + } +<% end %> \ No newline at end of file diff --git a/app/views/conversations/new.haml b/app/views/conversations/new.haml index 484534866..36c10b4fd 100644 --- a/app/views/conversations/new.haml +++ b/app/views/conversations/new.haml @@ -32,7 +32,7 @@ %h3 = t('conversations.index.new_message') - = form_for Conversation.new do |conversation| + = form_for Conversation.new, html: {class: "new_conversation form_do_not_clear"}, remote: true do |conversation| .span-2 %h4 diff --git a/spec/controllers/conversations_controller_spec.rb b/spec/controllers/conversations_controller_spec.rb index fed371a3f..58e065fec 100644 --- a/spec/controllers/conversations_controller_spec.rb +++ b/spec/controllers/conversations_controller_spec.rb @@ -97,6 +97,13 @@ describe ConversationsController do }.should change(Message, :count).by(1) end + it 'should set response with success to true and message to success message' do + post :create, @hash + assigns[:response][:success].should == true + assigns[:response][:message].should == I18n.t('conversations.create.sent') + assigns[:response][:conversation_id].should == Conversation.first.id + end + it 'sets the author to the current_user' do @hash[:author] = FactoryGirl.create(:user) post :create, @hash @@ -143,6 +150,13 @@ describe ConversationsController do post :create, @hash }.should change(Message, :count).by(1) end + + it 'should set response with success to true and message to success message' do + post :create, @hash + assigns[:response][:success].should == true + assigns[:response][:message].should == I18n.t('conversations.create.sent') + assigns[:response][:conversation_id].should == Conversation.first.id + end end context 'with empty text' do @@ -167,6 +181,12 @@ describe ConversationsController do post :create, @hash }.should_not change(Message, :count).by(1) end + + it 'should set response with success to false and message to create fail' do + post :create, @hash + assigns[:response][:success].should == false + assigns[:response][:message].should == I18n.t('conversations.create.fail') + end end context 'with empty contact' do @@ -191,6 +211,12 @@ describe ConversationsController do post :create, @hash }.should_not change(Message, :count).by(1) end + + it 'should set response with success to false and message to fail due to no contact' do + post :create, @hash + assigns[:response][:success].should == false + assigns[:response][:message].should == I18n.t('conversations.create.no_contact') + end end context 'with nil contact' do