-
github.com/benoitgoyette/lifestreamable
Lifestreamable is a rails gem that allows social network life lifestream operations. A lifestream is a series of events that occured and that are related to an owner. It has been designed to collect data upfront in model observers to minimize the number of request done at display time. the goal being that if the lifestream dislays several types of data over several different models, then only a single query will be run to get all the data to display instead of querying all data for each model. this radiaclly cuts down on display time. This is a port to a gem of the lifestream libraries that have been designed for the sports social network legrandclub.rds.ca
TODO:
-
add support for pagination.
-
add support for delayed jobs.
-
add support for memcached
-
port to rails 3.0
-
write the tests
-
complete the doc on which options are available for lifestreamable and lifestreamed
-
make sure that syntax like user.posts << Post.create(…) triggers the observers correctly.
Ths lifestream modules is made up of 2 mixins, the lifestreamable, and lifestreamed modules. The lifestreamed module is included for models that own events, while the lifestreamable module is included on each model that triggers the event.
for example, a user can write posts and comments, we want to report in the user’s lifestream that the user has written posts and comments.
defining the owner class
class User < ActiveRecord::Base lifestreamed :order=>'id asc' # <= overrides the normal order which is 'id desc' end
defining the event classes
class Post < ActiveRecord::Base belongs_to :user has_many :comments lifestreamable :on=>[:create, :update, :destroy], data=>:get_data, :owner=>:user def get_data # this method must return a data structure that is serializable by YAML { :user=>{:firstname=>self.user.firstname, :lastname=>self.user.lastname}, :post=>{:title=>self.title} } end end class Comment < ActiveRecord::Base belongs_to :user belongs_to :post # another way to get the data is through a Proc lifestreamable :on=>[:create, :update, :destroy], :owner=>:user, data=> lambda {|model| { :user=>{:firstname=>model.user.firstname, :lastname=>model.user.lastname}, :post=>{:title=>model.post.title}, :comment=>{:body=>model.body} } } end
Whenever a new post is created, it will create a new entry in the lifestream model. To get the lifestream from the owner:
user=User.first # get the lifestream for the user lifestream = user.lifestream #=> returns an array of Lifestreamable::Lifesteam model instances #get the data that was stored data = lifestream.first.object_data
see the module Lifestreamable for all the details.
-
ActiveRecord
-
YAML
-
sudo gem install lifestreamable
-
script/generate lifestreamable_migration
(The MIT License)
Copyright © 2010 Benoit Goyette
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.