diff --git a/' b/' deleted file mode 100644 index 95265a85e..000000000 --- a/' +++ /dev/null @@ -1,43 +0,0 @@ -# Copyright (c) 2010, Diaspora Inc. This file is -# licensed under the Affero General Public License version 3 or later. See -# the COPYRIGHT file. - -require 'spec_helper' - -describe AspectMembershipsController do - before do - @user = alice - @user2 = bob - - @aspect0 = @user.aspects.first - @aspect1 = @user.aspects.create(:name => "another aspect") - @aspect2 = @user2.aspects.first - - @contact = @user.contact_for(@user2.person) - @user.getting_started = false - @user.save - sign_in :user, @user - @controller.stub(:current_user).and_return(@user) - request.env["HTTP_REFERER"] = 'http://' + request.host - end - - describe "#new" do - it 'succeeds' do - get :new - response.should be_success - end - end - - describe "#destroy" do - it 'removes contacts from an aspect' do - @user.add_contact_to_aspect(@contact, @aspect1) - delete :destroy, - :format => 'js', :id => 123, - :person_id => @user2.person.id, - :aspect_id => @aspect0.id - response.should be_success - @aspect0.reload - @aspect0.contacts.include?(@contact).should be false - end - end -end diff --git a/app/controllers/aspect_memberships_controller.rb b/app/controllers/aspect_memberships_controller.rb index e1f8cfe66..85350c26f 100644 --- a/app/controllers/aspect_memberships_controller.rb +++ b/app/controllers/aspect_memberships_controller.rb @@ -10,28 +10,33 @@ class AspectMembershipsController < ApplicationController render :nothing => true end - + def index + raise + end + + + def destroy #note :id is garbage - @person_id = params[:person_id] @aspect_id = params[:aspect_id] - contact = current_user.contact_for(Person.where(:id => @person_id).first) - - membership = contact ? contact.aspect_memberships.where(:aspect_id => @aspect_id).first : nil + @contact = current_user.contact_for(Person.where(:id => @person_id).first) + membership = @contact ? @contact.aspect_memberships.where(:aspect_id => @aspect_id).first : nil if membership && membership.destroy flash.now[:notice] = I18n.t 'aspect_memberships.destroy.success' respond_to do |format| - format.js { render :json => {:button_html => - render_to_string(:partial => 'aspects/remove_from_aspect', - :locals => {:aspect_id => @aspect_id, - :person_id => @person_id}), - :aspect_id => @aspect_id - }} + format.js do + render :json => {:button_html => + render_to_string(:partial => 'aspect_memberships/remove_from_aspect', + :locals => {:aspect => membership.aspect, + :person => @contact.person, + :contact => @contact }), + :aspect_id => @aspect_id} + end format.html{ redirect_to :back } @@ -50,17 +55,17 @@ class AspectMembershipsController < ApplicationController def create @person = Person.find(params[:person_id]) - @aspect = current_user.aspects.find(params[:aspect_id]) + @aspect = current_user.aspects.where(params[:aspect_id]).first @contact = current_user.contact_for(@person) - current_user.add_contact_to_aspect(contact, aspect) + 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', + :button_html => render_to_string(:partial => 'aspect_memberships/add_to_aspect', :locals => {:aspect_id => @aspect.id, :person_id => @person.id}), :badge_html => render_to_string(:partial => 'aspects/aspect_badge', diff --git a/app/controllers/aspects_controller.rb b/app/controllers/aspects_controller.rb index 41f47e969..20af009da 100644 --- a/app/controllers/aspects_controller.rb +++ b/app/controllers/aspects_controller.rb @@ -55,14 +55,13 @@ class AspectsController < ApplicationController @person = Person.where(:id => params[:aspect][:person_id]).first @contact = current_user.contact_for(@person) - @contact = current_user.contact_for(@person) respond_to do |format| format.js { render :json => {:html => render_to_string( :partial => 'aspects/aspect_list_item', :locals => {:aspect => @aspect, :person => @person, :contact => @contact} - )},:status => 201 } + ), :aspect_id => @aspect.id},:status => 201 } end else respond_with @aspect diff --git a/app/controllers/contacts_controller.rb b/app/controllers/contacts_controller.rb index 284f91608..b005eef24 100644 --- a/app/controllers/contacts_controller.rb +++ b/app/controllers/contacts_controller.rb @@ -19,7 +19,7 @@ class ContactsController < ApplicationController respond_to do |format| format.js { render :json => { - :button_html => render_to_string(:partial => 'aspects/add_to_aspect', + :button_html => render_to_string(:partial => 'aspect_memberships/add_to_aspect', :locals => {:aspect_id => @aspect.id, :person_id => @person.id}), :badge_html => render_to_string(:partial => 'aspects/aspect_badge', @@ -30,15 +30,13 @@ class ContactsController < ApplicationController end private - def request_to_aspect( aspect, person) + + 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/app/helpers/aspects_helper.rb b/app/helpers/aspects_helper.rb index c80215209..b658cc2f4 100644 --- a/app/helpers/aspects_helper.rb +++ b/app/helpers/aspects_helper.rb @@ -18,29 +18,36 @@ module AspectsHelper end end - def add_to_aspect_button(aspect_id, person_id) + def add_to_aspect_button(aspect_id, person_id, kontroller) link_to image_tag('icons/monotone_plus_add_round.png'), - {:controller => "aspects", - :action => 'add_to_aspect', + {:controller => kontroller, + :action => 'create', :aspect_id => aspect_id, :person_id => person_id}, :remote => true, + :method => 'post', :class => 'add button' end def remove_from_aspect_button(aspect_id, person_id) link_to image_tag('icons/monotone_check_yes.png'), - {:controller => "aspects", - :action => 'remove_from_aspect', + {:controller => "aspect_memberships", + :action => 'destroy', + :id => 42, :aspect_id => aspect_id, :person_id => person_id}, :remote => true, + :method => 'delete', :class => 'added button' end + def contact_or_membership(contact) + (contact) ? 'aspect_memberships' : 'contacts' + end + def aspect_membership_button(aspect, contact, person) if contact.nil? || !aspect.contacts.include?(contact) - add_to_aspect_button(aspect.id, person.id) + add_to_aspect_button(aspect.id, person.id, contact_or_membership(contact)) else remove_from_aspect_button(aspect.id, person.id) end diff --git a/app/models/user.rb b/app/models/user.rb index fa4ec5e52..ea3b3acca 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -92,6 +92,20 @@ 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/app/views/aspects/_add_to_aspect.haml b/app/views/aspect_memberships/_add_to_aspect.haml similarity index 100% rename from app/views/aspects/_add_to_aspect.haml rename to app/views/aspect_memberships/_add_to_aspect.haml diff --git a/app/views/aspects/_remove_from_aspect.haml b/app/views/aspect_memberships/_remove_from_aspect.haml similarity index 75% rename from app/views/aspects/_remove_from_aspect.haml rename to app/views/aspect_memberships/_remove_from_aspect.haml index 606997d90..6bfa71452 100644 --- a/app/views/aspects/_remove_from_aspect.haml +++ b/app/views/aspect_memberships/_remove_from_aspect.haml @@ -3,5 +3,5 @@ -# the COPYRIGHT file. -= add_to_aspect_button(aspect_id,person_id) += aspect_membership_button(aspect, contact, person) diff --git a/app/views/aspects/add_to_aspect.js.haml b/app/views/aspect_memberships/add_to_aspect.js.haml similarity index 100% rename from app/views/aspects/add_to_aspect.js.haml rename to app/views/aspect_memberships/add_to_aspect.js.haml diff --git a/app/views/aspects/remove_from_aspect.js.haml b/app/views/aspect_memberships/remove_from_aspect.js.haml similarity index 75% rename from app/views/aspects/remove_from_aspect.js.haml rename to app/views/aspect_memberships/remove_from_aspect.js.haml index 90add3f2f..737e6b67c 100644 --- a/app/views/aspects/remove_from_aspect.js.haml +++ b/app/views/aspect_memberships/remove_from_aspect.js.haml @@ -3,5 +3,5 @@ -# the COPYRIGHT file. -= add_to_aspect_button(@aspect_id,@person_id) += aspect_membership_button(aspect, person, contact) diff --git a/features/connects_users.feature b/features/connects_users.feature index 22b9a9849..185a220f4 100644 --- a/features/connects_users.feature +++ b/features/connects_users.feature @@ -40,6 +40,7 @@ Feature: sending and receiving requests When I go to the home page Then I go to the aspects manage page + Then I should see 1 contact in "Super People" Then I go to the destroy user session page diff --git a/public/javascripts/contact-list.js b/public/javascripts/contact-list.js index 01007a325..6f09bf280 100644 --- a/public/javascripts/contact-list.js +++ b/public/javascripts/contact-list.js @@ -101,6 +101,7 @@ $(document).ready(function() { $('.new_aspect').live('ajax:success', function(data, json, xhr){ var json = JSON.parse(json); $('#aspects_list ul').append(json.html); + $("#aspects_list ul li[data-guid='" + json.aspect_id + "'] .add.button").click(); }); List.initialize(); diff --git a/spec/controllers/aspect_memberships_controller_spec.rb b/spec/controllers/aspect_memberships_controller_spec.rb index 8063b8aac..007d914b9 100644 --- a/spec/controllers/aspect_memberships_controller_spec.rb +++ b/spec/controllers/aspect_memberships_controller_spec.rb @@ -5,6 +5,8 @@ require 'spec_helper' describe AspectMembershipsController do + render_views + before do @user = alice @user2 = bob diff --git a/spec/controllers/aspects_controller_spec.rb b/spec/controllers/aspects_controller_spec.rb index 9bbab776d..775cc9d5b 100644 --- a/spec/controllers/aspects_controller_spec.rb +++ b/spec/controllers/aspects_controller_spec.rb @@ -156,14 +156,6 @@ describe AspectsController do response.should redirect_to(:back) end end - it "adds to aspect if the person_id is present" do - @aspect = @user.aspects.create(:name => "new aspect") - @user.aspects.stub!(:create).and_return(@aspect) - @controller.should_receive(:invite_or_add_contact_to_aspect).with( - anything(), @user2.person, @user.contact_for(@user2.person)) - - post :create, "aspect" => {"name" => "new aspect", :person_id => @user2.person.id, :share_with => true} - end end describe "#manage" do diff --git a/spec/controllers/contacts_controller_spec.rb b/spec/controllers/contacts_controller_spec.rb index f1641a638..59c2271a6 100644 --- a/spec/controllers/contacts_controller_spec.rb +++ b/spec/controllers/contacts_controller_spec.rb @@ -9,8 +9,19 @@ describe ContactsController do render_views before do - @user = alice + @user = alice + @user2 = bob + + @aspect0 = @user.aspects.first + @aspect1 = @user.aspects.create(:name => "another aspect") + @aspect2 = @user2.aspects.first + + @contact = @user.contact_for(@user2.person) + @user.getting_started = false + @user.save sign_in :user, @user + @controller.stub(:current_user).and_return(@user) + request.env["HTTP_REFERER"] = 'http://' + request.host end describe 'new' do