diff --git a/app/controllers/documents_controller.rb b/app/controllers/documents_controller.rb index 39906610..45c207ad 100644 --- a/app/controllers/documents_controller.rb +++ b/app/controllers/documents_controller.rb @@ -43,20 +43,33 @@ class DocumentsController < ApplicationController end def new - @document = @project.documents.build(params[:document]) - if request.post? and @document.save - attachments = Attachment.attach_files(@document, params[:attachments]) - render_attachment_warning_if_needed(@document) - flash[:notice] = l(:notice_successful_create) - redirect_to :action => 'index', :project_id => @project + @document = @project.documents.build(params[:document]) + if request.post? + if User.current.allowed_to?(:add_document_watchers, @project) && params[:document]['watcher_user_ids'].present? + @document.watcher_user_ids = params[:document]['watcher_user_ids'] + end + + if @document.save + attachments = Attachment.attach_files(@document, params[:attachments]) + render_attachment_warning_if_needed(@document) + flash[:notice] = l(:notice_successful_create) + redirect_to :action => 'index', :project_id => @project + end end end def edit @categories = DocumentCategory.all - if request.post? and @document.update_attributes(params[:document]) - flash[:notice] = l(:notice_successful_update) - redirect_to :action => 'show', :id => @document + + if request.post? + if User.current.allowed_to?(:add_document_watchers, @project) && params[:document]['watcher_user_ids'].present? + @document.watcher_user_ids = params[:document]['watcher_user_ids'] + end + + if @document.update_attributes(params[:document]) + flash[:notice] = l(:notice_successful_update) + redirect_to :action => 'show', :id => @document + end end end diff --git a/app/models/document.rb b/app/models/document.rb index 06df9720..b6c4af2c 100644 --- a/app/models/document.rb +++ b/app/models/document.rb @@ -24,6 +24,7 @@ class Document < ActiveRecord::Base end) acts_as_searchable :columns => ['title', "#{table_name}.description"], :include => :project + acts_as_watchable validates_presence_of :project, :title, :category validates_length_of :title, :maximum => 60 @@ -48,4 +49,10 @@ class Document < ActiveRecord::Base end @updated_on end + + def recipients + mails = super # from acts_as_event + mails += watcher_recipients + mails.uniq + end end diff --git a/app/views/documents/_form.rhtml b/app/views/documents/_form.rhtml index 37d67d1e..74d9248c 100644 --- a/app/views/documents/_form.rhtml +++ b/app/views/documents/_form.rhtml @@ -9,6 +9,15 @@

<%= text_area 'document', 'description', :cols => 60, :rows => 15, :class => 'wiki-edit' %>

+ +<% if User.current.allowed_to?(:add_document_watchers, @project) -%> +

+<% @document.project.users.sort.each do |user| -%> + +<% end -%> +

+<% end %> + diff --git a/app/views/documents/show.rhtml b/app/views/documents/show.rhtml index 922fe36b..63444f0a 100644 --- a/app/views/documents/show.rhtml +++ b/app/views/documents/show.rhtml @@ -27,6 +27,16 @@ <% html_title h(@document.title) -%> +<% content_for :sidebar do %> + <% if User.current.allowed_to?(:add_document_watchers, @project) || + (@document.watchers.present? && User.current.allowed_to?(:view_document_watchers, @project)) %> +
+ <%= render :partial => 'watchers/watchers', :locals => {:watched => @document} %> +
+ <% end %> + +<% end %> + <% content_for :header_tags do %> <%= stylesheet_link_tag 'scm' %> <% end %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 6bde069f..7fe8846b 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -774,6 +774,7 @@ en: label_incoming_emails: Incoming emails label_generate_key: Generate a key label_issue_watchers: Watchers + label_document_watchers: Watchers label_example: Example label_display: Display label_sort: Sort diff --git a/lib/redmine.rb b/lib/redmine.rb index 0ce62425..ec5a2618 100644 --- a/lib/redmine.rb +++ b/lib/redmine.rb @@ -116,6 +116,9 @@ Redmine::AccessControl.map do |map| map.project_module :documents do |map| map.permission :manage_documents, {:documents => [:new, :edit, :destroy, :add_attachment]}, :require => :loggedin map.permission :view_documents, :documents => [:index, :show, :download] + map.permission :view_document_watchers, {} + map.permission :add_document_watchers, {:watchers => :new} + map.permission :delete_document_watchers, {:watchers => :destroy} end map.project_module :files do |map| diff --git a/test/fixtures/roles.yml b/test/fixtures/roles.yml index cd2538cc..2f287e1b 100644 --- a/test/fixtures/roles.yml +++ b/test/fixtures/roles.yml @@ -34,6 +34,9 @@ roles_001: - :comment_news - :view_documents - :manage_documents + - :view_document_watchers + - :add_document_watchers + - :delete_document_watchers - :view_wiki_pages - :export_wiki_pages - :view_wiki_edits diff --git a/test/functional/documents_controller_test.rb b/test/functional/documents_controller_test.rb index 23eaf8e1..c1ae49d7 100644 --- a/test/functional/documents_controller_test.rb +++ b/test/functional/documents_controller_test.rb @@ -83,6 +83,70 @@ LOREM assert_equal 2, ActionMailer::Base.deliveries.size end + context "#new" do + should "allow adding watchers" do + @request.session[:user_id] = 2 + set_tmp_attachments_directory + + post(:new, + :project_id => 'ecookbook', + :document => { + :title => 'DocumentsControllerTest#test_post_new', + :description => 'This is a new document', + :category_id => 2, + :watcher_user_ids => ['2','3'] + }, + :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}) + + assert_redirected_to '/projects/ecookbook/documents' + + document = Document.find_by_title('DocumentsControllerTest#test_post_new') + assert_not_nil document + assert document.watched_by?(User.find(2)) + assert document.watched_by?(User.find(3)) + end + end + + context "POST #edit" do + setup do + @request.session[:user_id] = 2 + set_tmp_attachments_directory + + @document = Document.generate!(:project => Project.find('ecookbook'), + :title => 'Test') + end + + should "update the document" do + post(:edit, + :id => @document.id, + :document => { + :title => 'Change' + }) + + assert_response :redirect + + @document.reload + assert_not_nil @document + assert_equal 'Change', @document.title + end + + should "allow adding watchers" do + post(:edit, + :id => @document.id, + :document => { + :title => 'Change', + :watcher_user_ids => ['2','3'] + }) + + assert_response :redirect + + @document.reload + assert_not_nil @document + assert @document.watched_by?(User.find(2)) + assert @document.watched_by?(User.find(3)) + end + end + def test_destroy @request.session[:user_id] = 2 post :destroy, :id => 1 diff --git a/test/unit/document_test.rb b/test/unit/document_test.rb index c3e161d6..ec9545bd 100644 --- a/test/unit/document_test.rb +++ b/test/unit/document_test.rb @@ -14,7 +14,7 @@ require File.expand_path('../../test_helper', __FILE__) class DocumentTest < ActiveSupport::TestCase - fixtures :projects, :enumerations, :documents, :attachments + fixtures :all def test_create doc = Document.new(:project => Project.find(1), :title => 'New document', :category => Enumeration.find_by_name('User documentation')) @@ -51,4 +51,22 @@ class DocumentTest < ActiveSupport::TestCase assert d.attachments.empty? assert_equal d.created_on, d.updated_on end + + should "allow watchers" do + assert Document.included_modules.include?(Redmine::Acts::Watchable::InstanceMethods) + assert Document.new.respond_to?(:add_watcher) + end + + context "#recipients" do + should "include watchers" do + document = Document.generate!(:project => Project.find(1)) + user = User.find(1) + assert document.add_watcher(user) + + assert document.save + + assert document.recipients.include?(user.mail), "Watcher not included in recipients" + end + end + end