Skip to content

Complex pfeed usage for activerecord associations

parolkar edited this page Sep 13, 2010 · 1 revision

Lets assume that you want to log activity on method calls related to active record associations, eg.

 class User < ActiveRecord::Base
  has_many :friends  
  has_one  :company    
 end

  user1.create_company({}) # should produce pfeed " User1 created company ..."
  user1.friends.push(Friend.new) # should produce pfeed  "User1 added friend ..."

In order to achieve this , you might use call backs on the association e.g


class User < ActiveRecord::Base
  has_many :friends  , :after_add => :log_addition_of_friend   
  has_one  :company  

  def log_addition_of_friend(obj)
     # just a stub to help in the logging of friends
  end

  emits_pfeeds :on => [:log_addition_of_friend] , :for =>  [:itself ,:all_in_its_class]
  emits_pfeeds :on => [:create_company], :for =>  [:itself ,:all_in_its_class]     
  receives_pfeed


end