User#like! method.

This commit is contained in:
danielgrippi 2012-02-03 14:42:03 -08:00
parent 4be7daaabf
commit a3e13e3c0e
11 changed files with 80 additions and 84 deletions

View file

@ -6,23 +6,18 @@ class LikesController < ApplicationController
include ApplicationHelper
before_filter :authenticate_user!
respond_to :html, :mobile, :json
respond_to :html,
:mobile,
:json
def create
if target
@like = current_user.build_like(:target => target)
if @like.save
Rails.logger.info("event=create type=like user=#{current_user.diaspora_handle} status=success like=#{@like.id}")
Postzord::Dispatcher.build(current_user, @like).post
@like = current_user.like!(target) if target
if @like
respond_to do |format|
format.html { render :nothing => true, :status => 201 }
format.mobile { redirect_to post_path(@like.post_id) }
format.json{ render :json => @like.parent.as_api_response(:backbone), :status => 201 }
end
else
render :nothing => true, :status => 422
format.json { render :json => @like.parent.as_api_response(:backbone), :status => 201 }
end
else
render :nothing => true, :status => 422
@ -30,7 +25,9 @@ class LikesController < ApplicationController
end
def destroy
if @like = Like.where(:id => params[:id], :author_id => current_user.person.id).first
@like = Like.where(:id => params[:id], :author_id => current_user.person.id).first
if @like
current_user.retract(@like)
respond_to do |format|
format.any { }
@ -47,7 +44,7 @@ class LikesController < ApplicationController
def index
if target
@likes = target.likes.includes(:author => :profile)
@people = @likes.map{|x| x.author}
@people = @likes.map(&:author)
respond_to do |format|
format.all{ render :layout => false }

View file

@ -294,6 +294,16 @@ class User < ActiveRecord::Base
end
end
def like!(target, opts={})
like = build_like(opts.merge!(:target => target, :positive => true))
if like.save
dispatch_post(like)
like
else
false
end
end
def build_relayable(model, options = {})
r = model.new(options.merge(:author_id => self.person.id))
r.set_guid

View file

@ -95,8 +95,8 @@ describe StreamsController do
it "generates a jasmine fixture with a post that has been liked", :fixture => true do
message = alice.post(:status_message, :text => "hello "*800, :to => @alices_aspect_2.id)
alice.build_like(:positive => true, :target => message).save
bob.build_like(:positive => true, :target => message).save
alice.like!(message)
bob.like!(message)
get :aspects
save_fixture(html_for("body"), "aspects_index_with_a_post_with_likes")

View file

@ -47,7 +47,7 @@ TXT
posts << alice.post(:status_message, :text => "you're gonna love this.", :to => alice.aspects.first.id)
end
Timecop.travel time += 1.minute
alice.like(1, :target => posts.last)
alice.like!(posts.last)
end
end

View file

@ -39,7 +39,7 @@ describe LikesController do
context "on a post from a contact" do
before do
@target = bob.post :status_message, :text => "AWESOME", :to => @bobs_aspect.id
@target = bob.post(:status_message, :text => "AWESOME", :to => @bobs_aspect.id)
@target = bob.comment!(@target, "hey") if class_const == Comment
end
@ -54,7 +54,7 @@ describe LikesController do
end
it "doesn't post multiple times" do
alice.like(1, :target => @target)
alice.like!(@target)
post :create, dislike_hash
response.code.should == '422'
end
@ -67,7 +67,7 @@ describe LikesController do
end
it "doesn't post" do
alice.should_not_receive(:like)
alice.should_not_receive(:like!)
post :create, like_hash
response.code.should == '422'
end
@ -92,9 +92,7 @@ describe LikesController do
end
it 'returns an array of likes for a post' do
like = bob.build_like(:positive => true, :target => @message)
like.save!
like = bob.like!(@message)
get :index, id_field => @message.id
assigns[:likes].map(&:id).should == @message.likes.map(&:id)
end
@ -109,8 +107,7 @@ describe LikesController do
before do
@message = bob.post(:status_message, :text => "hey", :to => @alices_aspect.id)
@message = bob.comment!(@message, "hey") if class_const == Comment
@like = alice.build_like(:positive => true, :target => @message)
@like.save
@like = alice.like!(@message)
end
it 'lets a user destroy their like' do
@ -121,8 +118,7 @@ describe LikesController do
end
it 'does not let a user destroy other likes' do
like2 = eve.build_like(:positive => true, :target => @message)
like2.save
like2 = eve.like!(@message)
expect {
delete :destroy, :format => :json, id_field => like2.target_id, :id => like2.id

View file

@ -114,7 +114,7 @@ describe PhotosController do
sm = bob.post(:status_message, :text => 'parent post', :to => 'all')
@bobs_photo.status_message_guid = sm.guid
@bobs_photo.save!
alice.like(1, :target => @bobs_photo.status_message)
alice.like!(@bobs_photo.status_message)
get :show, :id => @bobs_photo.id
response.should be_success
end

View file

@ -16,7 +16,7 @@ describe 'deleteing your account' do
@aspect_vis = AspectVisibility.where(:aspect_id => @bob2.aspects.map(&:id))
#objects on post
@bob2.like(true, :target => @alices_post)
@bob2.like!(@alices_post)
@bob2.comment!(@alices_post, "here are some thoughts on your post")
#conversations

View file

@ -7,77 +7,56 @@ require File.join(Rails.root, "spec", "shared_behaviors", "relayable")
describe Like do
before do
@alices_aspect = alice.aspects.first
@bobs_aspect = bob.aspects.first
@bob = bob
@eve = eve
@status = bob.post(:status_message, :text => "hello", :to => @alices_aspect.id)
bobs_aspect = bob.aspects.first
@status = bob.post(:status_message, :text => "hello", :to => bobs_aspect.id)
end
it 'has a valid factory' do
Factory(:like).should be_valid
end
describe 'User#like' do
it "should be able to like on one's own status" do
alice.like(1, :target => @status)
@status.reload.likes.first.positive.should == true
end
it "should be able to like on a contact's status" do
bob.like(0, :target => @status)
@status.reload.dislikes.first.positive.should == false
end
it "does not allow multiple likes" do
lambda {
alice.like(1, :target => @status)
alice.like(0, :target => @status)
}.should raise_error
end
end
describe '#notification_type' do
before do
@like = @alice.like(1, :target => @status)
@like = alice.like!(@status)
end
it 'should be notifications liked if you are the post owner' do
@like.notification_type(@bob, @alice.person).should be Notifications::Liked
@like.notification_type(bob, alice.person).should be Notifications::Liked
end
it 'should not notify you if you are the like-r' do
@like.notification_type(@alice, @alice.person).should be_nil
@like.notification_type(alice, alice.person).should be_nil
end
it 'should not notify you if you did not create the post' do
@like.notification_type(@eve, @alice.person).should be_nil
@like.notification_type(eve, alice.person).should be_nil
end
end
describe 'counter cache' do
it 'increments the counter cache on its post' do
lambda {
@alice.like(1, :target => @status)
alice.like!(@status)
}.should change{ @status.reload.likes_count }.by(1)
end
it 'increments the counter cache on its comment' do
comment = Factory(:comment, :post => @status)
lambda {
@alice.like(1, :target => comment)
alice.like!(comment)
}.should change{ comment.reload.likes_count }.by(1)
end
end
describe 'xml' do
before do
alices_aspect = alice.aspects.first
@liker = Factory(:user)
@liker_aspect = @liker.aspects.create(:name => "dummies")
connect_users(alice, @alices_aspect, @liker, @liker_aspect)
@post = alice.post :status_message, :text => "huhu", :to => @alices_aspect.id
@like = @liker.like 0, :target => @post
connect_users(alice, alices_aspect, @liker, @liker_aspect)
@post = alice.post(:status_message, :text => "huhu", :to => alices_aspect.id)
@like = @liker.like!(@post)
@xml = @like.to_xml.to_s
end
it 'serializes the sender handle' do
@ -105,14 +84,14 @@ describe Like do
@remote_parent = Factory(:status_message, :author => @remote_raphael)
@local_parent = @local_luke.post :status_message, :text => "foobar", :to => @local_luke.aspects.first
@object_by_parent_author = @local_luke.like(1, :target => @local_parent)
@object_by_recipient = @local_leia.build_like(:positive => 1, :target => @local_parent)
@object_by_parent_author = @local_luke.like!(@local_parent)
@object_by_recipient = @local_leia.like!(@local_parent)
@dup_object_by_parent_author = @object_by_parent_author.dup
@object_on_remote_parent = @local_luke.like(0, :target => @remote_parent)
@object_on_remote_parent = @local_luke.like!(@remote_parent)
end
let(:build_object) { alice.build_like(:target => @status, :positive => 1) }
let(:build_object) { alice.build_like(:target => @status, :positive => true) }
it_should_behave_like 'it is relayable'
end

View file

@ -218,7 +218,7 @@ describe Post do
describe 'Likeable#update_likes_counter' do
before do
@post = bob.post :status_message, :text => "hello", :to => 'all'
bob.like(1, :target => @post)
bob.like!(@post)
end
it 'does not update updated_at' do
old_time = Time.zone.now - 10000

View file

@ -689,11 +689,35 @@ describe User do
context 'likes' do
before do
alices_aspect = alice.aspects.where(:name => "generic").first
bobs_aspect = bob.aspects.where(:name => "generic").first
@bobs_aspect = bob.aspects.where(:name => "generic").first
@message = alice.post(:status_message, :text => "cool", :to => alices_aspect)
@message2 = bob.post(:status_message, :text => "uncool", :to => bobs_aspect)
@like = alice.like(true, :target => @message)
@like2 = bob.like(true, :target => @message)
@message2 = bob.post(:status_message, :text => "uncool", :to => @bobs_aspect)
@like = alice.like!(@message)
@like2 = bob.like!(@message)
end
describe 'User#like' do
before do
@status = bob.post(:status_message, :text => "hello", :to => @bobs_aspect.id)
end
it "should be able to like on one's own status" do
like = alice.like!(@status)
@status.reload.likes.first.should == like
end
it "should be able to like on a contact's status" do
like = bob.like!(@status)
@status.reload.likes.first.should == like
end
it "does not allow multiple likes" do
alice.like!(@status)
lambda {
alice.like!(@status)
}.should_not change(@status, :likes)
end
end
describe '#like_for' do

View file

@ -32,16 +32,6 @@ class User
end
end
def like(positive, options ={})
fantasy_resque do
l = build_like(options.merge(:positive => positive))
if l.save!
Postzord::Dispatcher.build(self, l).post
end
l
end
end
def post_at_time(time)
to_aspect = self.aspects.length == 1 ? self.aspects.first : self.aspects.where(:name => "generic")
p = self.post(:status_message, :text => 'hi', :to => to_aspect)