Code Monkey home page Code Monkey logo

my_feeds's Introduction

MyFeeds

Help you implement feeds / timeline in rails application.

Installation

Add this line to your application's Gemfile:

gem 'my_feeds'

And then execute:

$ bundle

Or install it yourself as:

$ gem install my_feeds

Usage

before usage

my_feeds need feeder, 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

feed model is what feeder published and eater subscribed, you can customize it.

rails generate my_feeds

associate model

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.

and

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

feeder

include MyFeeds::Feeder in your model which will as feed source

class User < ActiveRecord::Base
  ...
  include MyFeeds::Feeder
  ...
end

now User get a method define_feed_event

use 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

eater

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

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

my_feeds's People

Contributors

jjyr avatar larryzhao avatar massongit avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

my_feeds's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.