diff --git a/app/controllers/documents_controller.rb b/app/controllers/documents_controller.rb index dbf0e88dc..9690a5fb4 100644 --- a/app/controllers/documents_controller.rb +++ b/app/controllers/documents_controller.rb @@ -31,7 +31,8 @@ class DocumentsController < ApplicationController documents = @project.documents.includes(:attachments, :category).to_a case @sort_by when 'date' - @grouped = documents.group_by {|d| d.updated_on.to_date } + documents.sort!{|a,b| b.updated_on <=> a.updated_on} + @grouped = documents.group_by {|d| d.updated_on.to_date} when 'title' @grouped = documents.group_by {|d| d.title.first.upcase} when 'author' diff --git a/app/views/documents/index.html.erb b/app/views/documents/index.html.erb index fc4090f55..c0458f8bf 100644 --- a/app/views/documents/index.html.erb +++ b/app/views/documents/index.html.erb @@ -18,7 +18,7 @@ <% if @grouped.empty? %>

<%= l(:label_no_data) %>

<% end %> -<% @grouped.keys.sort.each do |group| %> +<% @grouped.keys.sort.__send__(@sort_by == 'date' ? :reverse_each : :each) do |group| %>

<%= group %>

<%= render :partial => 'documents/document', :collection => @grouped[group] %> <% end %> diff --git a/test/fixtures/attachments.yml b/test/fixtures/attachments.yml index 1b8740f49..15556c6c6 100644 --- a/test/fixtures/attachments.yml +++ b/test/fixtures/attachments.yml @@ -268,3 +268,16 @@ attachments_020: filename: root_attachment.txt filesize: 54 author_id: 2 +attachments_021: + created_on: 2007-03-05 15:08:27 +01:00 + container_type: Document + container_id: 3 + downloads: 0 + disk_filename: 060719210727_archive.zip + disk_directory: "2006/07" + digest: b91e08d0cf966d5c6ff411bd8c4cc3a2 + id: 21 + filesize: 157 + filename: archive.zip + author_id: 1 + content_type: application/zip diff --git a/test/fixtures/documents.yml b/test/fixtures/documents.yml index eda9a1046..b96fab6cf 100644 --- a/test/fixtures/documents.yml +++ b/test/fixtures/documents.yml @@ -12,3 +12,10 @@ documents_002: id: 2 description: "" category_id: 1 +documents_003: + created_on: 2007-03-05 15:08:27 +01:00 + project_id: 1 + title: "An other document 2" + id: 3 + description: "" + category_id: 3 diff --git a/test/functional/documents_controller_test.rb b/test/functional/documents_controller_test.rb index 0a7aed72b..272b82d31 100644 --- a/test/functional/documents_controller_test.rb +++ b/test/functional/documents_controller_test.rb @@ -45,13 +45,32 @@ class DocumentsControllerTest < Redmine::ControllerTest end end + def test_index_grouped_by_category + get :index, :params => { + :project_id => 'ecookbook', + :sort_by => 'category' + } + assert_response :success + assert_select '#content' do + # ascending order of DocumentCategory#id. + ['Uncategorized', 'Technical documentation'].each_with_index do |text,idx| + assert_select "h3:nth-of-type(#{idx + 1})", :text => text + end + end + end + def test_index_grouped_by_date get :index, :params => { :project_id => 'ecookbook', :sort_by => 'date' } assert_response :success - assert_select 'h3', :text => '2007-02-12' + assert_select '#content' do + # descending order of date. + ['2007-03-05', '2007-02-12'].each_with_index do |text,idx| + assert_select "h3:nth-of-type(#{idx + 1})", :text => text + end + end end def test_index_grouped_by_title @@ -60,7 +79,12 @@ class DocumentsControllerTest < Redmine::ControllerTest :sort_by => 'title' } assert_response :success - assert_select 'h3', :text => 'T' + assert_select '#content' do + # ascending order of title. + ['A', 'T'].each_with_index do |text,idx| + assert_select "h3:nth-of-type(#{idx + 1})", :text => text + end + end end def test_index_grouped_by_author @@ -69,8 +93,13 @@ class DocumentsControllerTest < Redmine::ControllerTest :sort_by => 'author' } assert_response :success - assert_select 'h3', :text => 'John Smith' - end + assert_select '#content' do + # ascending order of author. + ['John Smith', 'Redmine Admin'].each_with_index do |text,idx| + assert_select "h3:nth-of-type(#{idx + 1})", :text => text + end + end + end def test_index_with_long_description # adds a long description to the first document diff --git a/test/unit/project_copy_test.rb b/test/unit/project_copy_test.rb index 752e51b4c..4ba3e79a4 100644 --- a/test/unit/project_copy_test.rb +++ b/test/unit/project_copy_test.rb @@ -368,7 +368,7 @@ class ProjectCopyTest < ActiveSupport::TestCase source_project = Project.find(1) assert @project.copy(source_project) - assert_equal 2, @project.documents.size + assert_equal 3, @project.documents.size @project.documents.each do |document| assert !source_project.documents.include?(document) end