Project

General

Profile

Feature #34862 » 34862.patch

Yuichi HARADA, 2021-03-16 08:55

View differences:

app/models/message.rb
: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
......
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|
......
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
......
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)
app/views/messages/_form.html.erb
<%= wikitoolbar_for 'message_content', preview_board_message_path(:board_id => @board, :id => @message) %>
<!--[eoform:message]-->
<p><%= l(:label_attachment_plural) %><br />
<%= render :partial => 'attachments/form', :locals => {:container => @message} %></p>
<fieldset>
<legend><%= l(:label_attachment_plural) %></legend>
<% if @message.attachments.any? && @message.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="<%= 'display:none;' if @message.deleted_attachment_ids.blank? %>">
<% @message.attachments.each do |attachment| -%>
<span class="existing-attachment">
<%= text_field_tag nil, attachment.filename, :id => nil, :class => 'filename', :disabled => true %>
<label>
<%= check_box_tag 'message[deleted_attachment_ids][]',
attachment.id,
@message.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', :locals => {:container => @message} %>
</div>
</fieldset>
</div>
test/functional/messages_controller_test.rb
class MessagesControllerTest < Redmine::ControllerTest
fixtures :projects, :users, :email_addresses, :user_preferences, :members, :member_roles, :roles, :boards, :messages, :enabled_modules,
:watchers
:watchers, :attachments
def setup
User.current = nil
......
assert_equal Board.find(2), message.board
end
def test_post_edit_with_deleted_attachment_ids
set_tmp_attachments_directory
@request.session[:user_id] = 2
message = Message.find(1)
attachment = message.attachments.first
assert_difference 'Attachment.count', -1 do
post(
:edit,
:params => {
:board_id => 1,
:id => 1,
:message => {
:subject => 'New subject',
:content => 'New body',
:deleted_attachment_ids => [attachment.id]
}
}
)
end
message.reload
assert_not_includes message.attachments, attachment
end
def test_reply
@request.session[:user_id] = 2
post(
(2-2/4)