diff --git a/app/models/message.rb b/app/models/message.rb index c6fe1dc85..205785414 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -51,6 +51,8 @@ class Message < ActiveRecord::Base :author_key => :author_id acts_as_watchable + attr_writer :deleted_attachment_ids + validates_presence_of :board, :subject, :content validates_length_of :subject, :maximum => 255 validate :cannot_reply_to_locked_topic, :on => :create @@ -58,6 +60,7 @@ class Message < ActiveRecord::Base after_create :add_author_as_watcher, :reset_counters! after_update :update_messages_board after_destroy :reset_counters! + after_save :delete_selected_attachments after_create_commit :send_notification scope :visible, (lambda do |*args| @@ -73,6 +76,14 @@ class Message < ActiveRecord::Base user.allowed_to?(:edit_messages, message.project) end ) + safe_attributes( + 'deleted_attachment_ids', + :if => + lambda do |message, user| + message.attachments_deletable?(user) + end + ) + def visible?(user=User.current) !user.nil? && user.allowed_to?(:view_messages, project) end @@ -121,12 +132,23 @@ class Message < ActiveRecord::Base project.notified_users.reject {|user| !visible?(user)} end + def deleted_attachment_ids + Array(@deleted_attachment_ids).map(&:to_i) + end + private def add_author_as_watcher Watcher.create(:watchable => self.root, :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?('message_posted') Mailer.deliver_message_posted(self) diff --git a/app/views/messages/_form.html.erb b/app/views/messages/_form.html.erb index e030627a2..db772d564 100644 --- a/app/views/messages/_form.html.erb +++ b/app/views/messages/_form.html.erb @@ -32,6 +32,27 @@ <%= wikitoolbar_for 'message_content', preview_board_message_path(:board_id => @board, :id => @message) %> -
<%= l(:label_attachment_plural) %>
-<%= render :partial => 'attachments/form', :locals => {:container => @message} %>