Commit c6eb5d03 authored by maxwell's avatar maxwell Committed by zhitomirskiyi
Browse files

IZ MS; refactored aspect_membership_controller, moved and updated views accordingly

parent 8ae23f57
# 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
......@@ -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',
......
......@@ -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
......
......@@ -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
......@@ -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
......
......@@ -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
......
......@@ -3,5 +3,5 @@
-# the COPYRIGHT file.
= add_to_aspect_button(aspect_id,person_id)
= aspect_membership_button(aspect, contact, person)
......@@ -3,5 +3,5 @@
-# the COPYRIGHT file.
= add_to_aspect_button(@aspect_id,@person_id)
= aspect_membership_button(aspect, person, contact)
......@@ -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
......
......@@ -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();
......
......@@ -5,6 +5,8 @@
require 'spec_helper'
describe AspectMembershipsController do
render_views
before do
@user = alice
@user2 = bob
......
......@@ -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
......
......@@ -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
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment