瀏覽代碼

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
tags/1.3.0
Toshi MARUYAMA 12 年之前
父節點
當前提交
564321b2d5
共有 3 個檔案被更改,包括 30 行新增4 行删除
  1. 4
    4
      app/helpers/application_helper.rb
  2. 6
    0
      app/models/attachment.rb
  3. 20
    0
      test/unit/attachment_test.rb

+ 4
- 4
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}\""

+ 6
- 0
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

+ 20
- 0
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

Loading…
取消
儲存