diff --git a/app/assets/javascripts/app/models/like_interactions.js b/app/assets/javascripts/app/models/like_interactions.js index c2df4c806..4642d663b 100644 --- a/app/assets/javascripts/app/models/like_interactions.js +++ b/app/assets/javascripts/app/models/like_interactions.js @@ -44,7 +44,8 @@ app.models.LikeInteractions = Backbone.Model.extend({ var self = this; this.userLike().destroy({ success: function() { - // TODO: If user unlikes a post and the last like of all comments, then set participation to false + // TODO: unlike always sets participation to false in the UI, even if there are more participations left + // in the backend (from manually participating, other likes or comments) self.post.set({participation: false}); self.trigger("change"); self.set({"likes_count": self.get("likes_count") - 1}); diff --git a/app/models/like.rb b/app/models/like.rb index 70f2745cf..2d3e51702 100644 --- a/app/models/like.rb +++ b/app/models/like.rb @@ -32,7 +32,8 @@ class Like < ApplicationRecord after_destroy do self.parent.update_likes_counter - participation = author.participations.find_by(target_id: target.id) + participation_target_id = parent.is_a?(Comment) ? parent.commentable.id : parent.id + participation = author.participations.find_by(target_id: participation_target_id) participation.unparticipate! if participation.present? end diff --git a/app/models/user/social_actions.rb b/app/models/user/social_actions.rb index 3ab60f137..e47ea3368 100644 --- a/app/models/user/social_actions.rb +++ b/app/models/user/social_actions.rb @@ -18,7 +18,9 @@ module User::SocialActions end def like_comment!(target, opts={}) - Like::Generator.new(self, target).create!(opts) + Like::Generator.new(self, target).create!(opts).tap do + update_or_create_participation!(target.commentable) + end end def participate_in_poll!(target, answer, opts={}) diff --git a/spec/models/like_spec.rb b/spec/models/like_spec.rb index d8e214eb5..487b9fce4 100644 --- a/spec/models/like_spec.rb +++ b/spec/models/like_spec.rb @@ -12,20 +12,34 @@ describe Like, type: :model do end describe "#destroy" do - before do - @like = alice.like!(status) - end + let!(:like) { alice.like!(status) } it "should delete a participation" do - expect { @like.destroy }.to change { Participation.count }.by(-1) + expect { like.destroy }.to change { Participation.count }.by(-1) end it "should decrease count participation" do alice.comment!(status, "Are you there?") - @like.destroy - participations = Participation.where(target_id: @like.target_id, author_id: @like.author_id) + like.destroy + participations = Participation.where(target_id: status.id, author_id: like.author_id) expect(participations.first.count).to eq(1) end + + context "on comment" do + let(:comment) { bob.comment!(status, "Are you there?") } + let!(:like) { alice.like_comment!(comment) } + + it "should delete a participation" do + expect { like.destroy }.to change { Participation.count }.by(-1) + end + + it "should decrease count participation" do + alice.comment!(status, "Yes, I am here!") + like.destroy + participations = Participation.where(target_id: status.id, author_id: like.author_id) + expect(participations.first.count).to eq(1) + end + end end describe "counter cache" do