From fcfb6b537c69861e742acdd59e29de287140434a Mon Sep 17 00:00:00 2001 From: Gregor Schmidt Date: Tue, 19 Jun 2018 11:37:00 +0200 Subject: [PATCH] Adds link to container to attachment view Introduces @link_to_record@ helper which can be used with many Redmine records and generates a general purpose link to a matching view. Furthermore introduces @link_to_attachment_container@ which behaves slightly different than @link_to_record@, since project and version attachments do not point to the project or milestone view, but to the files module within a project. --- app/helpers/application_helper.rb | 34 ++++++++++++++ app/views/layouts/_file.html.erb | 4 +- test/helpers/application_helper_test.rb | 46 ++++++++++++++++++- .../field_format/attachment_format_test.rb | 2 +- 4 files changed, 83 insertions(+), 3 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index dd152f32c..576b99647 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -170,6 +170,40 @@ module ApplicationHelper link_to_if version.visible?, format_version_name(version), version_path(version), options end + RECORD_LINK = { + 'CustomValue' => -> (custom_value) { link_to_record(custom_value.customized) }, + 'Document' => -> (document) { link_to(document.title, document_path(document)) }, + 'Group' => -> (group) { link_to(group.name, group_path(group)) }, + 'Issue' => -> (issue) { link_to_issue(issue, :subject => false) }, + 'Message' => -> (message) { link_to_message(message) }, + 'News' => -> (news) { link_to(news.title, news_path(news)) }, + 'Project' => -> (project) { link_to_project(project) }, + 'User' => -> (user) { link_to_user(user) }, + 'Version' => -> (version) { link_to_version(version) }, + 'WikiPage' => -> (wiki_page) { link_to(wiki_page.pretty_title, project_wiki_page_path(wiki_page.project, wiki_page.title)) } + } + + def link_to_record(record) + if link = RECORD_LINK[record.class.name] + self.instance_exec(record, &link) + end + end + + ATTACHMENT_CONTAINER_LINK = { + # Custom list, since project/version attachments are listed in the files + # view and not in the project/milestone view + 'Project' => -> (project) { link_to(l(:project_module_files), project_files_path(project)) }, + 'Version' => -> (version) { link_to(l(:project_module_files), project_files_path(version.project)) }, + } + + def link_to_attachment_container(attachment_container) + if link = ATTACHMENT_CONTAINER_LINK[attachment_container.class.name] || + RECORD_LINK[attachment_container.class.name] + self.instance_exec(attachment_container, &link) + end + end + + # Helper that formats object for html or text rendering def format_object(object, html=true, &block) if block_given? diff --git a/app/views/layouts/_file.html.erb b/app/views/layouts/_file.html.erb index eb11f9931..3b225508b 100644 --- a/app/views/layouts/_file.html.erb +++ b/app/views/layouts/_file.html.erb @@ -2,7 +2,9 @@ <%= link_to_attachment @attachment, :text => "#{l(:button_download)} (#{number_to_human_size(@attachment.filesize)})", :download => true, :class => 'icon icon-download' -%> -

<%=h @attachment.filename %>

+

+ <%= safe_join([link_to_attachment_container(@attachment.container), @attachment.filename].compact, ' » ') %> +

<%= "#{@attachment.description} - " unless @attachment.description.blank? %> diff --git a/test/helpers/application_helper_test.rb b/test/helpers/application_helper_test.rb index 34e4fe119..e28a56797 100644 --- a/test/helpers/application_helper_test.rb +++ b/test/helpers/application_helper_test.rb @@ -31,7 +31,8 @@ class ApplicationHelperTest < Redmine::HelperTest :trackers, :issue_statuses, :issues, :versions, :documents, :wikis, :wiki_pages, :wiki_contents, :boards, :messages, :news, - :attachments, :enumerations + :attachments, :enumerations, + :custom_values, :custom_fields, :custom_fields_projects def setup super @@ -1490,6 +1491,49 @@ RAW link_to_project(Project.find(1)) end + def test_link_to_record + [ + [custom_values(:custom_values_007), 'eCookbook'], + [documents(:documents_001), 'Test document'], + [Group.find(10), 'A Team'], + [issues(:issues_001), link_to_issue(issues(:issues_001), :subject => false)], + [messages(:messages_001), 'First post'], + [news(:news_001), 'eCookbook first release !'], + [projects(:projects_001), 'eCookbook'], + [users(:users_001), 'Redmine Admin'], + [versions(:versions_001), 'eCookbook - 0.1'], + [wiki_pages(:wiki_pages_001), 'CookBook documentation'] + ].each do |record, link| + assert_equal link, link_to_record(record) + end + end + + def test_link_to_attachment_container + field = ProjectCustomField.generate!(:name => "File", :field_format => 'attachment') + project = projects(:projects_001) + project_custom_value_attachment = new_record(Attachment) do + project.custom_field_values = {field.id => {:file => mock_file}} + project.save + end + + news_attachment = attachments(:attachments_004) + news_attachment.container = news(:news_001) + news_attachment.save! + + [ + [project_custom_value_attachment, 'eCookbook'], + [attachments(:attachments_002), 'Test document'], + [attachments(:attachments_001), link_to_issue(issues(:issues_003), :subject => false)], + [attachments(:attachments_013), 'First post'], + [news_attachment, 'eCookbook first release !'], + [attachments(:attachments_008), 'Files'], + [attachments(:attachments_009), 'Files'], + [attachments(:attachments_003), 'Page with an inline image'], + ].each do |attachment, link| + assert_equal link, link_to_attachment_container(attachment.container) + end + end + def test_principals_options_for_select_with_users User.current = nil users = [User.find(2), User.find(4)] diff --git a/test/integration/lib/redmine/field_format/attachment_format_test.rb b/test/integration/lib/redmine/field_format/attachment_format_test.rb index 759a73b17..958b25769 100644 --- a/test/integration/lib/redmine/field_format/attachment_format_test.rb +++ b/test/integration/lib/redmine/field_format/attachment_format_test.rb @@ -85,7 +85,7 @@ class AttachmentFieldFormatTest < Redmine::IntegrationTest # preview the attachment get link.attr('href') assert_response :success - assert_select 'h2', :text => 'testfile.txt' + assert_select 'h2', :text => "#{issue.tracker} ##{issue.id} » testfile.txt" end def test_create_without_attachment -- 2.18.0