4

I have a rails app that is working fine using acts_as_votable. The like button upvotes the post count, and then switches to an un-like button and this down votes the post count.

My issue is, that since I started using the Public Activity gem, I can't find a way to remove likes from the feed. I have used the following loop in the activities index view:

<% @activities.each do |activity| %>
    <p>
        <% if activity.trackable %>
            <%= link_to activity.owner.name, activity.owner %>
            <%= render_activity activity %>
        <% end %>
    </p>
<% end %>

When I delete a comment, the entire line in the activity feed of 'FOO added a comment on BAR' disappears. However, because the acts as votable gem actually creates a downvote rather than destroying the upvote, the line 'FOO liked BAR' still appears and would be subsequently followed by 'FOO unliked BAR'.

Does anybody know how I can locate the upvote by the current_user on a particular post and then destroy it?

Below is my controller code for like and unlike as it stands:

def like
  @ink.create_activity :like, owner: current_user
  @ink.upvote_by current_user

  redirect_to :back
end

def unlike
  @ink.downvote_by current_user

  redirect_to :back
end

Thanks

abbott567
  • 862
  • 6
  • 18

3 Answers3

4

I know this have been answered but the ideal and easiest answer would be using the official gem method which is unvote_by it works for both upvotes and downvotes.

Malek Zalfana
  • 318
  • 2
  • 11
2

It looks like what you want to do is remove the model's like notification when it is "unliked" - is that correct? All you have to do is find the relevant activity and destroy it. Because Activities are models just like any other, you can do this with destroy and/or destroy_all.

I'm not quite sure what your public_activity model looks like, so instead of giving a specific example I'll link you to this post on the mailing list for the gem, which shows examples of deleting public activity records.

You may also find it useful to delete the record by its trackable_id - for example:

@activity = PublicActivity::Activity.find_by(trackable_id: (params[:id]), trackable_type: controller_path.classify)
@activity.destroy

There's more information about how that works in this SO answer.

Community
  • 1
  • 1
element119
  • 7,475
  • 8
  • 51
  • 74
  • I've given you the bounty, because you pointed me in the right direction! I still had to do quite a bit of digging but I eventually managed to sort it! Thanks so much for taking the time to answer this. I will post the final solution to this puzzle as an answer to help anyone in future! – abbott567 Mar 25 '15 at 13:38
2

For anyone that stumbles across this in future, thanks to Element119 I was eventually able to pinpoint the current users likes on a particular post with a few variables and arel searches. I then destroyed the pinpointed likes.

def like
    @post.create_activity  :like, 
                           owner: current_user,
                           recipient: @post.user
    @post.upvote_by current_user

    redirect_to :back
  end



def unlike
    @post.downvote_by current_user

    @currentUserLikes = PublicActivity::Activity.where(trackable_id: @post.id, owner_id: current_user.id, key: "post.like")
    @currentUserLikes.destroy_all

    redirect_to :back
  end
abbott567
  • 862
  • 6
  • 18