diff --git a/app/controllers/groups_controller.rb b/app/controllers/groups_controller.rb index 66fc47480..b3abcf607 100644 --- a/app/controllers/groups_controller.rb +++ b/app/controllers/groups_controller.rb @@ -49,4 +49,15 @@ class GroupsController < ApplicationController end end + def move_person + unless current_user.move_friend( :friend_id => params[:friend_id], :from => params[:from], :to => params[:to][:to]) + flash[:error] = "didn't work #{params.inspect}" + end + if group = Group.first(:id => params[:to][:to]) + redirect_to group + else + redirect_to Person.first(:id => params[:friend_id]) + end + end + end diff --git a/app/controllers/people_controller.rb b/app/controllers/people_controller.rb index 8b6ae849b..0a6a44dfc 100644 --- a/app/controllers/people_controller.rb +++ b/app/controllers/people_controller.rb @@ -16,6 +16,10 @@ class PeopleController < ApplicationController @profile = @person.profile @person_groups = current_user.groups_with_person(@person) + + @groups_array = current_user.groups.collect{|x| [x.to_s, x.id]} + + @posts = Post.where(:person_id => @person.id, :_id.in => current_user.visible_post_ids).paginate :page => params[:page], :order => 'created_at DESC' @latest_status_message = current_user.raw_visible_posts.find_all_by__type_and_person_id("StatusMessage", params[:id]).last diff --git a/app/models/group.rb b/app/models/group.rb index a01dcca5f..432ff697c 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 44976cbc5..409e964e3 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -40,6 +40,26 @@ 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 + from_group.person_ids.delete(ensure_bson(friend.id)) + posts_to_move.each{ |x| from_group.post_ids.delete(x.id)} + 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 51c528a5a..a0b69f271 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 8bf59a1aa..76fc3acef 100644 --- a/app/views/people/show.html.haml +++ b/app/views/people/show.html.haml @@ -17,6 +17,13 @@ %i= "friends since: #{how_long_ago(@person)}" %li ="groups: #{@person_groups}" + = "edit" + = @groups_array.inspect + = form_tag move_person_path + = select :to, :to, @groups_array + = hidden_field_tag :from, :from, :value => @person_groups.first.id + = hidden_field_tag :friend_id, :friend_id, :value => @person.id + = submit_tag "save" %li url: = @person.url diff --git a/config/routes.rb b/config/routes.rb index 25a243172..e3c5b352a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -20,7 +20,7 @@ Diaspora::Application.routes.draw do match 'login', :to => 'devise/sessions#new', :as => "new_user_session" match 'logout', :to => 'devise/sessions#destroy', :as => "destroy_user_session" match 'get_to_the_choppa', :to => 'devise/registrations#new', :as => "new_user_registration" - + match 'groups/move_person', :to => 'groups#move_person', :as => 'move_person' #public routes # match 'webfinger', :to => 'publics#webfinger' diff --git a/public/javascripts/group-edit.js b/public/javascripts/group-edit.js new file mode 100644 index 000000000..500691266 --- /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!'); + $("
  • ").text(ui.draggable.text()).appendTo(this); + } + }); + + + }); diff --git a/spec/models/group_spec.rb b/spec/models/group_spec.rb index 90c572503..936deb6b7 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