Home

parolkar edited this page Sep 11, 2011 · 3 revisions

Simple Pfeed Integration

Note: updated version of this document for Rails 3.x is here

In order to use pfeed you need to have rails app with few sample models and associations. Hence we first create the app and then integrate the plugin, (if you are in hurry, you can also grab the sample app here )


 $ rails mysample
 $ cd mysample
 $ script/generate model User login:string   first_name:string last_name:string
 $ script/generate model Company user_id:integer name:string address:string
 $ script/generate model Friend  user_id:integer friend_id:integer 

And fill the models


 # app/models/user.rb
 class User < ActiveRecord::Base
   has_many :friends
   has_one  :company
   def buy(x)
    puts "buying"
   end
   def sell(x)
    puts "selling"
   end

   def find_friends
     puts "finding friends"
   end  
 end

 #app/models/friend.rb
 class Friend < ActiveRecord::Base
   belongs_to :user
 end

 #app/models/company.rb
 class Company < ActiveRecord::Base
   belongs_to :user
 end

now grab the plugin


 $ script/plugin install git://github.com/parolkar/pfeed.git

setup the plugin

 
 $ rake pfeed:setup
 Inflectionist plugin is required by pfeed, while  you dont seem to have it installed 
  Attempting to install...
 Initialized empty Git repository in /Users/parolkar/Work/Code/sample-pfeed/vendor/plugins/inflectionist/.git/
 remote: Counting objects: 22, done.
 remote: Compressing objects: 100% (20/20), done.
 remote: Total 22 (delta 3), reused 0 (delta 0)
 Unpacking objects: 100% (22/22), done.
 From git://github.com/parolkar/inflectionist
  * branch            HEAD       -> FETCH_HEAD
 Created : /db/migrate /20090427174511_create_pfeed_items.rb
 Created : /db/migrate/20090427174512_create_pfeed_deliveries.rb
 Running "rake db:migrate" for you...
 ....
 Congratulations for setting up the pfeed plugin! 

Now add these lines to bottom of your User model


 # app/models/user.rb
 class User < ActiveRecord::Base
   ...
   emits_pfeeds :on => [:buy,:sell,:find_friends,:update_attribute] , :for => [:itself ,:all_in_its_class, :friends]
   receives_pfeed

 end

Generate a controller action which displays the feed


 $ script/generate  controller profile feed
 
 class ProfileController < ApplicationController
   def feed
     render :partial => "pfeeds/pfeed", :collection => current_user.pfeed_inbox
   end

   private
   def current_user # stub
     User.first 
   end  
 end

Perform some operations


$ script/console 

>> u1 = User.new(:login => 'parolkar' , :first_name => 'Abhishek', :last_name => 'Parolkar')
=> #<User id: nil, login: "parolkar", first_name: "Abhishek", last_name: "Parolkar", created_at: nil, updated_at: nil>
>> u2 = User.new(:login => 'chad' , :first_name => 'Chad', :last_name => 'Fowler')
=> #<User id: nil, login: "chad", first_name: "Chad", last_name: "Fowler", created_at: nil, updated_at: nil>
>> u1.save
>> u2.save
>> u1.buy(10)
buying
>> u1.sell(5)
selling
>> list = u2.find_friends
finding friends
>> u2.update_attribute(:last_name, "Flower")
=> true
>> u2.buy(1)
buying

Start your server and navigate to http://localhost:3000/profile/feed
You should see

Abhishek bought about 2 minutes ago
Abhishek sold about 2 minutes ago
Chad found friends about 2 minutes ago
Chad updated attribute Last name about 1 minute ago
Chad bought about 1 minute ago