关注列表与关注
This commit is contained in:
parent
7efd2375ff
commit
fbd38b4a53
|
@ -162,4 +162,5 @@ class WatchersController < ApplicationController
|
|||
format.js { render :partial => 'set_watcher', :locals => {:user => user, :watched => watchables} }
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -0,0 +1,51 @@
|
|||
class Service
|
||||
def watch params
|
||||
@watchables = find_watchables params
|
||||
if @watchables.nil?
|
||||
|
||||
end
|
||||
set_watcher(@watchables, User.current, true)
|
||||
end
|
||||
|
||||
def unwatch params
|
||||
find_watchables params
|
||||
set_watcher(@watchables, User.current, false)
|
||||
end
|
||||
|
||||
def find_watchables params
|
||||
#根据参数获取关注对象的类型(user、project)
|
||||
klass = Object.const_get(params[:object_type].camelcase) rescue nil
|
||||
#判断获取的对象类型能否响应‘watched_by’方法
|
||||
if klass && klass.respond_to?('watched_by')
|
||||
@watchables = klass.find_all_by_id(Array.wrap(params[:object_id]))
|
||||
raise Unauthorized if @watchables.any? {|w| w.respond_to?(:visible?) && !w.visible?}
|
||||
end
|
||||
@watchables.present? ? @watchables : nil
|
||||
end
|
||||
|
||||
def set_watcher(watchables, user, watching)
|
||||
watchables.each do |watchable|
|
||||
watchable.set_watcher(user, watching)
|
||||
# @user = watchable # added by william
|
||||
if watching
|
||||
# 修改 user和project的状态
|
||||
if watchable.instance_of?(User)
|
||||
#写user_statuses表
|
||||
UserStatus.find_by_user_id(watchable.id).update_watchers_count(1)
|
||||
elsif watchable.instance_of?(Project)
|
||||
#写project_statuese表
|
||||
ProjectStatus.find_by_project_id(watchable.id).update_watchers_count(1)
|
||||
end
|
||||
else
|
||||
# 修改 user和project的状态
|
||||
if watchable.instance_of?(User)
|
||||
#写user_statuses表
|
||||
UserStatus.find_by_user_id(watchable.id).update_watchers_count(-1)
|
||||
elsif watchable.instance_of?(Project)
|
||||
#写project_statuese表 :project_status
|
||||
ProjectStatus.find_by_project_id(watchable.id).update_watchers_count(-1)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -107,4 +107,11 @@ class UsersService
|
|||
@se
|
||||
end
|
||||
end
|
||||
|
||||
#关注列表
|
||||
def user_watcher params
|
||||
@user = User.find(params[:id])
|
||||
User.watched_by(@user.id)
|
||||
end
|
||||
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue