Project

General

Profile

Feature #16410 » diff-r17405.patch

Mizuki ISHIKAWA, 2018-06-19 08:48

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

  
44
  attr_accessor :redirect_existing_links, :deleted_attachment_ids
44 45

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

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

  
68
  def initialize(attributes=nil, *args)
69
  safe_attributes 'deleted_attachment_ids',
70
    :if => lambda {|page, user| page.attachments_deletable?(user)}
71

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

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

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

  
254 269
  protected
255 270

  
256 271
  def validate_parent_title
app/views/wiki/edit.html.erb
16 16
<%= text_area_tag 'content[text]', @text, :cols => 100, :rows => 25,
17 17
                  :class => 'wiki-edit', :accesskey => accesskey(:edit) %>
18 18

  
19
<fieldset class='wiki-items'>
19 20
<% if @page.safe_attribute_names.include?('parent_id') && @wiki.pages.any? %>
20 21
  <%= fields_for @page do |fp| %>
21 22
    <p>
......
30 31
<% end %>
31 32

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

  
36 60
<p>
public/stylesheets/application.css
695 695
  width: 175px;
696 696
}
697 697

  
698
.tabular .wiki-items label {
699
  margin-left: -260px;
700
  width: 175px;
701
}
702

  
698 703
.tabular label.floating{
699 704
  font-weight: normal;
700 705
  margin-left: 0px;
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
(1-1/3)