From 0c7ba49e34a4aeec6aa2ade790ffa589f8296e7a Mon Sep 17 00:00:00 2001 From: danielvincent Date: Thu, 16 Sep 2010 16:03:18 -0700 Subject: [PATCH] DG IZ; aspect deletion --- app/controllers/aspects_controller.rb | 12 +++++-- app/helpers/aspects_helper.rb | 8 +++++ app/models/user.rb | 9 +++++ app/views/aspects/manage.html.haml | 10 +++--- lib/diaspora/user/friending.rb | 2 +- public/stylesheets/application.css | 43 +++++++++++++----------- public/stylesheets/sass/application.sass | 28 +++++++++------ spec/models/user_spec.rb | 27 +++++++++++++++ 8 files changed, 100 insertions(+), 39 deletions(-) diff --git a/app/controllers/aspects_controller.rb b/app/controllers/aspects_controller.rb index 87c41f097..88da5cc88 100644 --- a/app/controllers/aspects_controller.rb +++ b/app/controllers/aspects_controller.rb @@ -26,9 +26,15 @@ class AspectsController < ApplicationController def destroy @aspect = Aspect.find_by_id params[:id] - @aspect.destroy - flash[:notice] = "You are no longer sharing the aspect called #{@aspect.name}." - respond_with :location => aspects_url + + begin + current_user.drop_aspect @aspect + flash[:notice] = "#{@aspect.name} was successfully removed." + rescue RuntimeError => e + flash[:error] = e.message + end + + respond_with :location => aspects_manage_path end def show diff --git a/app/helpers/aspects_helper.rb b/app/helpers/aspects_helper.rb index adceab8fd..1c8f69b8a 100644 --- a/app/helpers/aspects_helper.rb +++ b/app/helpers/aspects_helper.rb @@ -7,4 +7,12 @@ module AspectsHelper def link_for_aspect( aspect ) link_to aspect.name, aspect end + + def remove_link( aspect ) + if aspect.people.size == 0 + link_to "remove", aspect, :method => :delete + else + "remove" + end + end end diff --git a/app/models/user.rb b/app/models/user.rb index 6a76ce772..216fe210c 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -65,6 +65,15 @@ class User Aspect.create(opts) end + def drop_aspect( aspect ) + if aspect.people.size == 0 + aspect.destroy + else + raise "Aspect not empty" + end + end + + def move_friend( opts = {}) return true if opts[:to] == opts[:from] friend = Person.first(:_id => opts[:friend_id]) diff --git a/app/views/aspects/manage.html.haml b/app/views/aspects/manage.html.haml index 8a6ed97ac..9a98b9d06 100644 --- a/app/views/aspects/manage.html.haml +++ b/app/views/aspects/manage.html.haml @@ -36,12 +36,12 @@ .aspect_name %h1{:contenteditable => true}= aspect.name - .tools - = link_to "add a new friend", "#add_request_pane_#{aspect.id}", :class => 'add_request_button' - | - = link_to "show", aspect_path(aspect) + %ul.tools + %li= link_to "add a new friend", "#add_request_pane_#{aspect.id}", :class => 'add_request_button' + %li= link_to "show", aspect_path(aspect) + %li!= remove_link(aspect) - %ul{:id => aspect.id} + %ul.dropzone{:id => aspect.id} -if aspect.people.size < 1 %li.grey Drag to add people diff --git a/lib/diaspora/user/friending.rb b/lib/diaspora/user/friending.rb index cf55a2bf0..e79608381 100644 --- a/lib/diaspora/user/friending.rb +++ b/lib/diaspora/user/friending.rb @@ -62,7 +62,7 @@ module Diaspora def receive_friend_request(friend_request) Rails.logger.info("receiving friend request #{friend_request.to_json}") - if request_from_me?(friend_request) + if request_from_me?(friend_request) && self.aspect_by_id(friend_request.aspect_id) aspect = self.aspect_by_id(friend_request.aspect_id) activate_friend(friend_request.person, aspect) diff --git a/public/stylesheets/application.css b/public/stylesheets/application.css index fdef9e4b9..f4c40f465 100644 --- a/public/stylesheets/application.css +++ b/public/stylesheets/application.css @@ -513,20 +513,33 @@ h1.big_text { .requests .aspect_name, .remove .aspect_name { position: relative; } - .aspect .aspect_name .tools, - .requests .aspect_name .tools, - .remove .aspect_name .tools { + .aspect .aspect_name ul.tools, + .requests .aspect_name ul.tools, + .remove .aspect_name ul.tools { position: absolute; top: 10px; right: 0; - display: inline; } - .aspect .aspect_name:hover .tools, - .requests .aspect_name:hover .tools, - .remove .aspect_name:hover .tools { - display: inline; } - .aspect ul, - .requests ul, - .remove ul { + display: inline; + padding: 0; + margin: 0; + list-style: none; } + .aspect .aspect_name ul.tools li, + .requests .aspect_name ul.tools li, + .remove .aspect_name ul.tools li { + display: inline; + margin-right: 1em; } + .aspect .aspect_name ul.tools li:last-child, + .requests .aspect_name ul.tools li:last-child, + .remove .aspect_name ul.tools li:last-child { + margin-right: 0; } + .aspect .grey, + .requests .grey, + .remove .grey { + color: #999999; + cursor: default; } + .aspect ul.dropzone, + .requests ul.dropzone, + .remove ul.dropzone { min-height: 20px; margin: 0; margin-bottom: 25px; @@ -574,14 +587,6 @@ h1.big_text { -webkit-box-shadow: 0 1px 3px #333333; -moz-box-shadow: 0 2px 4px #333333; opacity: 0.9; } - .aspect .person .grey, - .aspect .requested_person .grey, - .requests .person .grey, - .requests .requested_person .grey, - .remove .person .grey, - .remove .requested_person .grey { - font-style: italic; - color: #666666; } #notification_badge { position: fixed; diff --git a/public/stylesheets/sass/application.sass b/public/stylesheets/sass/application.sass index 3912e1648..85c5f3cdc 100644 --- a/public/stylesheets/sass/application.sass +++ b/public/stylesheets/sass/application.sass @@ -678,17 +678,29 @@ h1.big_text .aspect_name :position relative - .tools + ul.tools :position absolute :top 10px :right 0 :display inline - - &:hover - .tools + :padding 0 + :margin 0 + :list + :style none + li :display inline + :margin + :right 1em - ul + &:last-child + :margin + :right 0 + + .grey + :color #999 + :cursor default + + ul.dropzone :min-height 20px :margin 0 :bottom 25px @@ -726,12 +738,6 @@ h1.big_text :-moz-box-shadow 0 2px 4px #333 :opacity 0.9 - - .grey - :font - :style italic - :color #666 - #notification_badge :position fixed :bottom 0 diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index a02f5271a..6e8355021 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -22,4 +22,31 @@ describe User do @user.profile.image_url.should == "http://clown.com" end end + + describe 'aspects' do + it 'should delete an empty aspect' do + @user.aspects.include?(@aspect).should == true + @user.drop_aspect(@aspect) + @user.reload + + @user.aspects.include?(@aspect).should == false + end + + it 'should not delete an aspect with friends' do + user2 = Factory.create(:user) + aspect2 = user2.aspect(:name => 'stuff') + user2.reload + aspect2.reload + + friend_users(@user, Aspect.find_by_id(@aspect.id), user2, Aspect.find_by_id(aspect2.id)) + @aspect.reload + + @user.aspects.include?(@aspect).should == true + + proc{@user.drop_aspect(@aspect)}.should raise_error /Aspect not empty/ + + @user.reload + @user.aspects.include?(@aspect).should == true + end + end end