diff --git a/app/controllers/conversations_controller.rb b/app/controllers/conversations_controller.rb index 5806f43589d9c132079c50a2b00ae6115fab4366..2a4008d230d77e009114eb354aa3620a4439e086 100644 --- a/app/controllers/conversations_controller.rb +++ b/app/controllers/conversations_controller.rb @@ -5,20 +5,20 @@ class ConversationsController < ApplicationController def index @conversations = Conversation.joins(:conversation_visibilities).where( - :conversation_visibilities => {:person_id => current_user.person.id}).paginate( - :page => params[:page], :per_page => 15, :order => 'updated_at DESC') + :conversation_visibilities => {:person_id => current_user.person.id}).paginate( + :page => params[:page], :per_page => 15, :order => 'updated_at DESC') - @visibilities = ConversationVisibility.where( :person_id => current_user.person.id ).paginate( - :page => params[:page], :per_page => 15, :order => 'updated_at DESC') + @visibilities = ConversationVisibility.where(:person_id => current_user.person.id).paginate( + :page => params[:page], :per_page => 15, :order => 'updated_at DESC') @unread_counts = {} - @visibilities.each{|v| @unread_counts[v.conversation_id] = v.unread} + @visibilities.each { |v| @unread_counts[v.conversation_id] = v.unread } @authors = {} - @conversations.each{|c| @authors[c.id] = c.last_author} - + @conversations.each { |c| @authors[c.id] = c.last_author } + @conversation = Conversation.joins(:conversation_visibilities).where( - :conversation_visibilities => {:person_id => current_user.person.id, :conversation_id => params[:conversation_id]}).first + :conversation_visibilities => {:person_id => current_user.person.id, :conversation_id => params[:conversation_id]}).first end def create @@ -43,7 +43,7 @@ class ConversationsController < ApplicationController def show @conversation = Conversation.joins(:conversation_visibilities).where(:id => params[:id], - :conversation_visibilities => {:person_id => current_user.person.id}).first + :conversation_visibilities => {:person_id => current_user.person.id}).first if @visibility = ConversationVisibility.where(:conversation_id => params[:id], :person_id => current_user.person.id).first @visibility.unread = 0 @@ -58,7 +58,7 @@ class ConversationsController < ApplicationController end def new - @all_contacts_and_ids = current_user.contacts.map{|c| {:value => c.id, :name => c.person.name}} + @all_contacts_and_ids = current_user.contacts.map { |c| {:value => c.id, :name => c.person.name} } @contact = current_user.contacts.find(params[:contact_id]) if params[:contact_id] render :layout => false end diff --git a/spec/controllers/conversations_controller_spec.rb b/spec/controllers/conversations_controller_spec.rb index 50f6ed14a0686265fcf264904fc5cba77031b46d..77216148a2529ea2ae212735f9c72a89ff270cd2 100644 --- a/spec/controllers/conversations_controller_spec.rb +++ b/spec/controllers/conversations_controller_spec.rb @@ -4,15 +4,24 @@ describe ConversationsController do render_views before do - @user1 = alice - sign_in :user, @user1 + @alice = alice + sign_in :user, @alice end describe '#new' do - it 'succeeds' do + before do get :new + end + it 'succeeds' do response.should be_success end + it "assigns a list of the user's contacts" do + assigns(:all_contacts_and_ids).should == @alice.contacts.collect{|c| {"value" => c.id, "name" => c.person.name}} + end + it "assigns a contact if passed a contact id" do + get :new, :contact_id => @alice.contacts.first.id + assigns(:contact).should == @alice.contacts.first + end end describe '#index' do @@ -22,12 +31,9 @@ describe ConversationsController do end it 'retrieves all conversations for a user' do - hash = { :author => @user1.person, :participant_ids => [@user1.contacts.first.person.id, @user1.person.id], - :subject => 'not spam', :text => 'cool stuff'} - - 3.times do - cnv = Conversation.create(hash) - end + hash = {:author => @alice.person, :participant_ids => [@alice.contacts.first.person.id, @alice.person.id], + :subject => 'not spam', :text => 'cool stuff'} + 3.times { Conversation.create(hash) } get :index assigns[:conversations].count.should == 3 @@ -36,10 +42,12 @@ describe ConversationsController do describe '#create' do before do - @hash = {:conversation => { - :subject => "secret stuff", - :text => 'text'}, - :contact_ids => '@user1.contacts.first.id'} + @hash = { + :conversation => { + :subject => "secret stuff", + :text => 'text'}, + :contact_ids => [@alice.contacts.first.id] + } end it 'creates a conversation' do @@ -55,19 +63,17 @@ describe ConversationsController do end it 'sets the author to the current_user' do - pending @hash[:author] = Factory.create(:user) post :create, @hash - Message.first.author.should == @user1.person - Conversation.first.author.should == @user1.person + Message.first.author.should == @alice.person + Conversation.first.author.should == @alice.person end it 'dispatches the conversation' do - cnv = Conversation.create(@hash[:conversation].merge({ - :author => @user1.person, - :participant_ids => [@user1.contacts.first.person.id]})) + cnv = Conversation.create( + @hash[:conversation].merge({:author => @alice.person, :participant_ids => [@alice.contacts.first.person.id]})) - p = Postzord::Dispatch.new(@user1, cnv) + p = Postzord::Dispatch.new(@alice, cnv) Postzord::Dispatch.stub!(:new).and_return(p) p.should_receive(:post) post :create, @hash @@ -76,8 +82,8 @@ describe ConversationsController do describe '#show' do before do - hash = { :author => @user1.person, :participant_ids => [@user1.contacts.first.person.id, @user1.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', :text => 'cool stuff'} @conversation = Conversation.create(hash) end