Project

General

Profile

Feature #34862 » 34862.patch

Yuichi HARADA, 2021-03-16 08:55

View differences:

app/models/message.rb
51 51
                            :author_key => :author_id
52 52
  acts_as_watchable
53 53

  
54
  attr_writer :deleted_attachment_ids
55

  
54 56
  validates_presence_of :board, :subject, :content
55 57
  validates_length_of :subject, :maximum => 255
56 58
  validate :cannot_reply_to_locked_topic, :on => :create
......
58 60
  after_create :add_author_as_watcher, :reset_counters!
59 61
  after_update :update_messages_board
60 62
  after_destroy :reset_counters!
63
  after_save :delete_selected_attachments
61 64
  after_create_commit :send_notification
62 65

  
63 66
  scope :visible, (lambda do |*args|
......
73 76
        user.allowed_to?(:edit_messages, message.project)
74 77
      end
75 78
  )
79
  safe_attributes(
80
    'deleted_attachment_ids',
81
    :if =>
82
      lambda do |message, user|
83
        message.attachments_deletable?(user)
84
      end
85
  )
86

  
76 87
  def visible?(user=User.current)
77 88
    !user.nil? && user.allowed_to?(:view_messages, project)
78 89
  end
......
121 132
    project.notified_users.reject {|user| !visible?(user)}
122 133
  end
123 134

  
135
  def deleted_attachment_ids
136
    Array(@deleted_attachment_ids).map(&:to_i)
137
  end
138

  
124 139
  private
125 140

  
126 141
  def add_author_as_watcher
127 142
    Watcher.create(:watchable => self.root, :user => author)
128 143
  end
129 144

  
145
  def delete_selected_attachments
146
    if deleted_attachment_ids.present?
147
      objects = attachments.where(:id => deleted_attachment_ids)
148
      attachments.delete(objects)
149
    end
150
  end
151

  
130 152
  def send_notification
131 153
    if Setting.notified_events.include?('message_posted')
132 154
      Mailer.deliver_message_posted(self)
app/views/messages/_form.html.erb
32 32
<%= wikitoolbar_for 'message_content', preview_board_message_path(:board_id => @board, :id => @message) %>
33 33
<!--[eoform:message]-->
34 34

  
35
<p><%= l(:label_attachment_plural) %><br />
36
<%= render :partial => 'attachments/form', :locals => {:container => @message} %></p>
35
<fieldset>
36
  <legend><%= l(:label_attachment_plural) %></legend>
37
<% if @message.attachments.any? && @message.safe_attribute?('deleted_attachment_ids') -%>
38
  <div class="contextual"><%= link_to l(:label_edit_attachments), '#', :onclick => "$('#existing-attachments').toggle(); return false;" %></div>
39
  <div id="existing-attachments" style="<%= 'display:none;' if @message.deleted_attachment_ids.blank? %>">
40
  <% @message.attachments.each do |attachment| -%>
41
    <span class="existing-attachment">
42
      <%= text_field_tag nil, attachment.filename, :id => nil, :class => 'filename', :disabled => true %>
43
      <label>
44
        <%= check_box_tag 'message[deleted_attachment_ids][]',
45
                          attachment.id,
46
                          @message.deleted_attachment_ids.include?(attachment.id),
47
                          :id => nil, :class => 'deleted_attachment' %> <%= l(:button_delete) %>
48
      </label>
49
    </span>
50
  <% end -%>
51
    <hr />
52
  </div>
53
<% end -%>
54
  <div id="new-attachments" style="display:inline-block;">
55
    <%= render :partial => 'attachments/form', :locals => {:container => @message} %>
56
  </div>
57
</fieldset>
37 58
</div>
test/functional/messages_controller_test.rb
21 21

  
22 22
class MessagesControllerTest < Redmine::ControllerTest
23 23
  fixtures :projects, :users, :email_addresses, :user_preferences, :members, :member_roles, :roles, :boards, :messages, :enabled_modules,
24
           :watchers
24
           :watchers, :attachments
25 25

  
26 26
  def setup
27 27
    User.current = nil
......
235 235
    assert_equal Board.find(2), message.board
236 236
  end
237 237

  
238
  def test_post_edit_with_deleted_attachment_ids
239
    set_tmp_attachments_directory
240
    @request.session[:user_id] = 2
241
    message = Message.find(1)
242
    attachment = message.attachments.first
243
    assert_difference 'Attachment.count', -1 do
244
      post(
245
        :edit,
246
        :params => {
247
          :board_id => 1,
248
          :id => 1,
249
          :message => {
250
            :subject => 'New subject',
251
            :content => 'New body',
252
            :deleted_attachment_ids => [attachment.id]
253
          }
254
        }
255
      )
256
    end
257
    message.reload
258
    assert_not_includes message.attachments, attachment
259
  end
260

  
238 261
  def test_reply
239 262
    @request.session[:user_id] = 2
240 263
    post(
(2-2/4)