35462-2.patch

Takenori TAKAKI, 2021-07-28 08:44

Download (5.72 KB)

View differences:

app/controllers/attachments_controller.rb
225 225
      rescue
226 226
        nil
227 227
      end
228
    unless klass && klass.reflect_on_association(:attachments)
228
    unless klass && (klass.reflect_on_association(:attachments) || klass.method_defined?(:attachments))
229 229
      render_404
230 230
      return
231 231
    end
app/controllers/journals_controller.rb
28 28
  helper :issues
29 29
  helper :custom_fields
30 30
  helper :queries
31
  helper :attachments
31 32
  include QueriesHelper
32 33

  
33 34
  def index
app/helpers/application_helper.rb
930 930

  
931 931
    # when using an image link, try to use an attachment, if possible
932 932
    attachments = options[:attachments] || []
933
    attachments += obj.attachments if obj.respond_to?(:attachments)
933
    if obj.is_a?(Journal)
934
      attachments += obj.journalized.attachments if obj.journalized.respond_to?(:attachments)
935
    else
936
      attachments += obj.attachments if obj.respond_to?(:attachments)
937
    end
934 938
    if attachments.present?
935 939
      text.gsub!(/src="([^\/"]+\.(bmp|gif|jpg|jpe|jpeg|png))"(\s+alt="([^"]*)")?/i) do |m|
936 940
        filename, ext, alt, alttext = $1, $2, $3, $4
app/models/journal.rb
138 138
  end
139 139

  
140 140
  def attachments
141
    journalized.respond_to?(:attachments) ? journalized.attachments : []
141
    details.select{ |d| d.property == 'attachment' }.map{ |d| Attachment.find_by(:id => d.prop_key) }.compact
142 142
  end
143 143

  
144 144
  # Returns a string of css classes
app/views/issues/tabs/_history.html.erb
18 18
    </h4>
19 19

  
20 20
    <% if journal.details.any? %>
21
    <ul class="details">
22
      <% details_to_strings(journal.visible_details).each do |string| %>
23
       <li><%= string %></li>
24
      <% end %>
25
    </ul>
21
      <div class="note-details">
22
        <% if journal.attachments.size > 1 %>
23
          <div class="contextual">
24
            <%= link_to(l(:label_download_all_attachments),
25
                          container_attachments_download_path(journal),
26
                          :title => l(:label_download_all_attachments),
27
                          :class => 'icon-only icon-download') %>
28
          </div>
29
        <% end %>
30
        <ul class="details">
31
          <% details_to_strings(journal.visible_details).each do |string| %>
32
           <li><%= string %></li>
33
          <% end %>
34
        </ul>
35
      </div>
26 36
    <% if Setting.thumbnails_enabled? && (thumbnail_attachments = journal_thumbnail_attachments(journal)).any? %>
27 37
      <div class="thumbnails">
28 38
        <% thumbnail_attachments.each do |attachment| %>
test/helpers/application_helper_test.rb
182 182
    to_test.each {|text, result| assert_equal "<p>#{result}</p>", textilizable(text, :attachments => attachments)}
183 183
  end
184 184

  
185
  def test_attached_images_on_issue
186
    issue = Issue.generate!
187
    attachment_1 = Attachment.generate!(:file => mock_file_with_options(:original_filename => "attached_on_issue.png"), :container => issue)
188
    journal = issue.init_journal(User.find(2), issue)
189
    attachment_2 = Attachment.generate!(:file => mock_file_with_options(:original_filename => "attached_on_journal.png"), :container => issue)
190
    journal.journalize_attachment(attachment_2, :added)
191

  
192
    raw = <<~RAW
193
      !attached_on_issue.png!
194
      !attached_on_journal.png!
195
    RAW
196

  
197
    assert textilizable(raw, :object => journal).include?("<img src=\"/attachments/download/#{attachment_1.id}/attached_on_issue.png\" alt=\"\" />")
198
    assert textilizable(raw, :object => journal).include?("<img src=\"/attachments/download/#{attachment_2.id}/attached_on_journal.png\" alt=\"\" />")
199
  end
200

  
185 201
  def test_attached_images_with_textile_and_non_ascii_filename
186 202
    to_test = {
187 203
      'CAFÉ.JPG' => 'CAF%C3%89.JPG',
test/unit/journal_test.rb
222 222
    visible_details = journal.visible_details(User.find(2))
223 223
    assert_equal 2, visible_details.size
224 224
  end
225

  
226
  def test_attachments
227
    journal = Journal.new
228
    2.times.map{ |i| Attachment.generate!(:file => mock_file_with_options(:original_filename => "image#{i}.png")) }.each do |attachment|
229
      journal.details << JournalDetail.new(:property => 'attachment', :prop_key => attachment.id, :value => attachment.filename)
230
    end
231

  
232
    attachments = journal.attachments
233
    assert_equal 2, attachments.size
234
    attachments.each_with_index do |attachment, i|
235
      assert_kind_of Attachment, attachment
236
      assert_equal "image#{i}.png", attachment.filename
237
    end
238
  end
225 239
end