From 8e00f57a886ae529ae308a17bfc9a363056754bf Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Tue, 18 Dec 2007 18:50:41 +0000 Subject: [PATCH] Moved ProjectsController#list_documents and add_document to DocumentsController#index and new. git-svn-id: http://redmine.rubyforge.org/svn/trunk@1011 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/controllers/documents_controller.rb | 42 ++++++++++-- app/controllers/projects_controller.rb | 28 -------- .../index.rhtml} | 4 +- .../new.rhtml} | 2 +- config/routes.rb | 1 + lib/redmine.rb | 6 +- test/functional/documents_controller_test.rb | 64 +++++++++++++++++++ test/functional/projects_controller_test.rb | 7 -- 8 files changed, 109 insertions(+), 45 deletions(-) rename app/views/{projects/list_documents.rhtml => documents/index.rhtml} (87%) rename app/views/{projects/add_document.rhtml => documents/new.rhtml} (61%) create mode 100644 test/functional/documents_controller_test.rb diff --git a/app/controllers/documents_controller.rb b/app/controllers/documents_controller.rb index 104cca10c..b37a8371a 100644 --- a/app/controllers/documents_controller.rb +++ b/app/controllers/documents_controller.rb @@ -17,12 +17,40 @@ class DocumentsController < ApplicationController layout 'base' - before_filter :find_project, :authorize - + before_filter :find_project, :only => [:index, :new] + before_filter :find_document, :except => [:index, :new] + before_filter :authorize + + def index + @sort_by = %w(category date title author).include?(params[:sort_by]) ? params[:sort_by] : 'category' + documents = @project.documents.find :all, :include => [:attachments, :category] + case @sort_by + when 'date' + @grouped = documents.group_by {|d| d.created_on.to_date } + when 'title' + @grouped = documents.group_by {|d| d.title.first.upcase} + when 'author' + @grouped = documents.select{|d| d.attachments.any?}.group_by {|d| d.attachments.last.author} + else + @grouped = documents.group_by(&:category) + end + render :layout => false if request.xhr? + end + def show @attachments = @document.attachments.find(:all, :order => "created_on DESC") end + def new + @document = @project.documents.build(params[:document]) + if request.post? and @document.save + attach_files(@document, params[:attachments]) + flash[:notice] = l(:notice_successful_create) + Mailer.deliver_document_added(@document) if Setting.notified_events.include?('document_added') + redirect_to :action => 'index', :project_id => @project + end + end + def edit @categories = Enumeration::get_values('DCAT') if request.post? and @document.update_attributes(params[:document]) @@ -33,7 +61,7 @@ class DocumentsController < ApplicationController def destroy @document.destroy - redirect_to :controller => 'projects', :action => 'list_documents', :id => @project + redirect_to :controller => 'documents', :action => 'index', :project_id => @project end def download @@ -57,9 +85,15 @@ class DocumentsController < ApplicationController private def find_project + @project = Project.find(params[:project_id]) + rescue ActiveRecord::RecordNotFound + render_404 + end + + def find_document @document = Document.find(params[:id]) @project = @document.project rescue ActiveRecord::RecordNotFound render_404 - end + end end diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index 7b1e4ef3d..84203a34d 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -177,34 +177,6 @@ class ProjectsController < ApplicationController end end - # Add a new document to @project - def add_document - @document = @project.documents.build(params[:document]) - if request.post? and @document.save - attach_files(@document, params[:attachments]) - flash[:notice] = l(:notice_successful_create) - Mailer.deliver_document_added(@document) if Setting.notified_events.include?('document_added') - redirect_to :action => 'list_documents', :id => @project - end - end - - # Show documents list of @project - def list_documents - @sort_by = %w(category date title author).include?(params[:sort_by]) ? params[:sort_by] : 'category' - documents = @project.documents.find :all, :include => [:attachments, :category] - case @sort_by - when 'date' - @grouped = documents.group_by {|d| d.created_on.to_date } - when 'title' - @grouped = documents.group_by {|d| d.title.first.upcase} - when 'author' - @grouped = documents.select{|d| d.attachments.any?}.group_by {|d| d.attachments.last.author} - else - @grouped = documents.group_by(&:category) - end - render :layout => false if request.xhr? - end - # Add a new issue to @project # The new issue will be created from an existing one if copy_from parameter is given def add_issue diff --git a/app/views/projects/list_documents.rhtml b/app/views/documents/index.rhtml similarity index 87% rename from app/views/projects/list_documents.rhtml rename to app/views/documents/index.rhtml index 6829b9bf5..a7cefb733 100644 --- a/app/views/projects/list_documents.rhtml +++ b/app/views/documents/index.rhtml @@ -1,13 +1,13 @@
<%= link_to_if_authorized l(:label_document_new), - {:controller => 'projects', :action => 'add_document', :id => @project}, + {:controller => 'documents', :action => 'new', :project_id => @project}, :class => 'icon icon-add', :onclick => 'Element.show("add-document"); return false;' %>