diff --git a/app/controllers/documents_controller.rb b/app/controllers/documents_controller.rb index 104cca10..b37a8371 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 7b1e4ef3..84203a34 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 6829b9bf..a7cefb73 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;' %>