Sha256: a5c93300b1bf6cebbb79c0f4fcbb9517d4c57d5e828375e0359acb40f774a4e5

Contents?: true

Size: 1.05 KB

Versions: 1

Compression:

Stored size: 1.05 KB

Contents

module Mongoid
  module Follower
    extend ActiveSupport::Concern

    included do |base|
      base.has_many :followees, :class_name => 'Follow', :as => :followee, :dependent => :destroy
    end

    # follow a model
    #
    # Example:
    # >> @bonnie.follow(@clyde)
    def follow(model)
      model.followers.create!(:ff_type => self.class.name, :ff_id => self.id)

      self.followees.create!(:ff_type => model.class.name, :ff_id => model.id)
    end

    # unfollow a model
    #
    # Example:
    # >> @bonnie.unfollow(@clyde)
    def unfollow(model)
      model.followers.where(:ff_type => self.class.name, :ff_id => self.id).destroy

      self.followees.where(:ff_type => model.class.name, :ff_id => model.id).destroy
    end

    # follow a model
    #
    # Example:
    # >> @bonnie.follows?(@clyde)
    # => true
    def follows?(model)
      0 < self.followees.find(:all, conditions: {ff_id: model.id}).limit(1).count
    end

    # view all selfs followees
    #
    # Example:
    # >> @alec.followees
    def followees(model)
      # TODO
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mongoid_follow-0.0.2 lib/mongoid_follow/follower.rb