From 8ae23f5705f34649c63e135a4d97b711827216c7 Mon Sep 17 00:00:00 2001 From: maxwell Date: Tue, 15 Feb 2011 19:01:35 -0800 Subject: [PATCH] wip --- .../aspect_memberships_controller.rb | 23 +++++++++ app/controllers/aspects_controller.rb | 39 --------------- app/controllers/contacts_controller.rb | 33 ++++++++++++ .../aspect_memberships_controller_spec.rb | 12 +++++ spec/controllers/aspects_controller_spec.rb | 50 ------------------- spec/controllers/contacts_controller_spec.rb | 42 ++++++++++++++++ 6 files changed, 110 insertions(+), 89 deletions(-) diff --git a/app/controllers/aspect_memberships_controller.rb b/app/controllers/aspect_memberships_controller.rb index 28bdaea19..e1f8cfe66 100644 --- a/app/controllers/aspect_memberships_controller.rb +++ b/app/controllers/aspect_memberships_controller.rb @@ -47,4 +47,27 @@ class AspectMembershipsController < ApplicationController end end end + + def create + @person = Person.find(params[:person_id]) + @aspect = current_user.aspects.find(params[:aspect_id]) + @contact = current_user.contact_for(@person) + + + current_user.add_contact_to_aspect(contact, aspect) + + flash.now[:notice] = I18n.t 'aspects.add_to_aspect.success' + + respond_to do |format| + format.js { render :json => { + :button_html => render_to_string(:partial => 'aspects/add_to_aspect', + :locals => {:aspect_id => @aspect.id, + :person_id => @person.id}), + :badge_html => render_to_string(:partial => 'aspects/aspect_badge', + :locals => {:aspect => @aspect}) + }} + format.html{ redirect_to aspect_path(@aspect.id)} + end + end + end diff --git a/app/controllers/aspects_controller.rb b/app/controllers/aspects_controller.rb index e577c44b1..41f47e969 100644 --- a/app/controllers/aspects_controller.rb +++ b/app/controllers/aspects_controller.rb @@ -55,8 +55,6 @@ class AspectsController < ApplicationController @person = Person.where(:id => params[:aspect][:person_id]).first @contact = current_user.contact_for(@person) - invite_or_add_contact_to_aspect(@aspect, @person, @contact) - @contact = current_user.contact_for(@person) respond_to do |format| format.js { render :json => {:html => render_to_string( @@ -155,41 +153,4 @@ class AspectsController < ApplicationController render :text => response_hash.to_json end - - def add_to_aspect - @person = Person.find(params[:person_id]) - @aspect = current_user.aspects.find(params[:aspect_id]) - @contact = current_user.contact_for(@person) - - invite_or_add_contact_to_aspect(@aspect, @person, @contact) - - flash.now[:notice] = I18n.t 'aspects.add_to_aspect.success' - - respond_to do |format| - format.js { render :json => { - :button_html => render_to_string(:partial => 'aspects/add_to_aspect', - :locals => {:aspect_id => @aspect.id, - :person_id => @person.id}), - :badge_html => render_to_string(:partial => 'aspects/aspect_badge', - :locals => {:aspect => @aspect}) - }} - format.html{ redirect_to aspect_path(@aspect.id)} - end - end - - private - def invite_or_add_contact_to_aspect( aspect, person, contact) - if contact - current_user.add_contact_to_aspect(contact, aspect) - else - current_user.send_contact_request_to(person, aspect) - contact = current_user.contact_for(person) - - if request = Request.where(:sender_id => person.id, :recipient_id => current_user.person.id).first - request.destroy - contact.update_attributes(:pending => false) - end - end - - end end diff --git a/app/controllers/contacts_controller.rb b/app/controllers/contacts_controller.rb index f32fa8ae7..284f91608 100644 --- a/app/controllers/contacts_controller.rb +++ b/app/controllers/contacts_controller.rb @@ -8,4 +8,37 @@ class ContactsController < ApplicationController def new render :nothing => true end + + def create + @person = Person.find(params[:person_id]) + @aspect = current_user.aspects.find(params[:aspect_id]) + + request_to_aspect(@aspect, @person) + + flash.now[:notice] = I18n.t 'aspects.add_to_aspect.success' + + respond_to do |format| + format.js { render :json => { + :button_html => render_to_string(:partial => 'aspects/add_to_aspect', + :locals => {:aspect_id => @aspect.id, + :person_id => @person.id}), + :badge_html => render_to_string(:partial => 'aspects/aspect_badge', + :locals => {:aspect => @aspect}) + }} + format.html{ redirect_to aspect_path(@aspect.id)} + end + end + + private + def request_to_aspect( aspect, person) + current_user.send_contact_request_to(person, aspect) + contact = current_user.contact_for(person) + + if request = Request.where(:sender_id => person.id, :recipient_id => current_user.person.id).first + request.destroy + contact.update_attributes(:pending => false) + end + + end + end diff --git a/spec/controllers/aspect_memberships_controller_spec.rb b/spec/controllers/aspect_memberships_controller_spec.rb index 11fd0c1ec..8063b8aac 100644 --- a/spec/controllers/aspect_memberships_controller_spec.rb +++ b/spec/controllers/aspect_memberships_controller_spec.rb @@ -28,6 +28,18 @@ describe AspectMembershipsController do end end + describe '#create' do + it 'creates an aspect membership' do + @user.should_receive(:add_contact_to_aspect) + post :create, + :format => 'js', + :person_id => @user2.person.id, + :aspect_id => @aspect1.id + response.should be_success + end + end + + describe "#destroy" do it 'removes contacts from an aspect' do @user.add_contact_to_aspect(@contact, @aspect1) diff --git a/spec/controllers/aspects_controller_spec.rb b/spec/controllers/aspects_controller_spec.rb index bb59a3373..9bbab776d 100644 --- a/spec/controllers/aspects_controller_spec.rb +++ b/spec/controllers/aspects_controller_spec.rb @@ -262,56 +262,6 @@ describe AspectsController do end end - describe "#add_to_aspect" do - context 'with an incoming request' do - before do - @user3 = Factory.create(:user) - @user3.send_contact_request_to(@user.person, @user3.aspects.create(:name => "Walruses")) - end - it 'deletes the request' do - post 'add_to_aspect', - :format => 'js', - :person_id => @user3.person.id, - :aspect_id => @aspect1.id - Request.where(:sender_id => @user3.person.id, :recipient_id => @user.person.id).first.should be_nil - end - it 'does not leave the contact pending' do - post 'add_to_aspect', - :format => 'js', - :person_id => @user3.person.id, - :aspect_id => @aspect1.id - @user.contact_for(@user3.person).should_not be_pending - end - end - context 'with a non-contact' do - before do - @person = Factory(:person) - end - it 'calls send_contact_request_to' do - @user.should_receive(:send_contact_request_to).with(@person, @aspect1) - post 'add_to_aspect', - :format => 'js', - :person_id => @person.id, - :aspect_id => @aspect1.id - end - it 'does not call add_contact_to_aspect' do - @user.should_not_receive(:add_contact_to_aspect) - post 'add_to_aspect', - :format => 'js', - :person_id => @person.id, - :aspect_id => @aspect1.id - end - end - it 'adds the users to the aspect' do - @user.should_receive(:add_contact_to_aspect) - post 'add_to_aspect', - :format => 'js', - :person_id => @user2.person.id, - :aspect_id => @aspect1.id - response.should be_success - end - end - describe '#edit' do it 'renders' do get :edit, :id => @aspect0.id diff --git a/spec/controllers/contacts_controller_spec.rb b/spec/controllers/contacts_controller_spec.rb index 8ba4c86d8..f1641a638 100644 --- a/spec/controllers/contacts_controller_spec.rb +++ b/spec/controllers/contacts_controller_spec.rb @@ -21,4 +21,46 @@ describe ContactsController do response.should be_success end end + + describe 'create' do + context 'with an incoming request' do + before do + @user3 = Factory.create(:user) + @user3.send_contact_request_to(@user.person, @user3.aspects.create(:name => "Walruses")) + end + it 'deletes the request' do + post :create, + :format => 'js', + :person_id => @user3.person.id, + :aspect_id => @aspect1.id + Request.where(:sender_id => @user3.person.id, :recipient_id => @user.person.id).first.should be_nil + end + it 'does not leave the contact pending' do + post :create, + :format => 'js', + :person_id => @user3.person.id, + :aspect_id => @aspect1.id + @user.contact_for(@user3.person).should_not be_pending + end + end + context 'with a non-contact' do + before do + @person = Factory(:person) + end + it 'calls send_contact_request_to' do + @user.should_receive(:send_contact_request_to).with(@person, @aspect1) + post :create, + :format => 'js', + :person_id => @person.id, + :aspect_id => @aspect1.id + end + it 'does not call add_contact_to_aspect' do + @user.should_not_receive(:add_contact_to_aspect) + post :create, + :format => 'js', + :person_id => @person.id, + :aspect_id => @aspect1.id + end + end + end end