Fix participations for likes on comments in the backend
When liking a comment, the post also gets a participation, and if all likes/comments get removed again, the participation also gets removed again. The only thing still not working properly is the frontend, but that is already broken when unliking a post. So it shows an invalids state in the frontend when unliking the post/comment.
This commit is contained in:
parent
edfb603965
commit
df8275f000
4 changed files with 27 additions and 9 deletions
|
|
@ -44,7 +44,8 @@ app.models.LikeInteractions = Backbone.Model.extend({
|
||||||
var self = this;
|
var self = this;
|
||||||
this.userLike().destroy({
|
this.userLike().destroy({
|
||||||
success: function() {
|
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.post.set({participation: false});
|
||||||
self.trigger("change");
|
self.trigger("change");
|
||||||
self.set({"likes_count": self.get("likes_count") - 1});
|
self.set({"likes_count": self.get("likes_count") - 1});
|
||||||
|
|
|
||||||
|
|
@ -32,7 +32,8 @@ class Like < ApplicationRecord
|
||||||
|
|
||||||
after_destroy do
|
after_destroy do
|
||||||
self.parent.update_likes_counter
|
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?
|
participation.unparticipate! if participation.present?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,9 @@ module User::SocialActions
|
||||||
end
|
end
|
||||||
|
|
||||||
def like_comment!(target, opts={})
|
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
|
end
|
||||||
|
|
||||||
def participate_in_poll!(target, answer, opts={})
|
def participate_in_poll!(target, answer, opts={})
|
||||||
|
|
|
||||||
|
|
@ -12,20 +12,34 @@ describe Like, type: :model do
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "#destroy" do
|
describe "#destroy" do
|
||||||
before do
|
let!(:like) { alice.like!(status) }
|
||||||
@like = alice.like!(status)
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should delete a participation" do
|
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
|
end
|
||||||
|
|
||||||
it "should decrease count participation" do
|
it "should decrease count participation" do
|
||||||
alice.comment!(status, "Are you there?")
|
alice.comment!(status, "Are you there?")
|
||||||
@like.destroy
|
like.destroy
|
||||||
participations = Participation.where(target_id: @like.target_id, author_id: @like.author_id)
|
participations = Participation.where(target_id: status.id, author_id: like.author_id)
|
||||||
expect(participations.first.count).to eq(1)
|
expect(participations.first.count).to eq(1)
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
describe "counter cache" do
|
describe "counter cache" do
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue