0

I am using socialization gem and public_activity gem in a rails 4 app.

I have everything working pretty much how I want it, except I cannot figure out how to delete activity records once the item it refers to is deleted.

http://swatinakshtra.wordpress.com/2013/11/24/public_activity-gem/

This blog post mentions you can delete these with a before_destroy method in the controller of the object being deleted, but I dont have access to that controller since its part of the socializer gem.

What is the proper way to achieve this? I guess I'm looking for some kind of 'cascade on delete' option.

fidato
  • 719
  • 5
  • 22
Chris Valentine
  • 1,557
  • 1
  • 19
  • 36

2 Answers2

2

You can extend the models from the Socializer gem and add an :after_destroy method in the model of the object the activity i referring to.

I haven't worked with Socializer but did something similar wit the Acts_as_taggable_on gem. In my initializers, I have added a acts_as_taggable_on_extension.rb with following content:

module ActsAsTaggableOn
  class Tagging
  include PublicActivity::Common
  after_destroy :delete_tag_activity

  def delete_tag_activity
      activity = PublicActivity::Activity.where('trackable_type = ? AND trackable_id = ?',  'ActsAsTaggableOn::Tagging',self.id).first
    if activity
      activity.destroy
    end
  end

  end
end
NicoFerna
  • 603
  • 3
  • 11
0

I have temporarily resolved this by adding

PublicActivity::Activity.where('trackable_type = ? and trackable_id not in (select id from likes)', ["Socialization::ActiveRecordStores::Like"]).delete_all

to a procedure that runs just after my unlike, and a similar one after unfollow. It does the trick temporarily.

I hope someone has a better answer with a more automated solution.

Chris Valentine
  • 1,557
  • 1
  • 19
  • 36