Refactor conversations creation

This commit is contained in:
Gonzalo Rodriguez 2011-08-12 21:26:39 -03:00
parent fe93fd5c91
commit 2326206586
9 changed files with 65 additions and 30 deletions

View file

@ -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

View file

@ -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]

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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)

View file

@ -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

View file

@ -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)

View file

@ -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