0001-Adds-link-to-container-to-attachment-view.patch

Gregor Schmidt, 2018-07-04 14:33

Download (7.53 KB)

View differences:

app/helpers/application_helper.rb
170 170
    link_to_if version.visible?, format_version_name(version), version_path(version), options
171 171
  end
172 172

  
173
  RECORD_LINK = {
174
    'CustomValue'  => -> (custom_value) { link_to_record(custom_value.customized) },
175
    'Document'     => -> (document)     { link_to(document.title, document_path(document)) },
176
    'Group'        => -> (group)        { link_to(group.name, group_path(group)) },
177
    'Issue'        => -> (issue)        { link_to_issue(issue, :subject => false) },
178
    'Message'      => -> (message)      { link_to_message(message) },
179
    'News'         => -> (news)         { link_to(news.title, news_path(news)) },
180
    'Project'      => -> (project)      { link_to_project(project) },
181
    'User'         => -> (user)         { link_to_user(user) },
182
    'Version'      => -> (version)      { link_to_version(version) },
183
    'WikiPage'     => -> (wiki_page)    { link_to(wiki_page.pretty_title, project_wiki_page_path(wiki_page.project, wiki_page.title)) }
184
  }
185

  
186
  def link_to_record(record)
187
    if link = RECORD_LINK[record.class.name]
188
      self.instance_exec(record, &link)
189
    end
190
  end
191

  
192
  ATTACHMENT_CONTAINER_LINK = {
193
    # Custom list, since project/version attachments are listed in the files
194
    # view and not in the project/milestone view
195
    'Project'      => -> (project)      { link_to(l(:project_module_files), project_files_path(project)) },
196
    'Version'      => -> (version)      { link_to(l(:project_module_files), project_files_path(version.project)) },
197
  }
198

  
199
  def link_to_attachment_container(attachment_container)
200
    if link = ATTACHMENT_CONTAINER_LINK[attachment_container.class.name] ||
201
              RECORD_LINK[attachment_container.class.name]
202
      self.instance_exec(attachment_container, &link)
203
    end
204
  end
205

  
206

  
173 207
  # Helper that formats object for html or text rendering
174 208
  def format_object(object, html=true, &block)
175 209
    if block_given?
app/views/layouts/_file.html.erb
2 2
  <%= link_to_attachment @attachment, :text => "#{l(:button_download)} (#{number_to_human_size(@attachment.filesize)})", :download => true, :class => 'icon icon-download' -%>
3 3
</div>
4 4

  
5
<h2><%=h @attachment.filename %></h2>
5
<h2>
6
  <%= safe_join([link_to_attachment_container(@attachment.container), @attachment.filename].compact, ' » ') %>
7
</h2>
6 8

  
7 9
<div class="attachments">
8 10
<p><%= "#{@attachment.description} - " unless @attachment.description.blank? %>
test/helpers/application_helper_test.rb
31 31
           :trackers, :issue_statuses, :issues, :versions, :documents,
32 32
           :wikis, :wiki_pages, :wiki_contents,
33 33
           :boards, :messages, :news,
34
           :attachments, :enumerations
34
           :attachments, :enumerations,
35
           :custom_values, :custom_fields, :custom_fields_projects
35 36

  
36 37
  def setup
37 38
    super
......
1490 1491
                 link_to_project(Project.find(1))
1491 1492
  end
1492 1493

  
1494
  def test_link_to_record
1495
    [
1496
      [custom_values(:custom_values_007), '<a href="/projects/ecookbook">eCookbook</a>'],
1497
      [documents(:documents_001),         '<a href="/documents/1">Test document</a>'],
1498
      [Group.find(10),                    '<a href="/groups/10">A Team</a>'],
1499
      [issues(:issues_001),               link_to_issue(issues(:issues_001), :subject => false)],
1500
      [messages(:messages_001),           '<a href="/boards/1/topics/1">First post</a>'],
1501
      [news(:news_001),                   '<a href="/news/1">eCookbook first release !</a>'],
1502
      [projects(:projects_001),           '<a href="/projects/ecookbook">eCookbook</a>'],
1503
      [users(:users_001),                 '<a class="user active" href="/users/1">Redmine Admin</a>'],
1504
      [versions(:versions_001),           '<a title="07/01/2006" href="/versions/1">eCookbook - 0.1</a>'],
1505
      [wiki_pages(:wiki_pages_001),       '<a href="/projects/ecookbook/wiki/CookBook_documentation">CookBook documentation</a>']
1506
    ].each do |record, link|
1507
      assert_equal link, link_to_record(record)
1508
    end
1509
  end
1510

  
1511
  def test_link_to_attachment_container
1512
    field = ProjectCustomField.generate!(:name => "File", :field_format => 'attachment')
1513
    project = projects(:projects_001)
1514
    project_custom_value_attachment = new_record(Attachment) do
1515
      project.custom_field_values = {field.id => {:file => mock_file}}
1516
      project.save
1517
    end
1518

  
1519
    news_attachment = attachments(:attachments_004)
1520
    news_attachment.container = news(:news_001)
1521
    news_attachment.save!
1522

  
1523
    [
1524
      [project_custom_value_attachment, '<a href="/projects/ecookbook">eCookbook</a>'],
1525
      [attachments(:attachments_002),   '<a href="/documents/1">Test document</a>'],
1526
      [attachments(:attachments_001),   link_to_issue(issues(:issues_003), :subject => false)],
1527
      [attachments(:attachments_013),   '<a href="/boards/1/topics/1">First post</a>'],
1528
      [news_attachment,                 '<a href="/news/1">eCookbook first release !</a>'],
1529
      [attachments(:attachments_008),   '<a href="/projects/ecookbook/files">Files</a>'],
1530
      [attachments(:attachments_009),   '<a href="/projects/ecookbook/files">Files</a>'],
1531
      [attachments(:attachments_003),   '<a href="/projects/ecookbook/wiki/Page_with_an_inline_image">Page with an inline image</a>'],
1532
    ].each do |attachment, link|
1533
      assert_equal link, link_to_attachment_container(attachment.container)
1534
    end
1535
  end
1536

  
1493 1537
  def test_principals_options_for_select_with_users
1494 1538
    User.current = nil
1495 1539
    users = [User.find(2), User.find(4)]
test/integration/lib/redmine/field_format/attachment_format_test.rb
85 85
    # preview the attachment
86 86
    get link.attr('href')
87 87
    assert_response :success
88
    assert_select 'h2', :text => 'testfile.txt'
88
    assert_select 'h2', :text => "#{issue.tracker} ##{issue.id} » testfile.txt"
89 89
  end
90 90

  
91 91
  def test_create_without_attachment
92
-