From 2f5c17a0f6eac2f50e61053e06672a1e83d4f598 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Sun, 14 Mar 2010 08:33:53 +0000 Subject: [PATCH] Optimization: load attachments when needed. git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@3581 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/helpers/application_helper.rb | 9 ++++----- test/unit/helpers/application_helper_test.rb | 8 ++++++++ 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index ff877f18f..5663cc6d6 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -414,13 +414,11 @@ module ApplicationHelper only_path = options.delete(:only_path) == false ? false : true # when using an image link, try to use an attachment, if possible - attachments = options[:attachments] || (obj && obj.respond_to?(:attachments) ? obj.attachments : nil) - - if attachments - attachments = attachments.sort_by(&:created_on).reverse + if options[:attachments] || (obj && obj.respond_to?(:attachments)) + attachments = nil text.gsub!(/src="([^\/"]+\.(bmp|gif|jpg|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 @@ -592,6 +590,7 @@ module ApplicationHelper :class => (prefix == 'export' ? 'source download' : 'source') end when 'attachment' + attachments = options[:attachments] || (obj && obj.respond_to?(:attachments) ? obj.attachments : nil) if attachments && attachment = attachments.detect {|a| a.filename == name } link = link_to h(attachment.filename), {:only_path => only_path, :controller => 'attachments', :action => 'download', :id => attachment}, :class => 'attachment' diff --git a/test/unit/helpers/application_helper_test.rb b/test/unit/helpers/application_helper_test.rb index f5b0fb7d3..fafd9c04e 100644 --- a/test/unit/helpers/application_helper_test.rb +++ b/test/unit/helpers/application_helper_test.rb @@ -211,6 +211,14 @@ RAW to_test.each { |text, result| assert_equal "

#{result}

", textilizable(text), "#{text} failed" } end + def test_attachment_links + attachment_link = link_to('error281.txt', {:controller => 'attachments', :action => 'download', :id => '1'}, :class => 'attachment') + to_test = { + 'attachment:error281.txt' => attachment_link + } + to_test.each { |text, result| assert_equal "

#{result}

", textilizable(text, :attachments => Issue.find(3).attachments), "#{text} failed" } + end + def test_wiki_links to_test = { '[[CookBook documentation]]' => 'CookBook documentation', -- 2.39.5