Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using gem unread with public_activity

I'm not sure this is possible, but given the way you can inject a method into modules, is there a way to inject "acts_as_readable :on => :updated_at" into PublicActivity::Activity?

The issue is this: I want unread to also track the new activities the user has not yet seen, so I first tried it this way.

class Activity < PublicActivity::Activity
  acts_as_readable :on => :updated_at
end

class User < ActiveRecord::Base
  acts_as_reader
end

However I noticed in the record (DB table read_marks) when I do

Activity.mark_as_read! :all, :for => current_user

that the readable_type key is set to 'PublicActivity::Activity' instead of 'Activity'

This is because the Unread gem uses

user.read_marks.build(:readable_id => obj.id, :readable_type => self.base_class.name)

that base_class gives the class closest to ActiveRecord::Base What would be the best option?

1) inject somehow the 'acts_as_readable :on => :updated_at' directly into PublicActivity::Activity

2) fork PublicActivity and check if Unread gem is installed, if so add the line 'acts_as...'

3) fork the Unread gem and ... not sure what I would do here, the first ugly thing that comes to mind would be to check if a class begins with PublicActivity!

And just to define the issue: Since the record's (DB table read_marks) readable type is PublicActivity::Activity and not Activity, my

Activity.unread_by(current_user).count

never returns the correct count. Using

PublicActivity::Activity.unread...

would fail since the 'acts_as_readable :on => :updated_at' is in Activity and not PublicActivity::Activity.

UPDATE

I've tried to 'monkey-patch' the Public_Activity plugin but now I'm even more confused as to why it fails, it must be something dumb I'm doing. I added the following file. However the readable_type is still PublicActivity::Activity and my count remains the same after I do

PublicActivity::Activity.mark_as_read! :all, :for => current_user


# config/initializers/activity_unread.rb
PublicActivity::Activity.module_eval do

  acts_as_readable :on => :updated_at

end

UPDATE

AH! It was the readable_type field having a limit of only 20 that was truncating my class name. Now it works after I increased the size.

like image 454
Andrew Lank Avatar asked Oct 23 '22 07:10

Andrew Lank


1 Answers

reko

Not sure if you found the solution, but to get rid of the error (I am using Ruby 1.9.3 & Rails 3.2) change

PublicActivity::Activity.template = YAML.load_file("#{RAILS_ROOT}/config/pba.yml")

to

PublicActivity::Activity.template = YAML.load_file("#{Rails.root}/config/pba.yml")

I created a file called pba_setup and inserted the above line in the initializer folder. Now that cleared up the error when loading, but I still get a missing template error? Hope this helps!

like image 132
frgooall Avatar answered Oct 27 '22 00:10

frgooall