]> source.dussan.org Git - redmine.git/commitdiff
Enable the watching of news (#2549).
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Sun, 9 Feb 2014 11:54:21 +0000 (11:54 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Sun, 9 Feb 2014 11:54:21 +0000 (11:54 +0000)
git-svn-id: http://svn.redmine.org/redmine/trunk@12866 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/controllers/watchers_controller.rb
app/models/enabled_module.rb
app/models/mailer.rb
app/models/news.rb
app/models/project.rb
app/views/news/index.html.erb
test/functional/watchers_controller_test.rb
test/unit/mailer_test.rb

index f9de4a0303478fdf3562c1e3f18f6a4ebc0210d0..0096d8ac1c603db9997c0da2e201214b963876a5 100644 (file)
@@ -90,7 +90,13 @@ class WatchersController < ApplicationController
     klass = Object.const_get(params[:object_type].camelcase) rescue nil
     if klass && klass.respond_to?('watched_by')
       @watchables = klass.where(:id => Array.wrap(params[:object_id])).all
-      raise Unauthorized if @watchables.any? {|w| w.respond_to?(:visible?) && !w.visible?}
+      raise Unauthorized if @watchables.any? {|w|
+        if w.respond_to?(:visible?)
+          !w.visible?
+        elsif w.respond_to?(:project) && w.project
+          !w.project.visible?
+        end
+      }
     end
     render_404 unless @watchables.present?
   end
index 91a750ec84a12ff08db93403dd34b82b23288fa9..1cc84aaa5363a09a0676763d68aa1e1485bda413 100644 (file)
@@ -17,6 +17,7 @@
 
 class EnabledModule < ActiveRecord::Base
   belongs_to :project
+  acts_as_watchable
 
   validates_presence_of :name
   validates_uniqueness_of :name, :scope => :project_id
index 052bb9aef7b2e02c8beb3230efd544a747cc8b73..bfc39c0b34f57566a98ea95811dba3cc1f27c9cd 100644 (file)
@@ -158,6 +158,7 @@ class Mailer < ActionMailer::Base
     @news = news
     @news_url = url_for(:controller => 'news', :action => 'show', :id => news)
     mail :to => news.recipients,
+      :cc => news.cc_for_added_news,
       :subject => "[#{news.project.name}] #{l(:label_news)}: #{news.title}"
   end
 
index d9b0f513ee5052d6dede82a8a9f210d020389b89..c1f6655a540f039b830866eebed9e143d72bbe4b 100644 (file)
@@ -54,6 +54,18 @@ class News < ActiveRecord::Base
     project.users.select {|user| user.notify_about?(self)}.map(&:mail)
   end
 
+  # Returns the email addresses that should be cc'd when a new news is added
+  def cc_for_added_news
+    cc = []
+    if m = project.enabled_module('news')
+      cc = m.notified_watchers
+      unless project.is_public?
+        cc = cc.select {|user| project.users.include?(user)}
+      end
+    end
+    cc.map(&:mail)
+  end
+
   # returns latest news for projects visible by user
   def self.latest(user = User.current, count = 5)
     visible(user).includes([:author, :project]).order("#{News.table_name}.created_on DESC").limit(count).all
index a25513baffd78359c417fa5f577442a460c59251..c5392d7b09dd6ce546cf9048c31b503b2c84a5a6 100644 (file)
@@ -622,9 +622,16 @@ class Project < ActiveRecord::Base
     end
   end
 
-  def module_enabled?(module_name)
-    module_name = module_name.to_s
-    enabled_modules.detect {|m| m.name == module_name}
+       # Return the enabled module with the given name
+       # or nil if the module is not enabled for the project
+  def enabled_module(name)
+    name = name.to_s
+    enabled_modules.detect {|m| m.name == name}
+  end
+
+       # Return true if the module with the given name is enabled
+  def module_enabled?(name)
+    enabled_module(name).present?
   end
 
   def enabled_module_names=(module_names)
index 38eecd262c16a97f0676a4d80138a7a3e9c72496..7fa75dc51596d00427b25230650e4220b43b2df0 100644 (file)
@@ -3,6 +3,7 @@
             new_project_news_path(@project),
             :class => 'icon icon-add',
             :onclick => 'showAndScrollTo("add-news", "news_title"); return false;') if @project && User.current.allowed_to?(:manage_news, @project) %>
+<%= watcher_link(@project.enabled_module('news'), User.current) %>
 </div>
 
 <div id="add-news" style="display:none;">
index 1faf4b06e0220703048bc4db864e65809aa698b4..8ec2aa112bb209d004092bbe34fc6116ad47c767 100644 (file)
@@ -56,6 +56,27 @@ class WatchersControllerTest < ActionController::TestCase
     assert Issue.find(3).watched_by?(User.find(3))
   end
 
+  def test_watch_a_news_module_should_add_watcher
+    @request.session[:user_id] = 7
+    assert_not_nil m = Project.find(1).enabled_module('news')
+
+    assert_difference 'Watcher.count' do
+      xhr :post, :watch, :object_type => 'enabled_module', :object_id => m.id.to_s
+      assert_response :success
+    end
+    assert m.reload.watched_by?(User.find(7))
+  end
+
+  def test_watch_a_private_news_module_without_permission_should_fail
+    @request.session[:user_id] = 7
+    assert_not_nil m = Project.find(2).enabled_module('news')
+
+    assert_no_difference 'Watcher.count' do
+      xhr :post, :watch, :object_type => 'enabled_module', :object_id => m.id.to_s
+      assert_response 403
+    end
+  end
+
   def test_watch_should_be_denied_without_permission
     Role.find(2).remove_permission! :view_issues
     @request.session[:user_id] = 3
index b5f4086f7a2b518e3e915efd297745a228b604fc..5c155e6a5427e9dbfe3d6d2654633b0f4e7e33fc 100644 (file)
@@ -467,6 +467,17 @@ class MailerTest < ActiveSupport::TestCase
     end
   end
 
+  def test_news_added_should_notify_project_news_watchers
+    user1 = User.generate!
+    user2 = User.generate!
+    news = News.first
+    news.project.enabled_module('news').add_watcher(user1)
+
+    Mailer.news_added(news).deliver
+    assert_include user1.mail, last_email.bcc
+    assert_not_include user2.mail, last_email.bcc
+  end
+
   def test_news_comment_added
     comment = Comment.find(2)
     valid_languages.each do |lang|