Project

General

Profile

Feature #16410 » 16410.patch

Slightly updated Mizuki ISHIKAWA's patch - Go MAEDA, 2018-07-18 15:18

View differences:

app/models/wiki_page.rb
40 40
                     :permission => :view_wiki_pages,
41 41
                     :project_key => "#{Wiki.table_name}.project_id"
42 42

  
43
  attr_accessor :redirect_existing_links
43
  attr_accessor :redirect_existing_links, :deleted_attachment_ids
44 44

  
45 45
  validates_presence_of :title
46 46
  validates_format_of :title, :with => /\A[^,\.\/\?\;\|\s]*\z/
......
51 51
  validate :validate_parent_title
52 52
  before_destroy :delete_redirects
53 53
  before_save :handle_rename_or_move, :update_wiki_start_page
54
  after_save :handle_children_move
54
  after_save :handle_children_move, :delete_selected_attachments
55 55

  
56 56
  # eager load information about last updates, without loading text
57 57
  scope :with_updated_on, lambda { preload(:content_without_text) }
......
65 65
  safe_attributes 'is_start_page',
66 66
    :if => lambda {|page, user| user.allowed_to?(:manage_wiki, page.project)}
67 67

  
68
  safe_attributes 'deleted_attachment_ids',
69
    :if => lambda {|page, user| page.attachments_deletable?(user)}
70

  
68 71
  def initialize(attributes=nil, *args)
69 72
    super
70 73
    if new_record? && DEFAULT_PROTECTED_PAGES.include?(title.to_s.downcase)
......
251 254
    ret
252 255
  end
253 256

  
257
  def deleted_attachment_ids
258
    Array(@deleted_attachment_ids).map(&:to_i)
259
  end
260

  
261
  def delete_selected_attachments
262
    if deleted_attachment_ids.present?
263
      objects = attachments.where(:id => deleted_attachment_ids.map(&:to_i))
264
      attachments.delete(objects)
265
    end
266
  end
267

  
254 268
  protected
255 269

  
256 270
  def validate_parent_title
app/views/wiki/edit.html.erb
30 30
<% end %>
31 31

  
32 32
<p><label><%= l(:field_comments) %></label><%= f.text_field :comments, :size => 120, :maxlength => 1024 %></p>
33
<p><label><%=l(:label_attachment_plural)%></label><%= render :partial => 'attachments/form' %></p>
33
<fieldset>
34
<legend><%=l(:label_attachment_plural)%></legend>
35
<% if @page.attachments.any? && @page.safe_attribute?('deleted_attachment_ids') %>
36
<div class="contextual"><%= link_to l(:label_edit_attachments), '#', :onclick => "$('#existing-attachments').toggle(); return false;" %></div>
37
<div id="existing-attachments" style="<%= @page.deleted_attachment_ids.blank? ? 'display:none;' : '' %>">
38
  <% @page.attachments.each do |attachment| %>
39
  <span class="existing-attachment">
40
    <%= text_field_tag '', attachment.filename, :class => "icon icon-attachment filename", :disabled => true %>
41
    <label class='inline'>
42
      <%= check_box_tag 'wiki_page[deleted_attachment_ids][]',
43
                        attachment.id,
44
                        @page.deleted_attachment_ids.include?(attachment.id),
45
                        :id => nil, :class => "deleted_attachment" %> <%= l(:button_delete) %>
46
    </label>
47
  </span>
48
  <% end %>
49
  <hr />
50
</div>
51
<% end %>
52
<div id="new-attachments" style="display:inline-block;">
53
  <%= render :partial => 'attachments/form' %>
54
</div>
55
</fieldset>
34 56
</div>
35 57

  
36 58
<p>
test/functional/wiki_controller_test.rb
459 459
    assert_equal 1, page.content.version
460 460
  end
461 461

  
462
  def test_update_with_deleted_attachment_ids
463
    @request.session[:user_id] = 2
464
    page = WikiPage.find(4)
465
    attachment = page.attachments.first
466
    assert_difference 'Attachment.count', -1 do
467
      put :update, :params => {
468
        :project_id => page.wiki.project.id,
469
        :id => page.title,
470
        :content => {
471
          :comments => 'delete file',  # failure here, comment is too long
472
          :text => 'edited'
473
        },
474
        :wiki_page => {:deleted_attachment_ids => [attachment.id]}
475
      }
476
    end
477
    page.reload
478
    refute_includes page.attachments, attachment
479
  end
480

  
481
  def test_update_with_deleted_attachment_ids_and_failure_should_preserve_selected_attachments
482
    @request.session[:user_id] = 2
483
    page = WikiPage.find(4)
484
    attachment = page.attachments.first
485
    assert_no_difference 'Attachment.count' do
486
      put :update, :params => {
487
        :project_id => page.wiki.project.id,
488
        :id => page.title,
489
        :content => {
490
          :comments => 'a' * 1300,  # failure here, comment is too long
491
          :text => 'edited'
492
        },
493
        :wiki_page => {:deleted_attachment_ids => [attachment.id]}
494
      }
495
    end
496
    page.reload
497
    assert_includes page.attachments, attachment
498
  end
499

  
462 500
  def test_update_stale_page_should_not_raise_an_error
463 501
    @request.session[:user_id] = 2
464 502
    c = Wiki.find(1).find_page('Another_page').content
(3-3/3)