已经实现关注 取消关注 是否关注 关注我的某个类对象(这里要Recue)
This commit is contained in:
parent
fb083199c9
commit
f5228ee3ba
Binary file not shown.
|
@ -1,9 +1,14 @@
|
|||
require "acts_as_followable/version"
|
||||
|
||||
module ActsAsFollowable
|
||||
extend ActiveSupport::Autoload
|
||||
autoload :Followable, 'acts_as_followable/followable'
|
||||
# autoload :Follower, 'acts_as_followable/follower'
|
||||
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
|
||||
|
|
|
@ -3,11 +3,11 @@ module ActsAsFollowable
|
|||
|
||||
def self.included(base)
|
||||
base.extend ClassMethods
|
||||
base.send :include, InstanceMethods
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
def acts_as_followable #This means can be followed - 被关注
|
||||
include ActsAsFollowable::Followable::InstanceMethods
|
||||
include ActsAsFollowable::FollowableExt
|
||||
|
||||
has_many :followers, as: :followable, dependent: :destroy, class_name: 'Follow'
|
||||
|
@ -21,15 +21,53 @@ module ActsAsFollowable
|
|||
self.followers.count
|
||||
end
|
||||
|
||||
def followers(follower_type, options = {})
|
||||
followers = follower_type.constantize.
|
||||
joins(:follows).
|
||||
where('follows.followable_id' => self.id,
|
||||
'follows.followable_type' => class_name(self),
|
||||
'follows.follower_type' => follower_type
|
||||
)
|
||||
return followers
|
||||
def followers_by_type(follower_type, options = {})
|
||||
klass = follower_type.constantize
|
||||
ids = Follow.
|
||||
where('followable_id' => self.id,
|
||||
'followable_type' => class_name(self),
|
||||
'follower_type' => follower_type
|
||||
).pluck('follower_id')
|
||||
return klass.find(ids)
|
||||
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
|
||||
|
|
|
@ -3,13 +3,11 @@ require 'rails'
|
|||
|
||||
module ActsAsFollowable
|
||||
class Railtie < Rails::Railtie
|
||||
|
||||
initializer "acts_as_followable.active_record" do |app|
|
||||
ActiveSupport.on_load :active_record do
|
||||
# include ActsAsFollowable::Follower
|
||||
include ActsAsFollowable::Followable
|
||||
include ActsAsFollowable::Follower
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue