From 8c2e6dd8cb63da316cec0822cdbe681542a072b1 Mon Sep 17 00:00:00 2001
From: maxwell <maxwell@joindiaspora.com>
Date: Wed, 16 Feb 2011 15:40:42 -0800
Subject: [PATCH] aspect membership refactor complete

---
 .../aspect_memberships_controller.rb          |  2 +-
 app/controllers/contacts_controller.rb        |  3 +-
 app/models/user.rb                            | 24 +++--------
 features/connects_users.feature               | 21 ++++++++++
 features/step_definitions/custom_web_steps.rb |  6 +++
 features/step_definitions/user_steps.rb       |  2 +
 public/javascripts/contact-list.js            |  3 ++
 spec/models/aspect_spec.rb                    | 41 ++-----------------
 8 files changed, 44 insertions(+), 58 deletions(-)

diff --git a/app/controllers/aspect_memberships_controller.rb b/app/controllers/aspect_memberships_controller.rb
index 85350c26fc..c7be3a84f7 100644
--- a/app/controllers/aspect_memberships_controller.rb
+++ b/app/controllers/aspect_memberships_controller.rb
@@ -55,7 +55,7 @@ class AspectMembershipsController < ApplicationController
 
   def create
     @person = Person.find(params[:person_id])
-    @aspect = current_user.aspects.where(params[:aspect_id]).first
+    @aspect = current_user.aspects.where(:id => params[:aspect_id]).first
     @contact = current_user.contact_for(@person)
 
 
diff --git a/app/controllers/contacts_controller.rb b/app/controllers/contacts_controller.rb
index b005eef241..aa64ba2c1c 100644
--- a/app/controllers/contacts_controller.rb
+++ b/app/controllers/contacts_controller.rb
@@ -6,12 +6,13 @@ class ContactsController < ApplicationController
   before_filter :authenticate_user!
   
   def new
+    #should be share_with?
     render :nothing => true
   end
 
   def create
     @person = Person.find(params[:person_id])
-    @aspect = current_user.aspects.find(params[:aspect_id])
+    @aspect = current_user.aspects.where(:id => params[:aspect_id]).first
 
     request_to_aspect(@aspect, @person)
 
diff --git a/app/models/user.rb b/app/models/user.rb
index ea3b3acca1..bd2a68563f 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -74,11 +74,13 @@ class User < ActiveRecord::Base
   end
 
   def move_contact(person, to_aspect, from_aspect)
+    return true if to_aspect == from_aspect
     contact = contact_for(person)
-    if to_aspect == from_aspect
-      true
-    elsif add_contact_to_aspect(contact, to_aspect)
-      delete_person_from_aspect(person.id, from_aspect.id)
+    if add_contact_to_aspect(contact, to_aspect)
+      membership = contact ? contact.aspect_memberships.where(:aspect_id => from_aspect.id).first : nil
+      return ( membership && membership.destroy )
+    else 
+      false
     end
   end
 
@@ -92,20 +94,6 @@ class User < ActiveRecord::Base
     contact.aspect_memberships.create!(:aspect => aspect)
   end
 
-
-  def delete_person_from_aspect(person_id, aspect_id, opts = {})
-    aspect = Aspect.find(aspect_id)
-    raise "Can not delete a person from an aspect you do not own" unless aspect.user == self
-    contact = contact_for Person.find(person_id)
-
-    if opts[:force] || contact.aspect_ids.count > 1
-      contact.aspects.delete(aspect)
-    else
-      raise "Can not delete a person from last aspect"
-    end
-  end
-  
-
   ######## Posting ########
   def build_post(class_name, opts = {})
     opts[:person] = self.person
diff --git a/features/connects_users.feature b/features/connects_users.feature
index 185a220f4e..e6a96148d2 100644
--- a/features/connects_users.feature
+++ b/features/connects_users.feature
@@ -29,6 +29,27 @@ Feature: sending and receiving requests
     And I am on the aspects manage page
     Then I should see 1 contact in "Besties"
 
+  Scenario: accepting a contact request to multiple aspects
+    When I sign in as "alice@alice.alice"
+    And I am on "bob@bob.bob"'s page
+    And I press the 1st ".share_with.button" within "#author_info"
+    And I press the 1st ".add.button" within "#facebox #aspects_list ul > li:first-child"
+    And I wait for the ajax to finish
+    And I press the 1st ".add.button" within "#facebox #aspects_list ul > li:nth-child(2)"
+    And I wait for the ajax to finish
+
+   When I go to the home page
+   Then I go to the aspects manage page
+
+   Then I should see 1 contact in "Unicorns"
+   Then I should see 1 contact in "Besties"
+   Then I go to the destroy user session page
+
+   When I sign in as "bob@bob.bob"
+   And I am on the aspects manage page
+   Then I should see 1 contact in "Besties"
+
+
   
   Scenario: accepting a contact request into a new aspect
     When I sign in as "alice@alice.alice"
diff --git a/features/step_definitions/custom_web_steps.rb b/features/step_definitions/custom_web_steps.rb
index 8f3f6169fe..e3a5289924 100644
--- a/features/step_definitions/custom_web_steps.rb
+++ b/features/step_definitions/custom_web_steps.rb
@@ -40,6 +40,12 @@ When /^I press the first "([^"]*)"(?: within "([^"]*)")?$/ do |link_selector, wi
    find(:css, link_selector).click
   end
 end
+
+When /^I press the ([\d])(nd|rd|st|th) "([^"]*)"(?: within "([^"]*)")?$/ do |number,rd, link_selector, within_selector|
+  with_scope(within_selector) do
+   find(:css, link_selector+":nth-child(#{number})").click
+  end
+end
 Then /^(?:|I )should see a "([^"]*)"(?: within "([^"]*)")?$/ do |selector, scope_selector|
   with_scope(scope_selector) do
     page.has_css?(selector).should be_true
diff --git a/features/step_definitions/user_steps.rb b/features/step_definitions/user_steps.rb
index 1efa30eaf5..04a82a8116 100644
--- a/features/step_definitions/user_steps.rb
+++ b/features/step_definitions/user_steps.rb
@@ -2,6 +2,7 @@ Given /^a user with username "([^\"]*)" and password "([^\"]*)"$/ do |username,
   @me ||= Factory(:user, :username       => username, :password => password,
           :password_confirmation => password, :getting_started => false)
   @me.aspects.create(:name => "Besties")
+  @me.aspects.create(:name => "Unicorns")
 end
 
 Given /^that I am a rock star$/ do
@@ -12,6 +13,7 @@ Given /^a user with email "([^\"]*)"$/ do |email|
   user = Factory(:user, :email => email, :password => 'password',
           :password_confirmation => 'password', :getting_started => false)
   user.aspects.create(:name => "Besties")
+  user.aspects.create(:name => "Unicorns")
 end
 
 Given /^I have been invited by an admin$/ do
diff --git a/public/javascripts/contact-list.js b/public/javascripts/contact-list.js
index 6f09bf2806..f85cfbb054 100644
--- a/public/javascripts/contact-list.js
+++ b/public/javascripts/contact-list.js
@@ -87,6 +87,9 @@ $(document).ready(function() {
 
     $(".badges").prepend(json.badge_html);
     $(this).parent().html(json.button_html);
+
+    $('.aspect_list ul').find('.add').each(function(a,b){$(b).attr('href', $(b).attr('href').replace('contacts','aspect_memberships'));})
+
     $(this).fadeTo(200,1);
   });
 
diff --git a/spec/models/aspect_spec.rb b/spec/models/aspect_spec.rb
index 87f6397d05..bf7a2548cb 100644
--- a/spec/models/aspect_spec.rb
+++ b/spec/models/aspect_spec.rb
@@ -169,42 +169,6 @@ describe Aspect do
         user.add_contact_to_aspect(@contact, aspect).should == true
       end
     end
-
-    describe '#delete_person_from_aspect' do
-      it 'deletes a user from the aspect' do
-        user.add_contact_to_aspect(@contact, aspect1)
-        user.reload
-        user.delete_person_from_aspect(user2.person.id, aspect1.id)
-        user.reload
-        aspect1.contacts(true).include?(@contact).should be_false
-      end
-
-      it 'should check to make sure you have the aspect ' do
-        proc{user.delete_person_from_aspect(user2.person.id, aspect2.id) }.should raise_error /Can not delete a person from an aspect you do not own/
-      end
-
-      it 'deletes no posts' do
-         user.add_contact_to_aspect(@contact, aspect1)
-         user.reload
-         user2.post(:status_message, :message => "Hey Dude", :to => aspect2.id)
-         lambda{
-           user.delete_person_from_aspect(user2.person.id, aspect1.id)
-         }.should_not change(Post, :count)
-      end
-
-      it 'should not allow removing a contact from their last aspect' do
-        proc{user.delete_person_from_aspect(user2.person.id, aspect.id) }.should raise_error /Can not delete a person from last aspect/
-      end
-
-      it 'should allow a force removal of a contact from an aspect' do
-        @contact.aspect_ids.should_receive(:count).exactly(0).times
-
-        user.add_contact_to_aspect(@contact, aspect1)
-        user.delete_person_from_aspect(user2.person.id, aspect.id, :force => true)
-      end
-
-    end
-
     context 'moving and removing posts' do
       before do
         @message  = user2.post(:status_message, :message => "Hey Dude", :to => aspect2.id)
@@ -214,7 +178,8 @@ describe Aspect do
 
       it 'should keep the contact\'s posts in previous aspect' do
         aspect.post_ids.count.should == 1
-        user.delete_person_from_aspect(user2.person.id, aspect.id, :force => true)
+        user.move_contact(user2.person, user.aspects.create(:name => "Another aspect"), aspect)
+
 
         aspect.reload
         aspect.post_ids.count.should == 1
@@ -222,7 +187,7 @@ describe Aspect do
 
       it 'should not delete other peoples posts' do
         connect_users(user, aspect, user3, aspect3)
-        user.delete_person_from_aspect(user3.person.id, aspect.id, :force => true)
+        user.move_contact(user3.person, user.aspects.create(:name => "Another aspect"), aspect)
         aspect.reload
         aspect.posts.should == [@message]
       end
-- 
GitLab