##// END OF EJS Templates
move logic to use latest image file attachment to class method for common use (#3261)...
Toshi MARUYAMA -
r7788:564321b2d5b8
parent child
Show More
@@ -536,13 +536,13 module ApplicationHelper
536 536 def parse_inline_attachments(text, project, obj, attr, only_path, options)
537 537 # when using an image link, try to use an attachment, if possible
538 538 if options[:attachments] || (obj && obj.respond_to?(:attachments))
539 attachments = nil
539 attachments = options[:attachments] || obj.attachments
540 540 text.gsub!(/src="([^\/"]+\.(bmp|gif|jpg|jpe|jpeg|png))"(\s+alt="([^"]*)")?/i) do |m|
541 541 filename, ext, alt, alttext = $1.downcase, $2, $3, $4
542 attachments ||= (options[:attachments] || obj.attachments).sort_by(&:created_on).reverse
543 542 # search for the picture in attachments
544 if found = attachments.detect { |att| att.filename.downcase == filename }
545 image_url = url_for :only_path => only_path, :controller => 'attachments', :action => 'download', :id => found
543 if found = Attachment.latest_attach(attachments, filename)
544 image_url = url_for :only_path => only_path, :controller => 'attachments',
545 :action => 'download', :id => found
546 546 desc = found.description.to_s.gsub('"', '')
547 547 if !desc.blank? && alttext.blank?
548 548 alt = " title=\"#{desc}\" alt=\"#{desc}\""
@@ -167,6 +167,12 class Attachment < ActiveRecord::Base
167 167 {:files => attached, :unsaved => obj.unsaved_attachments}
168 168 end
169 169
170 def self.latest_attach(attachments, filename)
171 attachments.sort_by(&:created_on).reverse.detect {
172 |att| att.filename.downcase == filename.downcase
173 }
174 end
175
170 176 private
171 177 def sanitize_filename(value)
172 178 # get only the filename, not the whole path
@@ -121,4 +121,24 class AttachmentTest < ActiveSupport::TestCase
121 121 end
122 122 end
123 123 end
124
125 def test_latest_attach
126 Attachment.storage_path = "#{Rails.root}/test/fixtures/files"
127 a1 = Attachment.find(16)
128 assert_equal "testfile.png", a1.filename
129 assert a1.readable?
130 assert (! a1.visible?(User.anonymous))
131 assert a1.visible?(User.find(2))
132 a2 = Attachment.find(17)
133 assert_equal "testfile.PNG", a2.filename
134 assert a2.readable?
135 assert (! a2.visible?(User.anonymous))
136 assert a2.visible?(User.find(2))
137 assert a1.created_on < a2.created_on
138
139 la1 = Attachment.latest_attach([a1, a2], "testfile.png")
140 assert_equal 17, la1.id
141 la2 = Attachment.latest_attach([a1, a2], "Testfile.PNG")
142 assert_equal 17, la2.id
143 end
124 144 end
General Comments 0
You need to be logged in to leave comments. Login now