From 23262065867fd7e38f96345220e208ec3a265dbb Mon Sep 17 00:00:00 2001 From: Gonzalo Rodriguez Date: Fri, 12 Aug 2011 21:26:39 -0300 Subject: [PATCH] Refactor conversations creation --- app/controllers/conversations_controller.rb | 2 ++ app/models/conversation.rb | 10 +------- ...nversation_visibilities_controller_spec.rb | 8 ++++-- .../conversations_controller_spec.rb | 25 ++++++++++++++----- spec/controllers/messages_controller_spec.rb | 8 ++++-- spec/mailers/notifier_spec.rb | 8 ++++-- spec/models/conversation_spec.rb | 10 +++++--- spec/models/message_spec.rb | 16 +++++++++--- .../notifications/private_message_spec.rb | 8 ++++-- 9 files changed, 65 insertions(+), 30 deletions(-) diff --git a/app/controllers/conversations_controller.rb b/app/controllers/conversations_controller.rb index eaa6ed522..80d963caa 100644 --- a/app/controllers/conversations_controller.rb +++ b/app/controllers/conversations_controller.rb @@ -28,6 +28,8 @@ class ConversationsController < ApplicationController params[:conversation][:participant_ids] = person_ids | [current_user.person.id] params[:conversation][:author] = current_user.person + message_text = params[:conversation].delete(:text) + params[:conversation][:messages_attributes] = [ {:author => current_user.person, :text => message_text }] if @conversation = Conversation.create(params[:conversation]) Postzord::Dispatch.new(current_user, @conversation).post diff --git a/app/models/conversation.rb b/app/models/conversation.rb index 785e80628..e10b27030 100644 --- a/app/models/conversation.rb +++ b/app/models/conversation.rb @@ -15,15 +15,7 @@ class Conversation < ActiveRecord::Base belongs_to :author, :class_name => 'Person' - def self.create(opts={}) - opts = opts.dup - msg_opts = {:author => opts[:author], :text => opts.delete(:text)} - - cnv = super(opts) - message = Message.new(msg_opts.merge({:conversation_id => cnv.id})) - message.save - cnv - end + accepts_nested_attributes_for :messages def recipients self.participants - [self.author] diff --git a/spec/controllers/conversation_visibilities_controller_spec.rb b/spec/controllers/conversation_visibilities_controller_spec.rb index 514396903..211316957 100644 --- a/spec/controllers/conversation_visibilities_controller_spec.rb +++ b/spec/controllers/conversation_visibilities_controller_spec.rb @@ -9,8 +9,12 @@ describe ConversationVisibilitiesController do @user1 = alice sign_in :user, @user1 - hash = { :author => @user1.person, :participant_ids => [@user1.contacts.first.person.id, @user1.person.id], - :subject => 'not spam', :text => 'cool stuff'} + hash = { + :author => @user1.person, + :participant_ids => [@user1.contacts.first.person.id, @user1.person.id], + :subject => 'not spam', + :messages_attributes => [ {:author => @user1.person, :text => 'cool stuff'} ] + } @conversation = Conversation.create(hash) end diff --git a/spec/controllers/conversations_controller_spec.rb b/spec/controllers/conversations_controller_spec.rb index a1287d166..6ee8b1d59 100644 --- a/spec/controllers/conversations_controller_spec.rb +++ b/spec/controllers/conversations_controller_spec.rb @@ -40,8 +40,12 @@ describe ConversationsController do end it 'retrieves all conversations for a user' do - hash = {:author => alice.person, :participant_ids => [alice.contacts.first.person.id, alice.person.id], - :subject => 'not spam', :text => 'cool stuff'} + hash = { + :author => alice.person, + :participant_ids => [alice.contacts.first.person.id, alice.person.id], + :subject => 'not spam', + :messages_attributes => [ {:author => alice.person, :text => 'cool stuff'} ] + } 3.times { Conversation.create(hash) } get :index @@ -80,8 +84,13 @@ describe ConversationsController do it 'dispatches the conversation' do cnv = Conversation.create( - @hash[:conversation].merge({:author => alice.person, :participant_ids => [alice.contacts.first.person.id]})) - + { + :author => alice.person, + :participant_ids => [alice.contacts.first.person.id, alice.person.id], + :subject => 'not spam', + :messages_attributes => [ {:author => alice.person, :text => 'cool stuff'} ] + } + ) p = Postzord::Dispatch.new(alice, cnv) Postzord::Dispatch.stub!(:new).and_return(p) p.should_receive(:post) @@ -91,8 +100,12 @@ describe ConversationsController do describe '#show' do before do - hash = {:author => alice.person, :participant_ids => [alice.contacts.first.person.id, alice.person.id], - :subject => 'not spam', :text => 'cool stuff'} + hash = { + :author => alice.person, + :participant_ids => [alice.contacts.first.person.id, alice.person.id], + :subject => 'not spam', + :messages_attributes => [ {:author => alice.person, :text => 'cool stuff'} ] + } @conversation = Conversation.create(hash) end diff --git a/spec/controllers/messages_controller_spec.rb b/spec/controllers/messages_controller_spec.rb index 90ab7259c..6a120effd 100644 --- a/spec/controllers/messages_controller_spec.rb +++ b/spec/controllers/messages_controller_spec.rb @@ -17,8 +17,12 @@ describe MessagesController do describe '#create' do before do - @create_hash = { :author => @user1.person, :participant_ids => [@user1.contacts.first.person.id, @user1.person.id], - :subject => "cool stuff", :text => "stuff"} + @create_hash = { + :author => @user1.person, + :participant_ids => [@user1.contacts.first.person.id, @user1.person.id], + :subject => 'cool stuff', + :messages_attributes => [ {:author => @user1.person, :text => 'stuff'} ] + } end context "on my own post" do diff --git a/spec/mailers/notifier_spec.rb b/spec/mailers/notifier_spec.rb index 5640c4963..17326f582 100644 --- a/spec/mailers/notifier_spec.rb +++ b/spec/mailers/notifier_spec.rb @@ -134,8 +134,12 @@ describe Notifier do @user2 = bob @participant_ids = @user2.contacts.map{|c| c.person.id} + [ @user2.person.id] - @create_hash = { :author => @user2.person, :participant_ids => @participant_ids , - :subject => "cool stuff", :text => 'hey'} + @create_hash = { + :author => @user2.person, + :participant_ids => @participant_ids, + :subject => "cool stuff", + :messages_attributes => [ {:author => @user2.person, :text => 'hey'} ] + } @cnv = Conversation.create(@create_hash) diff --git a/spec/models/conversation_spec.rb b/spec/models/conversation_spec.rb index 147efbd3c..f81357df2 100644 --- a/spec/models/conversation_spec.rb +++ b/spec/models/conversation_spec.rb @@ -10,8 +10,12 @@ describe Conversation do @user2 = bob @participant_ids = [@user1.contacts.first.person.id, @user1.person.id] - @create_hash = { :author => @user1.person, :participant_ids => @participant_ids , - :subject => "cool stuff", :text => 'hey'} + @create_hash = { + :author => @user1.person, + :participant_ids => @participant_ids, + :subject => "cool stuff", + :messages_attributes => [ {:author => @user1.person, :text => 'hey'} ] + } end it 'creates a message on create' do @@ -60,8 +64,8 @@ describe Conversation do describe '#receive' do before do - Conversation.destroy_all Message.destroy_all + Conversation.destroy_all end it 'creates a message' do diff --git a/spec/models/message_spec.rb b/spec/models/message_spec.rb index 859900f4b..00aacfb10 100644 --- a/spec/models/message_spec.rb +++ b/spec/models/message_spec.rb @@ -10,8 +10,12 @@ describe Message do @user1 = alice @user2 = bob - @create_hash = { :author => @user1.person, :participant_ids => [@user1.contacts.first.person.id, @user1.person.id], - :subject => "cool stuff", :text => "stuff"} + @create_hash = { + :author => @user1.person, + :participant_ids => [@user1.contacts.first.person.id, @user1.person.id], + :subject => "cool stuff", + :messages_attributes => [ {:author => @user1.person, :text => 'stuff'} ] + } @cnv = Conversation.create(@create_hash) @message = @cnv.messages.first @@ -64,8 +68,12 @@ describe Message do before do @local_luke, @local_leia, @remote_raphael = set_up_friends - cnv_hash = {:author => @remote_raphael, :participant_ids => [@local_luke.person, @local_leia.person, @remote_raphael].map(&:id), - :subject => 'cool story, bro', :text => 'hey'} + cnv_hash = { + :author => @remote_raphael, + :participant_ids => [@local_luke.person, @local_leia.person, @remote_raphael].map(&:id), + :subject => 'cool story, bro', + :messages_attributes => [ {:author => @remote_raphael, :text => 'hey'} ] + } @remote_parent = Conversation.create(cnv_hash.dup) diff --git a/spec/models/notifications/private_message_spec.rb b/spec/models/notifications/private_message_spec.rb index 42b05f9ec..82bfde6b3 100644 --- a/spec/models/notifications/private_message_spec.rb +++ b/spec/models/notifications/private_message_spec.rb @@ -9,8 +9,12 @@ describe Notifications::PrivateMessage do @user1 = alice @user2 = bob - @create_hash = { :author => @user1.person, :participant_ids => [@user1.contacts.first.person.id, @user1.person.id], - :subject => "cool stuff", :text => "stuff"} + @create_hash = { + :author => @user1.person, + :participant_ids => [@user1.contacts.first.person.id, @user1.person.id], + :subject => 'cool stuff', + :messages_attributes => [ {:author => @user1.person, :text => 'stuff'} ] + } @cnv = Conversation.create(@create_hash) @msg = @cnv.messages.first