diff --git a/app/models/news.rb b/app/models/news.rb index 575dabd73..7a2fbff87 100644 --- a/app/models/news.rb +++ b/app/models/news.rb @@ -36,7 +36,10 @@ class News < ActiveRecord::Base :author_key => :author_id acts_as_watchable + attr_writer :deleted_attachment_ids + after_create :add_author_as_watcher + after_save :delete_selected_attachments after_create_commit :send_notification scope :visible, (lambda do |*args| @@ -45,6 +48,13 @@ class News < ActiveRecord::Base end) safe_attributes 'title', 'summary', 'description' + safe_attributes( + 'deleted_attachment_ids', + :if => + lambda do |news, user| + news.attachments_deletable?(user) + end + ) def visible?(user=User.current) !user.nil? && user.allowed_to?(:view_news, project) @@ -85,12 +95,23 @@ class News < ActiveRecord::Base visible(user).preload(:author, :project).order("#{News.table_name}.created_on DESC").limit(count).to_a end + def deleted_attachment_ids + Array(@deleted_attachment_ids).map(&:to_i) + end + private def add_author_as_watcher Watcher.create(:watchable => self, :user => author) end + def delete_selected_attachments + if deleted_attachment_ids.present? + objects = attachments.where(:id => deleted_attachment_ids) + attachments.delete(objects) + end + end + def send_notification if Setting.notified_events.include?('news_added') Mailer.deliver_news_added(self) diff --git a/app/views/news/_form.html.erb b/app/views/news/_form.html.erb index a441b671d..75ac2e35e 100644 --- a/app/views/news/_form.html.erb +++ b/app/views/news/_form.html.erb @@ -1,6 +1,7 @@ <%= error_messages_for @news %> -
+
+

<%= f.text_field :title, :required => true, :size => 60 %>

<%= f.text_area :summary, :cols => 60, :rows => 2 %>

<%= f.text_area :description, :required => true, :cols => 60, :rows => 15, :class => 'wiki-edit', @@ -8,7 +9,30 @@ :auto_complete => true } %>

-

<%= render :partial => 'attachments/form', :locals => {:container => @news} %>

+
+
+ <%= l(:label_attachment_plural) %> +<% if @news.attachments.any? && @news.safe_attribute?('deleted_attachment_ids') -%> +
<%= link_to l(:label_edit_attachments), '#', :onclick => "$('#existing-attachments').toggle(); return false;" %>
+
+ <% @news.attachments.each do |attachment| -%> + + <%= text_field_tag nil, attachment.filename, :id => nil, :class => 'icon icon-attachment filename', :disabled => true %> + + + <% end -%> +
+
+<% end -%> +
+ <%= render :partial => 'attachments/form', :locals => {:container => @news} %> +
+
<%= wikitoolbar_for 'news_description', preview_news_path(:project_id => @project, :id => @news) %> diff --git a/test/functional/news_controller_test.rb b/test/functional/news_controller_test.rb index 76517e1a1..80f92af5b 100644 --- a/test/functional/news_controller_test.rb +++ b/test/functional/news_controller_test.rb @@ -224,6 +224,27 @@ class NewsControllerTest < Redmine::ControllerTest assert_equal News.find(1), attachment.container end + def test_put_update_with_deleted_attachment_ids + set_tmp_attachments_directory + news = News.find(1) + attachment = Attachment.generate!(:container => news) + assert_include attachment, news.attachments + @request.session[:user_id] = 2 + assert_difference 'Attachment.count', -1 do + put( + :update, + :params => { + :id => 1, + :news => { + :description => 'This is the description', + :deleted_attachment_ids => [attachment.id] + } + } + ) + end + assert_not_include attachment, news.attachments + end + def test_update_with_failure @request.session[:user_id] = 2 put(