##// END OF EJS Templates
Enable the watching of news (#2549)....
Jean-Philippe Lang -
r12591:1ad33134d300
parent child
Show More
@@ -90,7 +90,13 class WatchersController < ApplicationController
90 90 klass = Object.const_get(params[:object_type].camelcase) rescue nil
91 91 if klass && klass.respond_to?('watched_by')
92 92 @watchables = klass.where(:id => Array.wrap(params[:object_id])).all
93 raise Unauthorized if @watchables.any? {|w| w.respond_to?(:visible?) && !w.visible?}
93 raise Unauthorized if @watchables.any? {|w|
94 if w.respond_to?(:visible?)
95 !w.visible?
96 elsif w.respond_to?(:project) && w.project
97 !w.project.visible?
98 end
99 }
94 100 end
95 101 render_404 unless @watchables.present?
96 102 end
@@ -17,6 +17,7
17 17
18 18 class EnabledModule < ActiveRecord::Base
19 19 belongs_to :project
20 acts_as_watchable
20 21
21 22 validates_presence_of :name
22 23 validates_uniqueness_of :name, :scope => :project_id
@@ -158,6 +158,7 class Mailer < ActionMailer::Base
158 158 @news = news
159 159 @news_url = url_for(:controller => 'news', :action => 'show', :id => news)
160 160 mail :to => news.recipients,
161 :cc => news.cc_for_added_news,
161 162 :subject => "[#{news.project.name}] #{l(:label_news)}: #{news.title}"
162 163 end
163 164
@@ -54,6 +54,18 class News < ActiveRecord::Base
54 54 project.users.select {|user| user.notify_about?(self)}.map(&:mail)
55 55 end
56 56
57 # Returns the email addresses that should be cc'd when a new news is added
58 def cc_for_added_news
59 cc = []
60 if m = project.enabled_module('news')
61 cc = m.notified_watchers
62 unless project.is_public?
63 cc = cc.select {|user| project.users.include?(user)}
64 end
65 end
66 cc.map(&:mail)
67 end
68
57 69 # returns latest news for projects visible by user
58 70 def self.latest(user = User.current, count = 5)
59 71 visible(user).includes([:author, :project]).order("#{News.table_name}.created_on DESC").limit(count).all
@@ -622,9 +622,16 class Project < ActiveRecord::Base
622 622 end
623 623 end
624 624
625 def module_enabled?(module_name)
626 module_name = module_name.to_s
627 enabled_modules.detect {|m| m.name == module_name}
625 # Return the enabled module with the given name
626 # or nil if the module is not enabled for the project
627 def enabled_module(name)
628 name = name.to_s
629 enabled_modules.detect {|m| m.name == name}
630 end
631
632 # Return true if the module with the given name is enabled
633 def module_enabled?(name)
634 enabled_module(name).present?
628 635 end
629 636
630 637 def enabled_module_names=(module_names)
@@ -3,6 +3,7
3 3 new_project_news_path(@project),
4 4 :class => 'icon icon-add',
5 5 :onclick => 'showAndScrollTo("add-news", "news_title"); return false;') if @project && User.current.allowed_to?(:manage_news, @project) %>
6 <%= watcher_link(@project.enabled_module('news'), User.current) %>
6 7 </div>
7 8
8 9 <div id="add-news" style="display:none;">
@@ -56,6 +56,27 class WatchersControllerTest < ActionController::TestCase
56 56 assert Issue.find(3).watched_by?(User.find(3))
57 57 end
58 58
59 def test_watch_a_news_module_should_add_watcher
60 @request.session[:user_id] = 7
61 assert_not_nil m = Project.find(1).enabled_module('news')
62
63 assert_difference 'Watcher.count' do
64 xhr :post, :watch, :object_type => 'enabled_module', :object_id => m.id.to_s
65 assert_response :success
66 end
67 assert m.reload.watched_by?(User.find(7))
68 end
69
70 def test_watch_a_private_news_module_without_permission_should_fail
71 @request.session[:user_id] = 7
72 assert_not_nil m = Project.find(2).enabled_module('news')
73
74 assert_no_difference 'Watcher.count' do
75 xhr :post, :watch, :object_type => 'enabled_module', :object_id => m.id.to_s
76 assert_response 403
77 end
78 end
79
59 80 def test_watch_should_be_denied_without_permission
60 81 Role.find(2).remove_permission! :view_issues
61 82 @request.session[:user_id] = 3
@@ -467,6 +467,17 class MailerTest < ActiveSupport::TestCase
467 467 end
468 468 end
469 469
470 def test_news_added_should_notify_project_news_watchers
471 user1 = User.generate!
472 user2 = User.generate!
473 news = News.first
474 news.project.enabled_module('news').add_watcher(user1)
475
476 Mailer.news_added(news).deliver
477 assert_include user1.mail, last_email.bcc
478 assert_not_include user2.mail, last_email.bcc
479 end
480
470 481 def test_news_comment_added
471 482 comment = Comment.find(2)
472 483 valid_languages.each do |lang|
General Comments 0
You need to be logged in to leave comments. Login now