Feature #34861 » 34861.patch
| app/models/news.rb | ||
|---|---|---|
|
: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|
|
||
| ... | ... | |
|
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)
|
||
| ... | ... | |
|
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)
|
||
| app/views/news/_form.html.erb | ||
|---|---|---|
|
<%= error_messages_for @news %>
|
||
|
<div class="box tabular">
|
||
|
<div class="box">
|
||
|
<div class="tabular">
|
||
|
<p><%= f.text_field :title, :required => true, :size => 60 %></p>
|
||
|
<p><%= f.text_area :summary, :cols => 60, :rows => 2 %></p>
|
||
|
<p><%= f.text_area :description, :required => true, :cols => 60, :rows => 15, :class => 'wiki-edit',
|
||
| ... | ... | |
|
:auto_complete => true
|
||
|
}
|
||
|
%></p>
|
||
|
<p id="attachments_form"><label><%= l(:label_attachment_plural) %></label><%= render :partial => 'attachments/form', :locals => {:container => @news} %></p>
|
||
|
</div>
|
||
|
<fieldset>
|
||
|
<legend><%= l(:label_attachment_plural) %></legend>
|
||
|
<% if @news.attachments.any? && @news.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 @news.deleted_attachment_ids.blank? %>">
|
||
|
<% @news.attachments.each do |attachment| -%>
|
||
|
<span class="existing-attachment">
|
||
|
<%= text_field_tag nil, attachment.filename, :id => nil, :class => 'icon icon-attachment filename', :disabled => true %>
|
||
|
<label>
|
||
|
<%= check_box_tag 'news[deleted_attachment_ids][]',
|
||
|
attachment.id,
|
||
|
@news.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 => @news} %>
|
||
|
</div>
|
||
|
</fieldset>
|
||
|
</div>
|
||
|
<%= wikitoolbar_for 'news_description', preview_news_path(:project_id => @project, :id => @news) %>
|
||
| test/functional/news_controller_test.rb | ||
|---|---|---|
|
assert_equal News.find(1), attachment.container
|
||
|
end
|
||
|
def test_put_update_with_deleted_attachment_ids
|
||
|
set_tmp_attachments_directory
|
||
|
news = News.find(1)
|
||
|
attachment = Attachment.generate!(:container => news)
|
||
|
assert_include attachment, news.attachments
|
||
|
@request.session[:user_id] = 2
|
||
|
assert_difference 'Attachment.count', -1 do
|
||
|
put(
|
||
|
:update,
|
||
|
:params => {
|
||
|
:id => 1,
|
||
|
:news => {
|
||
|
:description => 'This is the description',
|
||
|
:deleted_attachment_ids => [attachment.id]
|
||
|
}
|
||
|
}
|
||
|
)
|
||
|
end
|
||
|
assert_not_include attachment, news.attachments
|
||
|
end
|
||
|
def test_update_with_failure
|
||
|
@request.session[:user_id] = 2
|
||
|
put(
|
||
- « Previous
- 1
- 2
- Next »