[LSS#4190] Add watching to documents

This commit is contained in:
Eric Davis 2011-04-13 13:23:37 -07:00
parent 61a21f4990
commit 3af5544dbc
9 changed files with 138 additions and 10 deletions

View File

@ -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

View File

@ -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

View File

@ -9,6 +9,15 @@
<p><label for="document_description"><%=l(:field_description)%></label>
<%= text_area 'document', 'description', :cols => 60, :rows => 15, :class => 'wiki-edit' %></p>
<% if User.current.allowed_to?(:add_document_watchers, @project) -%>
<p id="watchers_form"><label><%= l(:label_document_watchers) %></label>
<% @document.project.users.sort.each do |user| -%>
<label class="floating"><%= check_box_tag 'document[watcher_user_ids][]', user.id, @document.watched_by?(user) %> <%=h user %></label>
<% end -%>
</p>
<% end %>
<!--[eoform:document]-->
</div>

View File

@ -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)) %>
<div id="watchers">
<%= render :partial => 'watchers/watchers', :locals => {:watched => @document} %>
</div>
<% end %>
<% end %>
<% content_for :header_tags do %>
<%= stylesheet_link_tag 'scm' %>
<% end %>

View File

@ -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

View File

@ -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|

View File

@ -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

View File

@ -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

View File

@ -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