已经实现关注 取消关注 是否关注 关注我的某个类对象(这里要Recue)

This commit is contained in:
ZengTao 2016-06-22 16:15:59 +08:00
parent fb083199c9
commit f5228ee3ba
4 changed files with 58 additions and 17 deletions

Binary file not shown.

View File

@ -1,9 +1,14 @@
require "acts_as_followable/version" require "acts_as_followable/version"
module ActsAsFollowable module ActsAsFollowable
extend ActiveSupport::Autoload
autoload :Followable, 'acts_as_followable/followable' autoload :Followable, 'acts_as_followable/followable'
# autoload :Follower, 'acts_as_followable/follower'
autoload :FollowableExt, 'acts_as_followable/followable_ext' autoload :FollowableExt, 'acts_as_followable/followable_ext'
require 'acts_as_followable/railtie' if defined?(Rails) && Rails::VERSION::MAJOR >= 3 # require 'acts_as_followable/railtie' if defined?(Rails) && Rails::VERSION::MAJOR >= 3
ActiveSupport.on_load(:active_record) do
include ActsAsFollowable::Followable
include ActsAsFollowable::Follower
end
end end

View File

@ -3,11 +3,11 @@ module ActsAsFollowable
def self.included(base) def self.included(base)
base.extend ClassMethods base.extend ClassMethods
base.send :include, InstanceMethods
end end
module ClassMethods module ClassMethods
def acts_as_followable #This means can be followed - 被关注 def acts_as_followable #This means can be followed - 被关注
include ActsAsFollowable::Followable::InstanceMethods
include ActsAsFollowable::FollowableExt include ActsAsFollowable::FollowableExt
has_many :followers, as: :followable, dependent: :destroy, class_name: 'Follow' has_many :followers, as: :followable, dependent: :destroy, class_name: 'Follow'
@ -21,15 +21,53 @@ module ActsAsFollowable
self.followers.count self.followers.count
end end
def followers(follower_type, options = {}) def followers_by_type(follower_type, options = {})
followers = follower_type.constantize. klass = follower_type.constantize
joins(:follows). ids = Follow.
where('follows.followable_id' => self.id, where('followable_id' => self.id,
'follows.followable_type' => class_name(self), 'followable_type' => class_name(self),
'follows.follower_type' => follower_type 'follower_type' => follower_type
) ).pluck('follower_id')
return followers return klass.find(ids)
end end
end end
end end
module Follower
module ClassMethods
def acts_as_follower
include ActsAsFollowable::FollowableExt
has_many :follows, as: :follower, dependent: :destroy, class_name: 'Follow'
end
end
def self.included(receiver)
receiver.extend ClassMethods
receiver.send :include, InstanceMethods
end
module InstanceMethods
# # 1:   关注某人
# # 2: 取消关注
# # 3: 是否关注
# # 4: 关注了某个 model 多少个 obj
def follow(obj)
self.follows.find_or_create_by(followable_id: obj.id, followable_type: class_name(obj)) if obj != self
end
def unfollow(obj)
get_follow_by_obj(obj).destroy
end
def follow?(obj)
!get_follow_by_obj(obj).blank?
end
private
def get_follow_by_obj(obj)
self.follows.find_by(followable_id: obj.id, followable_type: class_name(obj))
end
end
end
end end

View File

@ -3,13 +3,11 @@ require 'rails'
module ActsAsFollowable module ActsAsFollowable
class Railtie < Rails::Railtie class Railtie < Rails::Railtie
initializer "acts_as_followable.active_record" do |app| initializer "acts_as_followable.active_record" do |app|
ActiveSupport.on_load :active_record do ActiveSupport.on_load :active_record do
# include ActsAsFollowable::Follower
include ActsAsFollowable::Followable include ActsAsFollowable::Followable
include ActsAsFollowable::Follower
end end
end end
end end
end end