Skip to content

Commit

Permalink
add attachment_link macro, to create links to any attachment (also fo…
Browse files Browse the repository at this point in the history
…r other entities) with id
  • Loading branch information
alexandermeindl committed Jan 28, 2025
1 parent cd0ca07 commit d66c71e
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- gps macro has been added
- add options for link to video with youtube macro
- add options for link to video with vimeo macro
- attachment_link macro has been added

## 3.4.0

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ Uninstall `additionals` plugin.
* PDF for wiki pages
* Wiki macros for:
* asciinema
* attachment_link
* cryptocompare
* date
* fa
Expand Down
49 changes: 49 additions & 0 deletions lib/additionals/wiki_macros/attachment_link_macro.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# frozen_string_literal: true

module Additionals
module WikiMacros
module AttachmentLinkMacro
Redmine::WikiFormatting::Macros.register do
desc <<-DESCRIPTION
Link to any attachment.
Syntax:
{{attachment_link(ID, [id=INT, text=Custom link name, download=BOOL])}}
Parameters:
:param int id: id of attachment (required)
:param string text: alternativ link text (default is filename of attachment)
:param bool download: true or false (if true, attachment is linked to direct download; default false)
Examples:
{{attachment_link(1)}} or {{attachment_link(id=1)}}
...Link to attachment of issue with attachment id 1
{{attachment_link(1, name=Important file of other issue)}}
...Link to attachment of issue with attachment id 1 and use link name "Important file of other issue"
{{attachment_link(1, download=TRUE)}}
...Link to attachment of issue with attachment id 1. Link to download file"
DESCRIPTION

macro :attachment_link do |_obj, args|
args, options = extract_macro_options args, :text, :download, :id

attachment_id = options[:id].presence || args&.first

attachment = Attachment.find attachment_id
return '' unless attachment&.visible?

attachment_options = { class: 'attachment-link' }
attachment_options[:download] = true if options[:download]
attachment_options[:text] = options[:text] if options[:text].present?

link_to_attachment(attachment, **attachment_options)
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/additionals/wiki_macros/redmine_issue_macro.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module WikiMacros
module RedmineIssueMacro
Redmine::WikiFormatting::Macros.register do
desc <<-DESCRIPTION
Link to a redmine.org issue
Link to a redmine.org issue.
Syntax:
Expand Down
2 changes: 1 addition & 1 deletion lib/additionals/wiki_macros/redmine_wiki_macro.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module WikiMacros
module RedmineWikiMacro
Redmine::WikiFormatting::Macros.register do
desc <<-DESCRIPTION
Link to redmine.org wiki page
Link to redmine.org wiki page.
Syntax:
Expand Down
24 changes: 24 additions & 0 deletions test/functional/wiki_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -497,4 +497,28 @@ def test_show_user_with_name_fullname
assert_select '#content a[href=?]', '/users/2',
text: 'John Smith'
end

def test_show_with_attachment_link_macro
@request.session[:user_id] = WIKI_MACRO_USER_ID
page = WikiPage.generate! content: '{{attachment_link(15)}}',
title: __method__.to_s

get :show,
params: { project_id: 1, id: page.title }

assert_response :success
assert_select '#content a[href=?]', '/attachments/15', text: 'private.diff'
end

def test_show_with_attachment_link_macro_without_permission
@request.session[:user_id] = 6
page = WikiPage.generate! content: '{{attachment_link(15)}}',
title: __method__.to_s

get :show,
params: { project_id: 1, id: page.title }

assert_response :success
assert_select '#content a[href=?]', '/attachments/15', text: 'private.diff', count: 0
end
end

0 comments on commit d66c71e

Please sign in to comment.