From fbd38b4a5352c5afff03f2672987fe21ec37228b Mon Sep 17 00:00:00 2001 From: z9hang Date: Wed, 10 Dec 2014 14:33:05 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=85=B3=E6=B3=A8=E5=88=97=E8=A1=A8?= =?UTF-8?q?=E4=B8=8E=E5=85=B3=E6=B3=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/watchers_controller.rb | 1 + app/services/service.rb | 51 ++++++++++++++++++++++++++ app/services/users_service.rb | 7 ++++ 3 files changed, 59 insertions(+) create mode 100644 app/services/service.rb diff --git a/app/controllers/watchers_controller.rb b/app/controllers/watchers_controller.rb index 50bb2ccd8..6c61f1970 100644 --- a/app/controllers/watchers_controller.rb +++ b/app/controllers/watchers_controller.rb @@ -162,4 +162,5 @@ class WatchersController < ApplicationController format.js { render :partial => 'set_watcher', :locals => {:user => user, :watched => watchables} } end end + end diff --git a/app/services/service.rb b/app/services/service.rb new file mode 100644 index 000000000..c4eb4bc1c --- /dev/null +++ b/app/services/service.rb @@ -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 \ No newline at end of file diff --git a/app/services/users_service.rb b/app/services/users_service.rb index cd955e18d..da1d61d36 100644 --- a/app/services/users_service.rb +++ b/app/services/users_service.rb @@ -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 From 41fa6f02f17888e3645cd79a87175bcc30f28d7b Mon Sep 17 00:00:00 2001 From: z9hang Date: Wed, 10 Dec 2014 15:07:41 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=85=B3=E6=B3=A8=E5=88=97=E8=A1=A8?= =?UTF-8?q?=E3=80=81=E5=85=B3=E6=B3=A8=E4=B8=8E=E5=8F=96=E6=B6=88=E5=85=B3?= =?UTF-8?q?=E6=B3=A8=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/watchers_controller.rb | 30 +++++++++++++++++++++++--- app/services/service.rb | 14 ++++++++---- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/app/controllers/watchers_controller.rb b/app/controllers/watchers_controller.rb index 6c61f1970..096e44f06 100644 --- a/app/controllers/watchers_controller.rb +++ b/app/controllers/watchers_controller.rb @@ -15,13 +15,37 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. class WatchersController < ApplicationController - before_filter :require_login, :find_watchables, :only => [:watch, :unwatch] + before_filter :require_login#, :find_watchables, :only => [:watch, :unwatch] 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 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 def join diff --git a/app/services/service.rb b/app/services/service.rb index c4eb4bc1c..413d9bfb8 100644 --- a/app/services/service.rb +++ b/app/services/service.rb @@ -1,15 +1,20 @@ 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, User.current, true) + set_watcher(@watchables, @current_user, true) end def unwatch params - find_watchables params - set_watcher(@watchables, User.current, false) + @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 @@ -47,5 +52,6 @@ class Service end end end + watchables end end \ No newline at end of file