You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

attachments_helper.rb 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2021 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. module AttachmentsHelper
  19. def container_attachments_edit_path(container)
  20. object_attachments_edit_path container.class.name.underscore.pluralize, container.id
  21. end
  22. def container_attachments_path(container)
  23. object_attachments_path container.class.name.underscore.pluralize, container.id
  24. end
  25. def container_attachments_download_path(container)
  26. object_attachments_download_path container.class.name.underscore.pluralize, container.id
  27. end
  28. # Displays view/delete links to the attachments of the given object
  29. # Options:
  30. # :author -- author names are not displayed if set to false
  31. # :thumbails -- display thumbnails if enabled in settings
  32. def link_to_attachments(container, options = {})
  33. options.assert_valid_keys(:author, :thumbnails)
  34. attachments =
  35. if container.attachments.loaded?
  36. container.attachments
  37. else
  38. container.attachments.preload(:author).to_a
  39. end
  40. if attachments.any?
  41. options = {
  42. :editable => container.attachments_editable?,
  43. :deletable => container.attachments_deletable?,
  44. :author => true
  45. }.merge(options)
  46. render :partial => 'attachments/links',
  47. :locals => {
  48. :container => container,
  49. :attachments => attachments,
  50. :options => options,
  51. :thumbnails => (options[:thumbnails] && Setting.thumbnails_enabled?)
  52. }
  53. end
  54. end
  55. def render_pagination
  56. pagination_links_each @paginator do |text, parameters, options|
  57. if att = @attachments[parameters[:page] - 1]
  58. link_to text, named_attachment_path(att, att.filename)
  59. end
  60. end if @paginator
  61. end
  62. def render_api_attachment(attachment, api, options={})
  63. api.attachment do
  64. render_api_attachment_attributes(attachment, api)
  65. options.each {|key, value| eval("api.#{key} value")}
  66. end
  67. end
  68. def render_api_attachment_attributes(attachment, api)
  69. api.id attachment.id
  70. api.filename attachment.filename
  71. api.filesize attachment.filesize
  72. api.content_type attachment.content_type
  73. api.description attachment.description
  74. api.content_url download_named_attachment_url(attachment, attachment.filename)
  75. if attachment.thumbnailable?
  76. api.thumbnail_url thumbnail_url(attachment)
  77. end
  78. if attachment.author
  79. api.author(:id => attachment.author.id, :name => attachment.author.name)
  80. end
  81. api.created_on attachment.created_on
  82. end
  83. def render_file_content(attachment, content)
  84. if attachment.is_markdown?
  85. render :partial => 'common/markup', :locals => {:markup_text_formatting => 'markdown', :markup_text => content}
  86. elsif attachment.is_textile?
  87. render :partial => 'common/markup', :locals => {:markup_text_formatting => 'textile', :markup_text => content}
  88. else
  89. render :partial => 'common/file', :locals => {:content => content, :filename => attachment.filename}
  90. end
  91. end
  92. end