From: Go MAEDA Date: Mon, 22 Nov 2021 08:42:44 +0000 (+0000) Subject: Download all attachments in a journal (#35462). X-Git-Tag: 5.0.0~174 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=0a5ef25e87f7896a70bfd86302e6e8995b518f98;p=redmine.git Download all attachments in a journal (#35462). Patch by Takenori TAKAKI. git-svn-id: http://svn.redmine.org/redmine/trunk@21292 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- diff --git a/app/controllers/attachments_controller.rb b/app/controllers/attachments_controller.rb index 91cd7ce41..05561f015 100644 --- a/app/controllers/attachments_controller.rb +++ b/app/controllers/attachments_controller.rb @@ -225,7 +225,7 @@ class AttachmentsController < ApplicationController rescue nil end - unless klass && klass.reflect_on_association(:attachments) + unless klass && (klass.reflect_on_association(:attachments) || klass.method_defined?(:attachments)) render_404 return end diff --git a/app/controllers/journals_controller.rb b/app/controllers/journals_controller.rb index 95f47e360..c2674a4c4 100644 --- a/app/controllers/journals_controller.rb +++ b/app/controllers/journals_controller.rb @@ -28,6 +28,7 @@ class JournalsController < ApplicationController helper :issues helper :custom_fields helper :queries + helper :attachments include QueriesHelper def index diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 03fb26d4c..a798261bc 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -931,7 +931,11 @@ module ApplicationHelper # when using an image link, try to use an attachment, if possible attachments = options[:attachments] || [] - attachments += obj.attachments if obj.respond_to?(:attachments) + if obj.is_a?(Journal) + attachments += obj.journalized.attachments if obj.journalized.respond_to?(:attachments) + else + attachments += obj.attachments if obj.respond_to?(:attachments) + end if attachments.present? text.gsub!(/src="([^\/"]+\.(bmp|gif|jpg|jpe|jpeg|png))"(\s+alt="([^"]*)")?/i) do |m| filename, ext, alt, alttext = $1, $2, $3, $4 diff --git a/app/helpers/journals_helper.rb b/app/helpers/journals_helper.rb index d95d2b77b..927cdb684 100644 --- a/app/helpers/journals_helper.rb +++ b/app/helpers/journals_helper.rb @@ -32,6 +32,14 @@ module JournalsHelper indice = journal.indice || @journal.issue.visible_journals_with_index.find{|j| j.id == @journal.id}.indice dropbown_links << copy_object_url_link(issue_url(issue, anchor: "note-#{indice}", only_path: false)) + if journal.attachments.size > 1 + dropbown_links << link_to(l(:label_download_all_attachments), + container_attachments_download_path(journal), + :title => l(:label_download_all_attachments), + :class => 'icon icon-download' + ) + end + if journal.notes.present? if options[:reply_links] links << link_to(l(:button_quote), diff --git a/app/models/journal.rb b/app/models/journal.rb index a608703d7..f42394bc9 100644 --- a/app/models/journal.rb +++ b/app/models/journal.rb @@ -138,7 +138,7 @@ class Journal < ActiveRecord::Base end def attachments - journalized.respond_to?(:attachments) ? journalized.attachments : [] + details.select{ |d| d.property == 'attachment' }.map{ |d| Attachment.find_by(:id => d.prop_key) }.compact end # Returns a string of css classes diff --git a/test/helpers/application_helper_test.rb b/test/helpers/application_helper_test.rb index 108b0dfae..6e79dce2e 100644 --- a/test/helpers/application_helper_test.rb +++ b/test/helpers/application_helper_test.rb @@ -184,6 +184,22 @@ class ApplicationHelperTest < Redmine::HelperTest to_test.each {|text, result| assert_equal "

#{result}

", textilizable(text, :attachments => attachments)} end + def test_attached_images_on_issue + issue = Issue.generate! + attachment_1 = Attachment.generate!(:file => mock_file_with_options(:original_filename => "attached_on_issue.png"), :container => issue) + journal = issue.init_journal(User.find(2), issue) + attachment_2 = Attachment.generate!(:file => mock_file_with_options(:original_filename => "attached_on_journal.png"), :container => issue) + journal.journalize_attachment(attachment_2, :added) + + raw = <<~RAW + !attached_on_issue.png! + !attached_on_journal.png!' + RAW + + assert textilizable(raw, :object => journal).include?("\"\"") + assert textilizable(raw, :object => journal).include?("\"\"") + end + def test_attached_images_with_textile_and_non_ascii_filename to_test = { 'CAFÉ.JPG' => 'CAF%C3%89.JPG', diff --git a/test/unit/journal_test.rb b/test/unit/journal_test.rb index 096240734..345d64aba 100644 --- a/test/unit/journal_test.rb +++ b/test/unit/journal_test.rb @@ -222,4 +222,18 @@ class JournalTest < ActiveSupport::TestCase visible_details = journal.visible_details(User.find(2)) assert_equal 2, visible_details.size end + + def test_attachments + journal = Journal.new + [0, 1].map{ |i| Attachment.generate!(:file => mock_file_with_options(:original_filename => "image#{i}.png")) }.each do |attachment| + journal.details << JournalDetail.new(:property => 'attachment', :prop_key => attachment.id, :value => attachment.filename) + end + + attachments = journal.attachments + assert_equal 2, attachments.size + attachments.each_with_index do |attachment, i| + assert_kind_of Attachment, attachment + assert_equal "image#{i}.png", attachment.filename + end + end end