]> source.dussan.org Git - redmine.git/commitdiff
Download all attachments in a journal (#35462).
authorGo MAEDA <maeda@farend.jp>
Mon, 22 Nov 2021 08:42:44 +0000 (08:42 +0000)
committerGo MAEDA <maeda@farend.jp>
Mon, 22 Nov 2021 08:42:44 +0000 (08:42 +0000)
Patch by Takenori TAKAKI.

git-svn-id: http://svn.redmine.org/redmine/trunk@21292 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/controllers/attachments_controller.rb
app/controllers/journals_controller.rb
app/helpers/application_helper.rb
app/helpers/journals_helper.rb
app/models/journal.rb
test/helpers/application_helper_test.rb
test/unit/journal_test.rb

index 91cd7ce413339c6e5b3aaf81456fe784e77521ad..05561f015da17894b7c7eeba4ebb75cb78993a87 100644 (file)
@@ -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
index 95f47e360b24bc6d6190504dd6f9c867d478eb24..c2674a4c4244c344c6cc3b6c5c5ae1ba3e2b9342 100644 (file)
@@ -28,6 +28,7 @@ class JournalsController < ApplicationController
   helper :issues
   helper :custom_fields
   helper :queries
+  helper :attachments
   include QueriesHelper
 
   def index
index 03fb26d4ca50c28b696ddd93ecf50734ad5c710a..a798261bcb7f367fb2a6eeb9cfbc9f0535bcbc23 100644 (file)
@@ -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
index d95d2b77b146231b35dee7f5ae601910355d81e2..927cdb6847c1228e222cbd47c96d9002ba3e9fd3 100644 (file)
@@ -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),
index a608703d7f948ca789302ffa94f0a723e3e514c9..f42394bc91c027f1020bc0bcf5162ce0c40497b2 100644 (file)
@@ -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
index 108b0dfae5ba5dc5fc1cd77f051178eacb9e0796..6e79dce2e9149052bd45348ee723465bc54f18cd 100644 (file)
@@ -184,6 +184,22 @@ class ApplicationHelperTest < Redmine::HelperTest
     to_test.each {|text, result| assert_equal "<p>#{result}</p>", 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?("<img src=\"/attachments/download/#{attachment_1.id}/attached_on_issue.png\" alt=\"\" />")
+    assert textilizable(raw, :object => journal).include?("<img src=\"/attachments/download/#{attachment_2.id}/attached_on_journal.png\" alt=\"\" />")
+  end
+
   def test_attached_images_with_textile_and_non_ascii_filename
     to_test = {
       'CAFÉ.JPG' => 'CAF%C3%89.JPG',
index 096240734826c7a517e0b4030c34661866d56f7f..345d64aba5efcb59ef160c43c49cd1b53b0af5fa 100644 (file)
@@ -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