Refactor MessagesController

This commit is contained in:
Gonzalo 2012-10-13 03:35:14 -02:00
parent 811ef66b1c
commit 83809c924a
2 changed files with 71 additions and 60 deletions

View file

@ -3,18 +3,18 @@
# the COPYRIGHT file. # the COPYRIGHT file.
class MessagesController < ApplicationController class MessagesController < ApplicationController
include ApplicationHelper
before_filter :authenticate_user! before_filter :authenticate_user!
respond_to :html, :mobile respond_to :html, :mobile
respond_to :json, :only => :show respond_to :json, :only => :show
def create def create
cnv = Conversation.joins(:conversation_visibilities).where(:id => params[:conversation_id], conversation = Conversation.find(params[:conversation_id])
:conversation_visibilities => {:person_id => current_user.person_id}).first
if cnv message = conversation.messages.build(
message = Message.new(:conversation_id => cnv.id, :text => params[:message][:text], :author => current_user.person) :text => params[:message][:text],
:author => current_user.person
)
if message.save if message.save
Rails.logger.info("event=create type=comment user=#{current_user.diaspora_handle} status=success message=#{message.id} chars=#{params[:message][:text].length}") Rails.logger.info("event=create type=comment user=#{current_user.diaspora_handle} status=success message=#{message.id} chars=#{params[:message][:text].length}")
@ -22,10 +22,6 @@ class MessagesController < ApplicationController
else else
flash[:error] = I18n.t('conversations.new_message.fail') flash[:error] = I18n.t('conversations.new_message.fail')
end end
redirect_to conversations_path(:conversation_id => cnv.id) redirect_to conversations_path(:conversation_id => conversation.id)
else
render :nothing => true, :status => 422
end end
end
end end

View file

@ -6,98 +6,113 @@ require 'spec_helper'
describe MessagesController do describe MessagesController do
before do before do
@user1 = alice sign_in :user, alice
@user2 = bob
@aspect1 = @user1.aspects.first
@aspect2 = @user2.aspects.first
sign_in :user, @user1
end end
describe '#create' do describe '#create' do
before do before do
@create_hash = { @conversation_params = {
:author => @user1.person, :author => alice.person,
:participant_ids => [@user1.contacts.first.person.id, @user1.person.id], :participant_ids => [alice.contacts.first.person.id, alice.person.id],
:subject => 'cool stuff', :subject => 'cool stuff',
:messages_attributes => [ {:author => @user1.person, :text => 'stuff'} ] :messages_attributes => [ {:author => alice.person, :text => 'stuff'} ]
} }
end end
context "on my own post" do context "on my own post" do
before do before do
@cnv = Conversation.create(@create_hash) @conversation = Conversation.create!(@conversation_params)
end end
context "with a valid message" do context "with a valid message" do
before do before do
@message_hash = {:conversation_id => @cnv.id, :message => {:text => "here is something else"}} @message_params = {
:conversation_id => @conversation.id,
:message => { :text => "here is something else" }
}
end end
it 'redirects to conversation' do it 'redirects to conversation' do
lambda{ lambda {
post :create, @message_hash post :create, @message_params
}.should change(Message, :count).by(1) }.should change(Message, :count).by(1)
response.code.should == '302' response.status.should == 302
response.should redirect_to(conversations_path(:conversation_id => @cnv)) response.should redirect_to(conversations_path(:conversation_id => @conversation))
end end
end end
context "with an empty message" do context "with an empty message" do
before do before do
@message_hash = {:conversation_id => @cnv.id, :message => {:text => " "}} @message_params = {
:conversation_id => @conversation.id,
:message => { :text => " " }
}
end end
it 'redirects to conversation' do it 'does not create the message' do
lambda{ lambda {
post :create, @message_hash post :create, @message_params
}.should_not change(Message, :count).by(1) }.should_not change(Message, :count)
response.code.should == '302' flash[:error].should be_present
response.should redirect_to(conversations_path(:conversation_id => @cnv))
end end
end end
end end
context "on a post from a contact" do context "on a post from a contact" do
before do before do
@create_hash[:author] = @user2.person @conversation_params[:author] = bob.person
@cnv = Conversation.create(@create_hash) @conversation = Conversation.create!(@conversation_params)
@message_hash = {:conversation_id => @cnv.id, :message => {:text => "here is something else"}} @message_params = {
:conversation_id => @conversation.id,
:message => { :text => "here is something else" }
}
end end
it 'comments' do it 'comments' do
post :create, @message_hash post :create, @message_params
response.code.should == '302' response.status.should == 302
response.should redirect_to(conversations_path(:conversation_id => @cnv)) response.should redirect_to(conversations_path(:conversation_id => @conversation))
end end
it "doesn't overwrite author_id" do it "doesn't overwrite author_id" do
new_user = FactoryGirl.create(:user) new_user = FactoryGirl.create(:user)
@message_hash[:author_id] = new_user.person.id.to_s @message_params[:author_id] = new_user.person.id.to_s
post :create, @message_hash
Message.find_by_text(@message_hash[:message][:text]).author_id.should == @user1.person.id post :create, @message_params
created_message = Message.find_by_text(@message_params[:message][:text])
created_message.author.should == alice.person
end end
it "doesn't overwrite id" do it "doesn't overwrite id" do
old_message = Message.create(:text => "hello", :author_id => @user1.person.id, :conversation_id => @cnv.id) old_message = Message.create!(
@message_hash[:id] = old_message.id :text => "hello",
post :create, @message_hash :author_id => alice.person.id,
:conversation_id => @conversation.id
)
@message_params[:id] = old_message.id
post :create, @message_params
old_message.reload.text.should == 'hello' old_message.reload.text.should == 'hello'
end end
end end
context 'on a post from a stranger' do context 'on a post from a stranger' do
before do before do
@create_hash[:author] = eve.person conversation = Conversation.create!(
@create_hash[:participant_ids] = [eve.person.id, bob.person.id] :author => eve.person,
@cnv = Conversation.create(@create_hash) :participant_ids => [eve.person.id, bob.person.id]
@message_hash = {:conversation_id => @cnv.id, :message => {:text => "here is something else"}} )
@message_params = {
:conversation_id => conversation.id,
:message => { :text => "here is something else" }
}
end end
it 'posts no comment' do it 'does not create the message' do
post :create, @message_hash lambda {
response.code.should == '422' post :create, @message_params
}.should_not change(Message, :count)
flash[:error].should be_present
end end
end end
end end