diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 4825615a1..2a06ad926 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -91,14 +91,10 @@ module ApplicationHelper # * :download - Force download (default: false) def link_to_attachment(attachment, options={}) text = options.delete(:text) || attachment.filename - action = options.delete(:download) ? 'download' : 'show' - opt_only_path = {} - opt_only_path[:only_path] = (options[:only_path] == false ? false : true) - options.delete(:only_path) - link_to(h(text), - {:controller => 'attachments', :action => action, - :id => attachment, :filename => attachment.filename}.merge(opt_only_path), - options) + route_method = options.delete(:download) ? :download_named_attachment_path : :named_attachment_path + html_options = options.slice!(:only_path) + url = send(route_method, attachment, attachment.filename, options) + link_to text, url, html_options end # Generates a link to a SCM revision diff --git a/config/routes.rb b/config/routes.rb index 9a93b4780..7a1eb663a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -257,10 +257,10 @@ RedmineApp::Application.routes.draw do get 'projects/:id/repository', :to => 'repositories#show', :path => nil # additional routes for having the file name at the end of url - match 'attachments/:id/:filename', :controller => 'attachments', :action => 'show', :id => /\d+/, :filename => /.*/, :via => :get - match 'attachments/download/:id/:filename', :controller => 'attachments', :action => 'download', :id => /\d+/, :filename => /.*/, :via => :get - match 'attachments/download/:id', :controller => 'attachments', :action => 'download', :id => /\d+/, :via => :get - match 'attachments/thumbnail/:id(/:size)', :controller => 'attachments', :action => 'thumbnail', :id => /\d+/, :via => :get, :size => /\d+/ + get 'attachments/:id/:filename', :to => 'attachments#show', :id => /\d+/, :filename => /.*/, :as => 'named_attachment' + get 'attachments/download/:id/:filename', :to => 'attachments#download', :id => /\d+/, :filename => /.*/, :as => 'download_named_attachment' + get 'attachments/download/:id', :to => 'attachments#download', :id => /\d+/ + get 'attachments/thumbnail/:id(/:size)', :to => 'attachments#thumbnail', :id => /\d+/, :size => /\d+/ resources :attachments, :only => [:show, :destroy] resources :groups do diff --git a/test/unit/helpers/application_helper_test.rb b/test/unit/helpers/application_helper_test.rb index c5b3aaadc..506c30cc7 100644 --- a/test/unit/helpers/application_helper_test.rb +++ b/test/unit/helpers/application_helper_test.rb @@ -1063,6 +1063,20 @@ RAW assert_equal ::I18n.t(:label_user_anonymous), t end + def test_link_to_attachment + a = Attachment.find(3) + assert_equal 'logo.gif', + link_to_attachment(a) + assert_equal 'Text', + link_to_attachment(a, :text => 'Text') + assert_equal 'logo.gif', + link_to_attachment(a, :class => 'foo') + assert_equal 'logo.gif', + link_to_attachment(a, :download => true) + assert_equal 'logo.gif', + link_to_attachment(a, :only_path => false) + end + def test_link_to_project project = Project.find(1) assert_equal %(eCookbook),