Project

General

Profile

Feature #31472 » 31472-add-file-upload-v5.1.patch

de zhang, 2024-07-29 03:55

View differences:

app/controllers/documents_controller.rb
75 75
    @document.safe_attributes = params[:document]
76 76
    if @document.save
77 77
      flash[:notice] = l(:notice_successful_update)
78
      redirect_to document_path(@document)
78
      add_attachment
79 79
    else
80 80
      render :action => 'edit'
81 81
    end
app/models/document.rb
40 40
  )
41 41
  acts_as_activity_provider :scope => proc {preload(:project)}
42 42

  
43
  attr_writer :deleted_attachment_ids
44

  
43 45
  validates_presence_of :project, :title, :category
44 46
  validates_length_of :title, :maximum => 255
45 47

  
48
  after_save :delete_selected_attachments
46 49
  after_create_commit :send_notification
47 50

  
48 51
  scope :visible, (lambda do |*args|
......
51 54
  end)
52 55

  
53 56
  safe_attributes 'category_id', 'title', 'description', 'custom_fields', 'custom_field_values'
57
  safe_attributes(
58
    'deleted_attachment_ids',
59
    :if => lambda {|document, user| document.attachments_deletable?(user)}
60
  )
54 61

  
55 62
  def visible?(user=User.current)
56 63
    !user.nil? && user.allowed_to?(:view_documents, project)
......
75 82
    project.notified_users.reject {|user| !visible?(user)}
76 83
  end
77 84

  
85
  def deleted_attachment_ids
86
    Array(@deleted_attachment_ids).map(&:to_i)
87
  end
88

  
78 89
  private
79 90

  
91
  def delete_selected_attachments
92
    if deleted_attachment_ids.present?
93
      objects = attachments.where(:id => deleted_attachment_ids)
94
      attachments.delete(objects)
95
    end
96
  end
97

  
80 98
  def send_notification
81 99
    if Setting.notified_events.include?('document_added')
82 100
      Mailer.deliver_document_added(self, User.current)
app/views/documents/_form.html.erb
12 12
    <p><%= custom_field_tag_with_label :document, value %></p>
13 13
  <% end %>
14 14

  
15
  <% if @document.new_record? %>
16
    <p><label><%=l(:label_attachment_plural)%></label><%= render :partial => 'attachments/form', :locals => {:container => @document} %></p>
15
<fieldset>
16
<legend><%=l(:label_attachment_plural)%></legend>
17
<% if @document.attachments.any? && @document.safe_attribute?('deleted_attachment_ids') %>
18
<div class="contextual"><%= link_to l(:label_edit_attachments), '#', :onclick => "$('#existing-attachments').toggle(); return false;" %></div>
19
<div id="existing-attachments" style="<%= 'display:none;' if @document.deleted_attachment_ids.blank? %>">
20
  <% @document.attachments.each do |attachment| %>
21
  <span class="existing-attachment">
22
    <%= text_field_tag '', attachment.filename, :class => "icon icon-attachment filename", :disabled => true %>
23
    <label class='inline'>
24
      <%= check_box_tag 'document[deleted_attachment_ids][]',
25
                        attachment.id,
26
                        @document.deleted_attachment_ids.include?(attachment.id),
27
                        :id => nil, :class => "deleted_attachment" %>&nbsp;<%= l(:button_delete) %>
28
    </label>
29
  </span>
17 30
  <% end %>
31
  <hr />
32
</div>
33
<% end %>
34

  
35
<div id="new-attachments" style="display:inline-block;">
36
  <%= render :partial => 'attachments/form', :locals => {:container => @document} %>
37
</div>
38
</fieldset>
18 39
</div>
19 40

  
20 41
<%= wikitoolbar_for 'document_description' %>
test/functional/documents_controller_test.rb
224 224
  end
225 225

  
226 226
  def test_update
227
    set_tmp_attachments_directory
227 228
    @request.session[:user_id] = 2
228
    put(
229
      :update,
230
      :params => {
231
        :id => 1,
232
        :document => {
233
          :title => 'test_update'
229
    assert_difference 'Attachment.count' do
230
      put(
231
        :update,
232
        :params => {
233
          :id => 1,
234
          :document => {
235
            :title => 'test_update'
236
          },
237
          :attachments => {
238
            '1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}
239
          }
234 240
        }
235
      }
236
    )
241
      )
242
    end
237 243
    assert_redirected_to '/documents/1'
238 244
    document = Document.find(1)
239 245
    assert_equal 'test_update', document.title
240 246
  end
241 247

  
242 248
  def test_update_with_failure
249
    set_tmp_attachments_directory
243 250
    @request.session[:user_id] = 2
244
    put(
245
      :update,
246
      :params => {
247
        :id => 1,
248
        :document => {
249
          :title => ''
251
    assert_no_difference 'Attachment.count' do
252
      put(
253
        :update,
254
        :params => {
255
          :id => 1,
256
          :document => {
257
            :title => ''
258
          },
259
          :attachments => {
260
            '1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}
261
          }
250 262
        }
251
      }
252
    )
263
      )
264
    end
253 265
    assert_response :success
254 266
    assert_select_error /title cannot be blank/i
255 267
  end
256 268

  
269
  def test_update_with_deleted_attachment_ids
270
    set_tmp_attachments_directory
271
    @request.session[:user_id] = 2
272
    document = Document.find(1)
273
    attachment = document.attachments.first
274
    assert_difference 'Attachment.count', -1 do
275
      put(
276
        :update,
277
        :params => {
278
          :id => 1,
279
          :document => {
280
            :title => 'test_update',
281
            :deleted_attachment_ids => [attachment.id]
282
          }
283
        }
284
      )
285
    end
286
    document.reload
287
    assert_not_includes document.attachments, attachment
288
  end
289
  def test_update_with_deleted_attachment_ids_and_failure_should_preserve_selected_attachments
290
    set_tmp_attachments_directory
291
    @request.session[:user_id] = 2
292
    document = Document.find(1)
293
    attachment = document.attachments.first
294
    assert_no_difference 'Attachment.count' do
295
      put(
296
        :update,
297
        :params => {
298
          :id => 1,
299
          :document => {
300
            :title => '',
301
            :deleted_attachment_ids => [attachment.id]
302
          }
303
        }
304
      )
305
    end
306
    document.reload
307
    assert_includes document.attachments, attachment
308
  end
309

  
257 310
  def test_destroy
258 311
    set_tmp_attachments_directory
259 312
    @request.session[:user_id] = 2
(5-5/5)