diff --git a/app/controllers/groups_controller.rb b/app/controllers/groups_controller.rb index 66fc4748032ac58d5905e3935464241d448bd535..4810d64528e8df2018da4a2620780b2f41771b92 100644 --- a/app/controllers/groups_controller.rb +++ b/app/controllers/groups_controller.rb @@ -49,4 +49,9 @@ class GroupsController < ApplicationController end end + def move_person + current_user.move_friend( :friend_id => params[:person_id], :from => params[:old_group_id], :to => params[:new_group_id]) + render :nothing => true + end + end diff --git a/app/models/group.rb b/app/models/group.rb index a01dcca5f48beabb5eb46b139129b87b47486081..432ff697c091094e323a28f4c1227b565f6c27b8 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -19,5 +19,10 @@ class Group def to_s name end + + def posts_by_person_id( id ) + id = ensure_bson id + posts.detect{|x| x.person.id == id } + end end diff --git a/app/models/user.rb b/app/models/user.rb index 5032ef27358462b34b856defccc366d47450d46b..f3b0fcabf8c9380e76381a74f8c355d846e8bda2 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -40,6 +40,28 @@ class User Group.create(opts) end + def move_friend( opts = {}) + return true if opts[:to] == opts[:from] + friend = Person.first(:_id => opts[:friend_id]) + if self.friend_ids.include?(friend.id) + from_group = self.group_by_id(opts[:from]) + to_group = self.group_by_id(opts[:to]) + if from_group && to_group + posts_to_move = from_group.posts.find_all_by_person_id(friend.id) + puts posts_to_move.inspect + to_group.people << friend + to_group.posts << posts_to_move + puts to_group.inspect + from_group.person_ids.delete(ensure_bson(friend.id)) + posts_to_move.each{ |x| from_group.post_ids.delete(x.id)} + puts from_group.inspect + from_group.save + to_group.save + return true + end + end + false + end ######## Posting ######## def post(class_name, options = {}) options[:person] = self.person diff --git a/app/views/groups/edit.html.haml b/app/views/groups/edit.html.haml index 51c528a5ab7a636651b9dc7efacdc93efd3a20fc..a0b69f271996f91db218f634d2be9778b6183c69 100644 --- a/app/views/groups/edit.html.haml +++ b/app/views/groups/edit.html.haml @@ -6,17 +6,15 @@ %h1.big_text .back = link_to "⇧ #{@group.name}", @group - = "Editing Groups" -%ul - for group in @groups %li.group = group.name %ul -for person in group.people - %li..person + %li.person = person.real_name diff --git a/app/views/people/show.html.haml b/app/views/people/show.html.haml index 8bf59a1aa2770498e4fbc4c8610f2ac6bd196282..8503078636b1fd9017116f8ebdae14631c0df948 100644 --- a/app/views/people/show.html.haml +++ b/app/views/people/show.html.haml @@ -17,6 +17,11 @@ %i= "friends since: #{how_long_ago(@person)}" %li ="groups: #{@person_groups}" + = "edit" + = form_for Request.new do |f| + = f.select(:group_id, @groups_array) + = f.hidden_field :destination_url, :value => person.email + = f.submit "add friend" %li url: = @person.url diff --git a/public/javascripts/group-edit.js b/public/javascripts/group-edit.js new file mode 100644 index 0000000000000000000000000000000000000000..5006912663992a8dd429a642cafd9838b88b19c1 --- /dev/null +++ b/public/javascripts/group-edit.js @@ -0,0 +1,14 @@ +$(function() { + $("li .person").draggable({ + helper: 'clone', + cursor: 'move' + }); + $("li .group").droppable({ + drop: function(event, ui) { + //alert('dropped!'); + $("<li class='person'></li>").text(ui.draggable.text()).appendTo(this); + } + }); + + + }); diff --git a/spec/models/group_spec.rb b/spec/models/group_spec.rb index 90c572503ab81a619413c2caec3068002f20f21f..936deb6b75e684aeabfa38416d00eaf20cf24a7e 100644 --- a/spec/models/group_spec.rb +++ b/spec/models/group_spec.rb @@ -110,4 +110,59 @@ describe Group do end end + describe "group editing" do + before do + @group = @user.group(:name => 'losers') + @group2 = @user2.group(:name => 'failures') + friend_users(@user, @group, @user2, @group2) + @group.reload + @group3 = @user.group(:name => 'cats') + @user.reload + end + + it 'should be able to move a friend from one of users existing groups to another' do + @user.move_friend(:friend_id => @user2.person.id, :from => @group.id, :to => @group3.id) + @group.reload + @group3.reload + + @group.person_ids.include?(@user2.person.id).should be false + @group3.people.include?(@user2.person).should be true + end + + it "should not move a person who is not a friend" do + @user.move_friend(:friend_id => @friend.id, :from => @group.id, :to => @group3.id) + @group.reload + @group3.reload + @group.people.include?(@friend).should be false + @group3.people.include?(@friend).should be false + end + + it "should not move a person to a group that's not his" do + @user.move_friend(:friend_id => @user2.person.id, :from => @group.id, :to => @group2.id) + @group.reload + @group2.reload + @group.people.include?(@user2.person).should be true + @group2.people.include?(@user2.person).should be false + end + + it 'should move all the by that user to the new group' do + message = @user2.post(:status_message, :message => "Hey Dude", :to => @group2.id) + + @user.receive message.to_diaspora_xml + @group.reload + + @group.posts.count.should be 1 + @group3.posts.count.should be 0 + + @user.reload + @user.move_friend(:friend_id => @user2.person.id, :from => @group.id, :to => @group3.id) + @group.reload + @group3.reload + + @group3.posts.count.should be 1 + @group.posts.count.should be 0 + + end + + end end