From 564321b2d5b84a3b7d45b804c932790f8c6acac5 Mon Sep 17 00:00:00 2001 From: Toshi MARUYAMA Date: Thu, 24 Nov 2011 05:31:29 +0000 Subject: [PATCH] move logic to use latest image file attachment to class method for common use (#3261) git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@7908 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/helpers/application_helper.rb | 8 ++++---- app/models/attachment.rb | 6 ++++++ test/unit/attachment_test.rb | 20 ++++++++++++++++++++ 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index b8d0ba7fe..0f2c86746 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -536,13 +536,13 @@ module ApplicationHelper def parse_inline_attachments(text, project, obj, attr, only_path, options) # when using an image link, try to use an attachment, if possible if options[:attachments] || (obj && obj.respond_to?(:attachments)) - attachments = nil + attachments = options[:attachments] || obj.attachments text.gsub!(/src="([^\/"]+\.(bmp|gif|jpg|jpe|jpeg|png))"(\s+alt="([^"]*)")?/i) do |m| filename, ext, alt, alttext = $1.downcase, $2, $3, $4 - attachments ||= (options[:attachments] || obj.attachments).sort_by(&:created_on).reverse # search for the picture in attachments - if found = attachments.detect { |att| att.filename.downcase == filename } - image_url = url_for :only_path => only_path, :controller => 'attachments', :action => 'download', :id => found + if found = Attachment.latest_attach(attachments, filename) + image_url = url_for :only_path => only_path, :controller => 'attachments', + :action => 'download', :id => found desc = found.description.to_s.gsub('"', '') if !desc.blank? && alttext.blank? alt = " title=\"#{desc}\" alt=\"#{desc}\"" diff --git a/app/models/attachment.rb b/app/models/attachment.rb index c5a5187d1..2dda3f353 100644 --- a/app/models/attachment.rb +++ b/app/models/attachment.rb @@ -167,6 +167,12 @@ class Attachment < ActiveRecord::Base {:files => attached, :unsaved => obj.unsaved_attachments} end + def self.latest_attach(attachments, filename) + attachments.sort_by(&:created_on).reverse.detect { + |att| att.filename.downcase == filename.downcase + } + end + private def sanitize_filename(value) # get only the filename, not the whole path diff --git a/test/unit/attachment_test.rb b/test/unit/attachment_test.rb index 85404a375..c290fe960 100644 --- a/test/unit/attachment_test.rb +++ b/test/unit/attachment_test.rb @@ -121,4 +121,24 @@ class AttachmentTest < ActiveSupport::TestCase end end end + + def test_latest_attach + Attachment.storage_path = "#{Rails.root}/test/fixtures/files" + a1 = Attachment.find(16) + assert_equal "testfile.png", a1.filename + assert a1.readable? + assert (! a1.visible?(User.anonymous)) + assert a1.visible?(User.find(2)) + a2 = Attachment.find(17) + assert_equal "testfile.PNG", a2.filename + assert a2.readable? + assert (! a2.visible?(User.anonymous)) + assert a2.visible?(User.find(2)) + assert a1.created_on < a2.created_on + + la1 = Attachment.latest_attach([a1, a2], "testfile.png") + assert_equal 17, la1.id + la2 = Attachment.latest_attach([a1, a2], "Testfile.PNG") + assert_equal 17, la2.id + end end -- 2.39.5