Help you implement feeds / timeline in rails application.
Add this line to your application's Gemfile:
And then execute:
Or install it yourself as:
$ gem install my_feeds
eater and associate model
feeder publish feeds
eater receive publish
and associate model create a relation between feeder and eater
yes, like subscribe and publish
feed model is what feeder published and eater subscribed, you can customize it.
rails generate my_feeds
rails generate my_feeds:likes
use own associate model
you can also use your own associate model.
just make sure your model have a polymorphic field for feeder.
class YourCustomModel < ActiveRecord::Base include MyFeeds::Associate #make sure include this module init_feeds_polymorphic = :likeable #and set this, in this case is likeable, it should same as the polymorphic belongs_to #same as feeds_polymorphic belongs_to :likeable, polymorphic: true end # you should create a migration like below # in this case, this column name is likeable_identity, it depends on feeds_polymorphic #add_column :your_customs, :likeable_identity, :string, null: false
include MyFeeds::Feeder in your model which will as feed source
class User < ActiveRecord::Base ... include MyFeeds::Feeder ... end
User get a method
define_feed_event define your feed_event
class User < ActiveRecord::Base ... include MyFeeds::Feeder define_feed_event :hahahaha define_feed_event :kakakaka ... end #in you rails console user = User.create ... #user get a method hahahaha... #will create 3 feeds user.hahahaha user.hahahaha user.hahahaha #you can use source_feeds associate fetch your source feeds user.source_feeds.count #=> 3 #or scoped version user.hahahaha_feeds.count #=> 3 user.kakakaka_feeds.count #=> 0 #you can pass extra arguments to feed_event method, but must match feed model attributes user.hahahaha target_id: another_user.id, target_type: another_user.class user.source_feeds.last.target_id #=> another_user.id #you can also passed arguments with define_feed_event, then arguments will be default ... define_feed_event :hahahaha, target_type: User ... user.hahahaha target_id: another_user.id user.source_feeds.last.target_type #=> User
include MyFeeds::Eater in your model which will as feed receiver
class User < ActiveRecord::Base ... include MyFeeds::Eater has_many :likes #a eater must have associate with feeder def feeds #eater have method feeds_for, pass associate and return feeds feeds_for likes end ... end ##sample #create relation u = User.create like = u.likes.new like.likeable = another_user like.save like = u.likes.new like.likeable = Post.find ... like.save u.feeds #=> you get feeds ##feeds with scope?? class User < ActiveRecord::Base ... include MyFeeds::Eater has_many :likes #a eater must have associate with feeder def feeds #eater have method feeds_for, pass associate and return feeds feeds_for likes end def teams_feeds feeds_for likes.where(likeable_type: Team) end def users_posts_feeds feeds_for(likes.where likeable_type: User).where(target_type: Post) end ... end
- Fork it
- Create your feature branch (
git checkout -b my-new-feature)
- Commit your changes (
git commit -am 'Add some feature')
- Push to the branch (
git push origin my-new-feature)
- Create new Pull Request