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} %>
+