Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 public_activity, destroy record

I am using the public_activity gem tracking if a user creates a post. Is there a way to destroy a public activity record, on deletion of the post, so that in the activities feed it doesn't show something like:

A post was deleted.

And instead just deletes that particular activity in the activities table

Thanks.

like image 367
swaroopsm Avatar asked Dec 26 '22 02:12

swaroopsm


2 Answers

I think this is what the OP was looking for / may have figured out, but didn't post in a solution.

Quick example scenario:

A user creates a comment, therefore a Public Activity Record (key: comment.create) is created for that comment.

Now, lets say the user deletes his comment.

There is still an activity (key: comment.create) stored in the activities table that relates to the original comment that just got deleted.

To delete that original activity all together when a user deletes their corresponding activity (key: comment.create). Simply do the following.

#comments_controller.rb or whatever class you are tracking
def destroy
  @comment = current_user.comments.find(params[:id])
  @activity = PublicActivity::Activity.find_by(trackable_id: (params[:id]), trackable_type: controller_path.classify)
  @activity.destroy
  @comment.destroy
end

Hope this helps someone.

like image 118
itsthewrongway Avatar answered Feb 27 '23 08:02

itsthewrongway


In your Post model you can tracked a details 0f deleted post so you can use it when you are displaying a notifications about post deletion. you can improve your notification "A post was deleted" for example " A post with content XYZ deleted at abc time format" for example your Post.rb having a field :content so in your Post.rb

class Post <  ActiveRecord::Base
  include PublicActivity::Model
tracked :params => {
      :content => proc {|controller, model| (model.content)}   
  }

and in your public_activity/post/destroy.html.haml you can access content p[:content] Or you can reject the activity record with :key => post.destroy for that in your notifications controller in action index class NotificationsController < ApplicationController

def index
    @activities = PublicActivity::Activity.order("created_at DESC").reject{|activity| (activity.key == "post.destroy" }

this will not notify post deletions details in notifications.

like image 33
priyanka Ukirde Avatar answered Feb 27 '23 07:02

priyanka Ukirde