From 8b081a48b41a2ff12562446537b4a0001e2b51c0 Mon Sep 17 00:00:00 2001
From: danielgrippi <daniel@joindiaspora.com>
Date: Wed, 27 Apr 2011 12:00:58 -0700
Subject: [PATCH] likes controller now has destroy method.  commented out
 dislikes from views.  unlike links not functional.

---
 app/controllers/aspects_controller.rb      |  2 +-
 app/controllers/likes_controller.rb        | 18 ++++++++++++-
 app/views/likes/_likes.haml                | 14 +++++-----
 app/views/shared/_stream_element.html.haml | 23 +++++++++--------
 config/locales/diaspora/en.yml             | 25 +++++++++---------
 config/routes.rb                           |  2 +-
 public/stylesheets/sass/application.sass   | 11 +++++---
 spec/controllers/likes_controller_spec.rb  | 30 ++++++++++++++++++++--
 8 files changed, 87 insertions(+), 38 deletions(-)

diff --git a/app/controllers/aspects_controller.rb b/app/controllers/aspects_controller.rb
index 15c01772e2..782a841bf6 100644
--- a/app/controllers/aspects_controller.rb
+++ b/app/controllers/aspects_controller.rb
@@ -3,7 +3,7 @@
 #   the COPYRIGHT file.
 
 class AspectsController < ApplicationController
-  helper :comments, :aspect_memberships
+  helper :comments, :aspect_memberships, :likes
   before_filter :authenticate_user!
   before_filter :save_sort_order, :only => :index
   before_filter :ensure_page, :only => :index
diff --git a/app/controllers/likes_controller.rb b/app/controllers/likes_controller.rb
index 6eef3bebb6..72a9032fd7 100644
--- a/app/controllers/likes_controller.rb
+++ b/app/controllers/likes_controller.rb
@@ -1,4 +1,4 @@
-#   Copyright (c) 2010, Diaspora Inc.  This file is
+
 #   licensed under the Affero General Public License version 3 or later.  See
 #   the COPYRIGHT file.
 
@@ -41,4 +41,20 @@ class LikesController < ApplicationController
       render :nothing => true, :status => 422
     end
   end
+
+  def destroy
+    if @like = Like.where(:id => params[:id], :author_id => current_user.person.id).first
+      current_user.retract(@like)
+      respond_to do |format|
+        format.mobile{ redirect_to @like.post }
+        format.js {render :nothing => true, :status => 204}
+      end
+    else
+      respond_to do |format|
+        format.mobile {redirect_to :back}
+        format.js {render :nothing => true, :status => 403}
+      end
+    end
+  end
+
 end
diff --git a/app/views/likes/_likes.haml b/app/views/likes/_likes.haml
index 2c1ebe1e85..dbddecccd8 100644
--- a/app/views/likes/_likes.haml
+++ b/app/views/likes/_likes.haml
@@ -2,16 +2,16 @@
 -#   licensed under the Affero General Public License version 3 or later.  See
 -#   the COPYRIGHT file.
 
-- if likes.length > 0
+- if likes.size > 0
   .likes
     = image_tag('icons/happy_smiley.png')
     = link_to t('.people_like_this', :count => likes.length), "#", :class => "expand_likes"
     %span.hidden.likes_list
       = likes_list(likes)
 
-- if dislikes.length > 0
-  .dislikes
-    = image_tag('icons/sad_smiley.png')
-    = link_to t('.people_dislike_this', :count => dislikes.length), "#", :class => "expand_dislikes"
-    %span.hidden.dislikes_list
-      = likes_list(dislikes)
+/- if dislikes.length > 0
+/  .dislikes
+/    = image_tag('icons/sad_smiley.png')
+/    = link_to t('.people_dislike_this', :count => dislikes.length), "#", :class => "expand_dislikes"
+/    %span.hidden.dislikes_list
+/      = likes_list(dislikes)
diff --git a/app/views/shared/_stream_element.html.haml b/app/views/shared/_stream_element.html.haml
index 2b13768c58..3ccf84159e 100644
--- a/app/views/shared/_stream_element.html.haml
+++ b/app/views/shared/_stream_element.html.haml
@@ -39,15 +39,18 @@
         - unless (defined?(@commenting_disabled) && @commenting_disabled)
           = link_to t('comments.new_comment.comment'), '#', :class => 'focus_comment_textarea'
 
-          / TODO(likes)
-          /- if (defined?(current_user) && !current_user.liked?(post))
-          /  %span.like_links
-          /    |
-          /    = link_to t('.like'), likes_path(:positive => 'true', :post_id => post.id ), :method => :post, :class => "like_it", :remote => true
-          /    |
-          /    = link_to t('.dislike'), likes_path(:positive => 'false', :post_id => post.id), :method => :post, :class => "dislike_it", :remote => true
-
-      /.likes_container
-      /  = render "likes/likes", :post_id => post.id, :likes => post.likes, :dislikes => post.dislikes, :current_user => current_user
+          - if defined?(current_user)
+            %span.like_links
+              |
+              - if !current_user.liked?(post)
+                = link_to t('.like'), likes_path(:positive => 'true', :post_id => post.id ), :method => :post, :class => "like_it", :remote => true
+              - else
+                = link_to t('.unlike'), likes_path(:post_id => post.id ), :method => :delete, :class => "like_it", :remote => true
+
+              /|
+              /= link_to t('.dislike'), like_path(:positive => 'false', :post_id => post.id), :method => :post, :class => "dislike_it", :remote => true
+
+      .likes_container
+        = render "likes/likes", :post_id => post.id, :likes => post.likes, :dislikes => post.dislikes, :current_user => current_user
 
       = render "comments/comments", :post => post, :comments => post.comments, :current_user => current_user, :condensed => true, :commenting_disabled => (defined?(@commenting_disabled) && @commenting_disabled)
diff --git a/config/locales/diaspora/en.yml b/config/locales/diaspora/en.yml
index af94f78dad..60d14b83f9 100644
--- a/config/locales/diaspora/en.yml
+++ b/config/locales/diaspora/en.yml
@@ -271,17 +271,17 @@ en:
   likes:
     likes:
       people_like_this:
-        zero: "no people liked this"
-        one: "1 person liked this"
-        few: "%{count} people liked this"
-        many: "%{count} people liked this"
-        other: "%{count} people liked this"
+        zero: "no likes"
+        one: "%{count} like"
+        few: "%{count} likes"
+        many: "%{count} likes"
+        other: "%{count} likes"
       people_dislike_this:
-        zero: "no people disliked this"
-        one: "1 person disliked this"
-        few: "%{count} people disliked this"
-        many: "%{count} people disliked this"
-        other: "%{count} people disliked this"
+        zero: "no dislikes"
+        one: "%{count} dislike"
+        few: "%{count} dislikes"
+        many: "%{count} dislikes"
+        other: "%{count} dislikes"
 
   notifications:
     request_accepted: "accepted your share request."
@@ -570,8 +570,9 @@ en:
       all_contacts: "All contacts"
       cannot_remove: "Cannot remove person from last aspect. (If you want to disconnect from this person you must remove contact.)"
     stream_element:
-      like: "I like this"
-      dislike: "I dislike this"
+      like: "Like"
+      unlike: "Unlike"
+      dislike: "Dislike"
     footer:
       logged_in_as: "logged in as %{name}"
       your_aspects: "your aspects"
diff --git a/config/routes.rb b/config/routes.rb
index 55ff7b6261..d6a2c6186a 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -20,8 +20,8 @@ Diaspora::Application.routes.draw do
   end
 
   resources :comments, :only => [:create, :destroy]
-  resource :like,      :only => [:create]
 
+  resources :likes, :only => [:create, :destroy]
 
   resources :conversations do
     resources :messages, :only => [:create, :show]
diff --git a/public/stylesheets/sass/application.sass b/public/stylesheets/sass/application.sass
index c7d5d335ae..e922fece78 100644
--- a/public/stylesheets/sass/application.sass
+++ b/public/stylesheets/sass/application.sass
@@ -2828,11 +2828,14 @@ h1.tag
     a
       :padding 1px
       :vertical-align middle
-      :font-size smaller
+      :font-size 11px
     img
-      :width 12px
-      :height 12px
-      :margin-left 0.5em
+      :position relative
+      :width 14px
+      :height 14px
+      :margin-left 5px
+      :top 2px
+
 
 #contacts_of_contact 
   .section
diff --git a/spec/controllers/likes_controller_spec.rb b/spec/controllers/likes_controller_spec.rb
index eb7a844071..d336d19819 100644
--- a/spec/controllers/likes_controller_spec.rb
+++ b/spec/controllers/likes_controller_spec.rb
@@ -11,7 +11,8 @@ describe LikesController do
 
     @aspect1 = @user1.aspects.first
     @aspect2 = @user2.aspects.first
-
+  
+    @controller.stub(:current_user).and_return(alice)
     sign_in :user, @user1
   end
 
@@ -70,4 +71,29 @@ describe LikesController do
       end
     end
   end
-end
\ No newline at end of file
+
+  describe '#destroy' do
+    context 'your like' do
+      before do
+        @message = bob.post(:status_message, :text => "hey", :to => @aspect1.id)
+        @like = alice.build_like(true, :on => @message)
+        @like.save
+      end
+
+      it 'lets a user destroy their like' do
+        alice.should_receive(:retract).with(@like)
+        delete :destroy, :format => "js", :id => @like.id
+        response.status.should == 204
+      end
+
+      it 'does not let a user destroy other likes' do
+        like2 = eve.build_like(true, :on => @message)
+        like2.save
+
+        alice.should_not_receive(:retract)
+        delete :destroy, :format => "js", :id => like2.id
+        response.status.should == 403
+      end
+    end
+  end
+end
-- 
GitLab