-
Notifications
You must be signed in to change notification settings - Fork 14
Customizing the pfeed item
Ok, So you played with default pfeed items which create activity feed by watching the function calls and generating the activity sentences.
like this…
Abhishek bought about 2 minutes ago
Chad found friends about 2 minutes ago
Chad updated attribute Last name about 1 minute ago
But now you want to customize these feeds and make your own sentences and also skin them with CSS/jquery, yeah? This is where Pfeed plugin shines its ability by giving you full control on wrapping any complicated functionality around activity feed item. Here every pfeed item has a model and view ( the M&V of the MVC). Model can store custom attributes when activity feed is created and View renders the model’s data for you.
Let’s assume that , from previous example when u1.buy(10) is invoked, you want pfeed item model to know about the arguments passed , i.e 10 (Fixnum)
and view to render “Abhishek bought 10 items about 2 minutes ago”
Its just very simple,
create subdirectory “pfeeds” in models directory of your app.
create a model named Pfeeds::UserBought which inherits from PfeedItem
# app/models/pfeeds/user_bought.rb
class Pfeeds::UserBought < PfeedItem
def pack_data(method_name,method_name_in_past_tense,returned_result,*args_supplied_to_method,&block_supplied_to_method)
self.data = {} if ! self.data
hash_to_be_merged = {:no_of_items => args_supplied_to_method[0] }
self.data.merge! hash_to_be_merged
super
end
end
and then create view partial by first creating pfeeds subdirectory in app/views
<%# app/views/pfeeds/user_bought.html.erb >
< = user_bought.guess_identification(user_bought.originator) >
< = user_bought.data[:action_string] >
< = user_bought.data[:no_of_items] >
items
about < = time_ago_in_words(user_bought.createdat) %> ago
Thats it, and you are done! This way you can improve each feed item and bring in complex functionality as required.
Key points to remember:
- When you start customizing pfeed item as shown above , the older pfeed item (which has already been delivered) remains unchanged , only new activities will consider the subclass for added functionality.
- The name of subclass is always concatenation of originator class name and method name in past tense ,i.e UserBought , UserFoundFriends etc.
- You can treat these subclases as regular active record objects and write methods validations etc.
- You can write partials in any template engine conventions, HAML/SASS , xml, json etc
PS: if you are in hurry, download the sample app here