OSDN Git Service

Added watchers for message boards (watchers controller modified to support any watcha...
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Sun, 13 May 2007 19:43:35 +0000 (19:43 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Sun, 13 May 2007 19:43:35 +0000 (19:43 +0000)
No notification yet when a new message is posted.

git-svn-id: http://redmine.rubyforge.org/svn/trunk@530 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/controllers/boards_controller.rb
app/controllers/issues_controller.rb
app/controllers/watchers_controller.rb
app/helpers/watchers_helper.rb
app/models/board.rb
app/views/boards/show.rhtml
app/views/issues/show.rhtml

index b3be6b7..0ad2645 100644 (file)
@@ -25,6 +25,8 @@ class BoardsController < ApplicationController
   include MessagesHelper
   helper :sort
   include SortHelper
+  helper :watchers
+  include WatchersHelper
  
   def index
     @boards = @project.boards
index cec126d..0a8d19a 100644 (file)
@@ -27,6 +27,8 @@ class IssuesController < ApplicationController
   include IfpdfHelper
   helper :issue_relations
   include IssueRelationsHelper
+  helper :watchers
+  include WatchersHelper
 
   def show
     @status_options = @issue.status.find_new_statuses_allowed_to(logged_in_user.role_for_project(@project), @issue.tracker) if logged_in_user
index 09ec5bc..f617a5b 100644 (file)
@@ -20,19 +20,30 @@ class WatchersController < ApplicationController
   before_filter :require_login, :find_project, :check_project_privacy
   
   def add
-    @issue.add_watcher(logged_in_user)
-    redirect_to :controller => 'issues', :action => 'show', :id => @issue
+    user = logged_in_user
+    @watched.add_watcher(user)
+    respond_to do |format|
+      format.html { render :text => 'Watcher added.', :layout => true }
+      format.js { render(:update) {|page| page.replace_html 'watcher', watcher_link(@watched, user)} }
+    end
   end
   
   def remove
-    @issue.remove_watcher(logged_in_user)
-    redirect_to :controller => 'issues', :action => 'show', :id => @issue
+    user = logged_in_user
+    @watched.remove_watcher(user)
+    respond_to do |format|
+      format.html { render :text => 'Watcher removed.', :layout => true }
+      format.js { render(:update) {|page| page.replace_html 'watcher', watcher_link(@watched, user)} }
+    end
   end
 
 private
-
   def find_project
-    @issue = Issue.find(params[:issue_id])
-    @project = @issue.project
+    klass = Object.const_get(params[:object_type].camelcase)
+    return false unless klass.respond_to?('watched_by')
+    @watched = klass.find(params[:object_id])
+    @project = @watched.project
+  rescue
+    render_404
   end
 end
index 23f7676..87b3810 100644 (file)
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
 module WatchersHelper
+  def watcher_tag(object, user)
+    content_tag("span", watcher_link(object, user), :id => 'watcher')
+  end
+  
+  def watcher_link(object, user)
+    return '' unless user && object.respond_to?('watched_by?')
+    watched = object.watched_by?(user)
+    url = {:controller => 'watchers',
+           :action => (watched ? 'remove' : 'add'),
+           :object_type => object.class.to_s.underscore,
+           :object_id => object.id}           
+    link_to_remote((watched ? l(:button_unwatch) : l(:button_watch)),
+                   {:url => url},
+                   :href => url_for(url),
+                   :class => (watched ? 'icon icon-fav' : 'icon icon-fav-off'))
+  
+  end
 end
index a6ea22f..26e2004 100644 (file)
@@ -21,6 +21,7 @@ class Board < ActiveRecord::Base
   has_many :messages, :dependent => :delete_all, :order => "#{Message.table_name}.created_on DESC"
   belongs_to :last_message, :class_name => 'Message', :foreign_key => :last_message_id
   acts_as_list :scope => :project_id
+  acts_as_watchable
   
   validates_presence_of :name, :description
   validates_length_of :name, :maximum => 30
index 13a0560..cb38cdb 100644 (file)
@@ -1,5 +1,6 @@
 <div class="contextual">
 <%= link_to l(:label_message_new), {:controller => 'messages', :action => 'new', :board_id => @board}, :class => "icon icon-add" %>
+<%= watcher_tag(@board, @logged_in_user) %>
 </div>
 
 <h2><%=h @board.name %></h2>
index d7d9bcf..1895e7c 100644 (file)
@@ -58,13 +58,7 @@ end %>
 <div class="contextual">
 <%= link_to_if_authorized l(:button_edit), {:controller => 'issues', :action => 'edit', :id => @issue}, :class => 'icon icon-edit' %>
 <%= link_to_if_authorized l(:button_log_time), {:controller => 'timelog', :action => 'edit', :issue_id => @issue}, :class => 'icon icon-time' %>
-<% if @logged_in_user %>
-  <% if @issue.watched_by?(@logged_in_user) %>
-<%= link_to l(:button_unwatch), {:controller => 'watchers', :action => 'remove', :issue_id => @issue}, :class => 'icon icon-fav' %>
-  <% else %>
-<%= link_to l(:button_watch), {:controller => 'watchers', :action => 'add', :issue_id => @issue}, :class => 'icon icon-fav-off' %>
-  <% end %>
-<% end %>
+<%= watcher_tag(@issue, @logged_in_user) %>
 <%= link_to_if_authorized l(:button_move), {:controller => 'projects', :action => 'move_issues', :id => @project, "issue_ids[]" => @issue.id }, :class => 'icon icon-move' %>
 <%= link_to_if_authorized l(:button_delete), {:controller => 'issues', :action => 'destroy', :id => @issue}, :confirm => l(:text_are_you_sure), :method => :post, :class => 'icon icon-del' %>
 </div>