##// END OF EJS Templates
Backported r9130 from trunk....
Jean-Philippe Lang -
r9027:bddc546bf0ae
parent child
Show More
@@ -1,86 +1,88
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2011 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 class DocumentsController < ApplicationController
19 19 default_search_scope :documents
20 20 model_object Document
21 21 before_filter :find_project, :only => [:index, :new]
22 22 before_filter :find_model_object, :except => [:index, :new]
23 23 before_filter :find_project_from_association, :except => [:index, :new]
24 24 before_filter :authorize
25 25
26 26 helper :attachments
27 27
28 28 def index
29 29 @sort_by = %w(category date title author).include?(params[:sort_by]) ? params[:sort_by] : 'category'
30 30 documents = @project.documents.find :all, :include => [:attachments, :category]
31 31 case @sort_by
32 32 when 'date'
33 33 @grouped = documents.group_by {|d| d.updated_on.to_date }
34 34 when 'title'
35 35 @grouped = documents.group_by {|d| d.title.first.upcase}
36 36 when 'author'
37 37 @grouped = documents.select{|d| d.attachments.any?}.group_by {|d| d.attachments.last.author}
38 38 else
39 39 @grouped = documents.group_by(&:category)
40 40 end
41 41 @document = @project.documents.build
42 42 render :layout => false if request.xhr?
43 43 end
44 44
45 45 def show
46 46 @attachments = @document.attachments.find(:all, :order => "created_on DESC")
47 47 end
48 48
49 49 def new
50 @document = @project.documents.build(params[:document])
50 @document = @project.documents.build
51 @document.safe_attributes = params[:document]
51 52 if request.post? and @document.save
52 53 attachments = Attachment.attach_files(@document, params[:attachments])
53 54 render_attachment_warning_if_needed(@document)
54 55 flash[:notice] = l(:notice_successful_create)
55 56 redirect_to :action => 'index', :project_id => @project
56 57 end
57 58 end
58 59
59 60 def edit
60 61 @categories = DocumentCategory.active #TODO: use it in the views
61 if request.post? and @document.update_attributes(params[:document])
62 @document.safe_attributes = params[:document]
63 if request.post? and @document.save
62 64 flash[:notice] = l(:notice_successful_update)
63 65 redirect_to :action => 'show', :id => @document
64 66 end
65 67 end
66 68
67 69 def destroy
68 70 @document.destroy
69 71 redirect_to :controller => 'documents', :action => 'index', :project_id => @project
70 72 end
71 73
72 74 def add_attachment
73 75 attachments = Attachment.attach_files(@document, params[:attachments])
74 76 render_attachment_warning_if_needed(@document)
75 77
76 78 Mailer.deliver_attachments_added(attachments[:files]) if attachments.present? && attachments[:files].present? && Setting.notified_events.include?('document_added')
77 79 redirect_to :action => 'show', :id => @document
78 80 end
79 81
80 82 private
81 83 def find_project
82 84 @project = Project.find(params[:project_id])
83 85 rescue ActiveRecord::RecordNotFound
84 86 render_404
85 87 end
86 88 end
@@ -1,52 +1,55
1 1 # RedMine - project management software
2 2 # Copyright (C) 2006-2011 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 class Document < ActiveRecord::Base
19 include Redmine::SafeAttributes
19 20 belongs_to :project
20 21 belongs_to :category, :class_name => "DocumentCategory", :foreign_key => "category_id"
21 22 acts_as_attachable :delete_permission => :manage_documents
22 23
23 24 acts_as_searchable :columns => ['title', "#{table_name}.description"], :include => :project
24 25 acts_as_event :title => Proc.new {|o| "#{l(:label_document)}: #{o.title}"},
25 26 :author => Proc.new {|o| (a = o.attachments.find(:first, :order => "#{Attachment.table_name}.created_on ASC")) ? a.author : nil },
26 27 :url => Proc.new {|o| {:controller => 'documents', :action => 'show', :id => o.id}}
27 28 acts_as_activity_provider :find_options => {:include => :project}
28 29
29 30 validates_presence_of :project, :title, :category
30 31 validates_length_of :title, :maximum => 60
31 32
32 33 named_scope :visible, lambda {|*args| { :include => :project,
33 34 :conditions => Project.allowed_to_condition(args.shift || User.current, :view_documents, *args) } }
34 35
36 safe_attributes 'category_id', 'title', 'description'
37
35 38 def visible?(user=User.current)
36 39 !user.nil? && user.allowed_to?(:view_documents, project)
37 40 end
38 41
39 42 def after_initialize
40 43 if new_record?
41 44 self.category ||= DocumentCategory.default
42 45 end
43 46 end
44 47
45 48 def updated_on
46 49 unless @updated_on
47 50 a = attachments.find(:first, :order => 'created_on DESC')
48 51 @updated_on = (a && a.created_on) || created_on
49 52 end
50 53 @updated_on
51 54 end
52 55 end
General Comments 0
You need to be logged in to leave comments. Login now