Project

General

Profile

Feature #34861 » 34861.patch

Yuichi HARADA, 2021-03-18 06:55

View differences:

app/models/news.rb
36 36
                            :author_key => :author_id
37 37
  acts_as_watchable
38 38

  
39
  attr_writer :deleted_attachment_ids
40

  
39 41
  after_create :add_author_as_watcher
42
  after_save :delete_selected_attachments
40 43
  after_create_commit :send_notification
41 44

  
42 45
  scope :visible, (lambda do |*args|
......
45 48
  end)
46 49

  
47 50
  safe_attributes 'title', 'summary', 'description'
51
  safe_attributes(
52
    'deleted_attachment_ids',
53
    :if =>
54
      lambda do |news, user|
55
        news.attachments_deletable?(user)
56
      end
57
  )
48 58

  
49 59
  def visible?(user=User.current)
50 60
    !user.nil? && user.allowed_to?(:view_news, project)
......
85 95
    visible(user).preload(:author, :project).order("#{News.table_name}.created_on DESC").limit(count).to_a
86 96
  end
87 97

  
98
  def deleted_attachment_ids
99
    Array(@deleted_attachment_ids).map(&:to_i)
100
  end
101

  
88 102
  private
89 103

  
90 104
  def add_author_as_watcher
91 105
    Watcher.create(:watchable => self, :user => author)
92 106
  end
93 107

  
108
  def delete_selected_attachments
109
    if deleted_attachment_ids.present?
110
      objects = attachments.where(:id => deleted_attachment_ids)
111
      attachments.delete(objects)
112
    end
113
  end
114

  
94 115
  def send_notification
95 116
    if Setting.notified_events.include?('news_added')
96 117
      Mailer.deliver_news_added(self)
app/views/news/_form.html.erb
1 1
<%= error_messages_for @news %>
2 2

  
3
<div class="box tabular">
3
<div class="box">
4
<div class="tabular">
4 5
<p><%= f.text_field :title, :required => true, :size => 60 %></p>
5 6
<p><%= f.text_area :summary, :cols => 60, :rows => 2 %></p>
6 7
<p><%= f.text_area :description, :required => true, :cols => 60, :rows => 15, :class => 'wiki-edit',
......
8 9
                       :auto_complete => true
9 10
                   }
10 11
%></p>
11
<p id="attachments_form"><label><%= l(:label_attachment_plural) %></label><%= render :partial => 'attachments/form', :locals => {:container => @news} %></p>
12
</div>
13
<fieldset>
14
  <legend><%= l(:label_attachment_plural) %></legend>
15
<% if @news.attachments.any? && @news.safe_attribute?('deleted_attachment_ids') -%>
16
  <div class="contextual"><%= link_to l(:label_edit_attachments), '#', :onclick => "$('#existing-attachments').toggle(); return false;" %></div>
17
  <div id="existing-attachments" style="<%= 'display:none;' if @news.deleted_attachment_ids.blank? %>">
18
  <% @news.attachments.each do |attachment| -%>
19
    <span class="existing-attachment">
20
      <%= text_field_tag nil, attachment.filename, :id => nil, :class => 'icon icon-attachment filename', :disabled => true %>
21
      <label>
22
        <%= check_box_tag 'news[deleted_attachment_ids][]',
23
                          attachment.id,
24
                          @news.deleted_attachment_ids.include?(attachment.id),
25
                          :id => nil, :class => 'deleted_attachment' %> <%= l(:button_delete) %>
26
      </label>
27
    </span>
28
  <% end -%>
29
    <hr />
30
  </div>
31
<% end -%>
32
  <div id="new-attachments" style="display:inline-block;">
33
    <%= render :partial => 'attachments/form', :locals => {:container => @news} %>
34
   </div>
35
</fieldset>
12 36
</div>
13 37

  
14 38
<%= wikitoolbar_for 'news_description', preview_news_path(:project_id => @project, :id => @news) %>
test/functional/news_controller_test.rb
224 224
    assert_equal News.find(1), attachment.container
225 225
  end
226 226

  
227
  def test_put_update_with_deleted_attachment_ids
228
    set_tmp_attachments_directory
229
    news = News.find(1)
230
    attachment = Attachment.generate!(:container => news)
231
    assert_include attachment, news.attachments
232
    @request.session[:user_id] = 2
233
    assert_difference 'Attachment.count', -1 do
234
      put(
235
        :update,
236
        :params => {
237
          :id => 1,
238
          :news => {
239
            :description => 'This is the description',
240
            :deleted_attachment_ids => [attachment.id]
241
          }
242
        }
243
      )
244
    end
245
    assert_not_include attachment, news.attachments
246
  end
247

  
227 248
  def test_update_with_failure
228 249
    @request.session[:user_id] = 2
229 250
    put(
(2-2/2)