This commit is contained in:
guange 2014-12-10 15:11:26 +08:00
commit b9156b271e
3 changed files with 92 additions and 3 deletions

View File

@ -15,13 +15,37 @@
# along with this program; if not, write to the Free Software # along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
class WatchersController < ApplicationController class WatchersController < ApplicationController
before_filter :require_login, :find_watchables, :only => [:watch, :unwatch] before_filter :require_login#, :find_watchables, :only => [:watch, :unwatch]
def watch def watch
set_watcher(@watchables, User.current, true) s = Service.new
watchables = s.watch params.merge(:current_user_id => User.current.id)
respond_to do |format|
format.html { redirect_to_referer_or {render :text => (true ? 'Watcher added.' : 'Watcher removed.'), :layout => true}}
format.js { render :partial => 'set_watcher', :locals => {:user => User.current, :watched => watchables} }
end
rescue Exception => e
if e.message == "404"
render_404
else
raise e
end
#set_watcher(@watchables, User.current, true)
end end
def unwatch def unwatch
set_watcher(@watchables, User.current, false) s = Service.new
watchables = s.unwatch params.merge(:current_user_id => User.current.id)
respond_to do |format|
format.html { redirect_to_referer_or {render :text => (false ? 'Watcher added.' : 'Watcher removed.'), :layout => true}}
format.js { render :partial => 'set_watcher', :locals => {:user => User.current, :watched => watchables} }
end
rescue Exception => e
if e.message == "404"
render_404
else
raise e
end
#set_watcher(@watchables, User.current, false)
end end
def join def join
@ -162,4 +186,5 @@ class WatchersController < ApplicationController
format.js { render :partial => 'set_watcher', :locals => {:user => user, :watched => watchables} } format.js { render :partial => 'set_watcher', :locals => {:user => user, :watched => watchables} }
end end
end end
end end

57
app/services/service.rb Normal file
View File

@ -0,0 +1,57 @@
class Service
def watch params
@current_user = User.find(params[:current_user_id])
@watchables = find_watchables params
if @watchables.nil?
raise '404'
end
set_watcher(@watchables, @current_user, true)
end
def unwatch params
@current_user = User.find(params[:current_user_id])
@watchables = find_watchables params
if @watchables.nil?
raise '404'
end
set_watcher(@watchables, @current_user, 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
watchables
end
end

View File

@ -104,4 +104,11 @@ class UsersService
end end
@se @se
end end
#关注列表
def user_watcher params
@user = User.find(params[:id])
User.watched_by(@user.id)
end
end end