From 43fd27fd0ce4111501c36a6ef162aed61bf3318c Mon Sep 17 00:00:00 2001
From: Jean-Philippe Lang <jp_lang@yahoo.fr>
Date: Thu, 26 Nov 2009 20:12:20 +0000
Subject: [PATCH] Show last update datetime (last attachment added) on document
 list (#4232).

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@3095 e93f8b46-1217-0410-a6f0-8f06a7374b81
---
 app/controllers/documents_controller.rb |  2 +-
 app/models/document.rb                  |  8 ++++++++
 app/views/documents/_document.rhtml     |  2 +-
 test/fixtures/attachments.yml           | 14 +++++++++++++-
 test/fixtures/documents.yml             |  9 ++++++++-
 test/unit/document_test.rb              | 14 +++++++++++++-
 6 files changed, 44 insertions(+), 5 deletions(-)

diff --git a/app/controllers/documents_controller.rb b/app/controllers/documents_controller.rb
index c9eecf4f7..9d9c5a7d0 100644
--- a/app/controllers/documents_controller.rb
+++ b/app/controllers/documents_controller.rb
@@ -28,7 +28,7 @@ class DocumentsController < ApplicationController
     documents = @project.documents.find :all, :include => [:attachments, :category]
     case @sort_by
     when 'date'
-      @grouped = documents.group_by {|d| d.created_on.to_date }
+      @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/models/document.rb b/app/models/document.rb
index a96c278d0..1318e823d 100644
--- a/app/models/document.rb
+++ b/app/models/document.rb
@@ -34,4 +34,12 @@ class Document < ActiveRecord::Base
       self.category ||= DocumentCategory.default
     end
   end
+  
+  def updated_on
+    unless @updated_on
+      a = attachments.find(:first, :order => 'created_on DESC')
+      @updated_on = (a && a.created_on) || created_on
+    end
+    @updated_on
+  end
 end
diff --git a/app/views/documents/_document.rhtml b/app/views/documents/_document.rhtml
index 47b450000..94a64ac80 100644
--- a/app/views/documents/_document.rhtml
+++ b/app/views/documents/_document.rhtml
@@ -1,3 +1,3 @@
 <p><%= link_to h(document.title), :controller => 'documents', :action => 'show', :id => document %><br />
 <% unless document.description.blank? %><%=h(truncate(document.description, :length => 250)) %><br /><% end %>
-<em><%= format_time(document.created_on) %></em></p>
\ No newline at end of file
+<em><%= format_time(document.updated_on) %></em></p>
\ No newline at end of file
diff --git a/test/fixtures/attachments.yml b/test/fixtures/attachments.yml
index 62d5b38a1..c6493bd5e 100644
--- a/test/fixtures/attachments.yml
+++ b/test/fixtures/attachments.yml
@@ -12,7 +12,7 @@ attachments_001:
   filename: error281.txt
   author_id: 2
 attachments_002: 
-  created_on: 2006-07-19 21:07:27 +02:00
+  created_on: 2007-01-27 15:08:27 +01:00
   downloads: 0
   content_type: text/plain
   disk_filename: 060719210727_document.txt
@@ -121,4 +121,16 @@ attachments_010:
   filename: picture.jpg
   author_id: 2
   content_type: image/jpeg
+attachments_011: 
+  created_on: 2007-02-12 15:08:27 +01:00
+  container_type: Document
+  container_id: 1
+  downloads: 0
+  disk_filename: 060719210727_picture.jpg
+  digest: b91e08d0cf966d5c6ff411bd8c4cc3a2
+  id: 11
+  filesize: 452
+  filename: picture.jpg
+  author_id: 2
+  content_type: image/jpeg
   
\ No newline at end of file
diff --git a/test/fixtures/documents.yml b/test/fixtures/documents.yml
index 137cbcc67..042b93533 100644
--- a/test/fixtures/documents.yml
+++ b/test/fixtures/documents.yml
@@ -4,4 +4,11 @@ documents_001:
   title: "Test document"
   id: 1
   description: "Document description"
-  category_id: 1
\ No newline at end of file
+  category_id: 1
+documents_002: 
+  created_on: 2007-02-12 15:08:27 +01:00
+  project_id: 1
+  title: "An other document"
+  id: 2
+  description: ""
+  category_id: 2
\ No newline at end of file
diff --git a/test/unit/document_test.rb b/test/unit/document_test.rb
index 02ae94dd0..9e76311bd 100644
--- a/test/unit/document_test.rb
+++ b/test/unit/document_test.rb
@@ -18,7 +18,7 @@
 require File.dirname(__FILE__) + '/../test_helper'
 
 class DocumentTest < ActiveSupport::TestCase
-  fixtures :projects, :enumerations, :documents
+  fixtures :projects, :enumerations, :documents, :attachments
 
   def test_create
     doc = Document.new(:project => Project.find(1), :title => 'New document', :category => Enumeration.find_by_name('User documentation'))
@@ -43,4 +43,16 @@ class DocumentTest < ActiveSupport::TestCase
     assert_equal e, doc.category
     assert doc.save
   end
+  
+  def test_updated_on_with_attachments
+    d = Document.find(1)
+    assert d.attachments.any?
+    assert_equal d.attachments.map(&:created_on).max, d.updated_on
+  end
+  
+  def test_updated_on_without_attachments
+    d = Document.find(2)
+    assert d.attachments.empty?
+    assert_equal d.created_on, d.updated_on
+  end
 end
-- 
GitLab