Commit c0604fc6 authored by maeda's avatar maeda

Bulk delete wiki attachments (#16410).

Patch by Mizuki ISHIKAWA.


git-svn-id: https://svn.redmine.org/redmine/trunk@17453 e93f8b46-1217-0410-a6f0-8f06a7374b81
parent c8be63fe
......@@ -40,7 +40,7 @@ class WikiPage < ActiveRecord::Base
:permission => :view_wiki_pages,
:project_key => "#{Wiki.table_name}.project_id"
attr_accessor :redirect_existing_links
attr_accessor :redirect_existing_links, :deleted_attachment_ids
validates_presence_of :title
validates_format_of :title, :with => /\A[^,\.\/\?\;\|\s]*\z/
......@@ -51,7 +51,7 @@ class WikiPage < ActiveRecord::Base
validate :validate_parent_title
before_destroy :delete_redirects
before_save :handle_rename_or_move, :update_wiki_start_page
after_save :handle_children_move
after_save :handle_children_move, :delete_selected_attachments
# eager load information about last updates, without loading text
scope :with_updated_on, lambda { preload(:content_without_text) }
......@@ -65,6 +65,9 @@ class WikiPage < ActiveRecord::Base
safe_attributes 'is_start_page',
:if => lambda {|page, user| user.allowed_to?(:manage_wiki, page.project)}
safe_attributes 'deleted_attachment_ids',
:if => lambda {|page, user| page.attachments_deletable?(user)}
def initialize(attributes=nil, *args)
super
if new_record? && DEFAULT_PROTECTED_PAGES.include?(title.to_s.downcase)
......@@ -251,6 +254,17 @@ class WikiPage < ActiveRecord::Base
ret
end
def deleted_attachment_ids
Array(@deleted_attachment_ids).map(&:to_i)
end
def delete_selected_attachments
if deleted_attachment_ids.present?
objects = attachments.where(:id => deleted_attachment_ids.map(&:to_i))
attachments.delete(objects)
end
end
protected
def validate_parent_title
......
......@@ -30,7 +30,29 @@
<% end %>
<p><label><%= l(:field_comments) %></label><%= f.text_field :comments, :size => 120, :maxlength => 1024 %></p>
<p><label><%=l(:label_attachment_plural)%></label><%= render :partial => 'attachments/form' %></p>
<fieldset>
<legend><%=l(:label_attachment_plural)%></legend>
<% if @page.attachments.any? && @page.safe_attribute?('deleted_attachment_ids') %>
<div class="contextual"><%= link_to l(:label_edit_attachments), '#', :onclick => "$('#existing-attachments').toggle(); return false;" %></div>
<div id="existing-attachments" style="<%= @page.deleted_attachment_ids.blank? ? 'display:none;' : '' %>">
<% @page.attachments.each do |attachment| %>
<span class="existing-attachment">
<%= text_field_tag '', attachment.filename, :class => "icon icon-attachment filename", :disabled => true %>
<label class='inline'>
<%= check_box_tag 'wiki_page[deleted_attachment_ids][]',
attachment.id,
@page.deleted_attachment_ids.include?(attachment.id),
:id => nil, :class => "deleted_attachment" %> <%= l(:button_delete) %>
</label>
</span>
<% end %>
<hr />
</div>
<% end %>
<div id="new-attachments" style="display:inline-block;">
<%= render :partial => 'attachments/form' %>
</div>
</fieldset>
</div>
<p>
......
......@@ -459,6 +459,44 @@ class WikiControllerTest < Redmine::ControllerTest
assert_equal 1, page.content.version
end
def test_update_with_deleted_attachment_ids
@request.session[:user_id] = 2
page = WikiPage.find(4)
attachment = page.attachments.first
assert_difference 'Attachment.count', -1 do
put :update, :params => {
:project_id => page.wiki.project.id,
:id => page.title,
:content => {
:comments => 'delete file',
:text => 'edited'
},
:wiki_page => {:deleted_attachment_ids => [attachment.id]}
}
end
page.reload
refute_includes page.attachments, attachment
end
def test_update_with_deleted_attachment_ids_and_failure_should_preserve_selected_attachments
@request.session[:user_id] = 2
page = WikiPage.find(4)
attachment = page.attachments.first
assert_no_difference 'Attachment.count' do
put :update, :params => {
:project_id => page.wiki.project.id,
:id => page.title,
:content => {
:comments => 'a' * 1300, # failure here, comment is too long
:text => 'edited'
},
:wiki_page => {:deleted_attachment_ids => [attachment.id]}
}
end
page.reload
assert_includes page.attachments, attachment
end
def test_update_stale_page_should_not_raise_an_error
@request.session[:user_id] = 2
c = Wiki.find(1).find_page('Another_page').content
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment