]> source.dussan.org Git - redmine.git/commitdiff
Allows attachments on news (#1972).
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Sun, 29 Jan 2012 22:39:06 +0000 (22:39 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Sun, 29 Jan 2012 22:39:06 +0000 (22:39 +0000)
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@8728 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/controllers/news_controller.rb
app/models/news.rb
app/views/news/_form.html.erb
app/views/news/edit.html.erb
app/views/news/index.html.erb
app/views/news/new.html.erb
app/views/news/show.html.erb
test/functional/news_controller_test.rb
test/unit/news_test.rb

index 0d091396ac073ac349e16f323332080c957d4c9f..34d4a13cc43a7e94662470df2f661690e229f59d 100644 (file)
@@ -27,6 +27,7 @@ class NewsController < ApplicationController
   accept_api_auth :index
 
   helper :watchers
+  helper :attachments
 
   def index
     case params[:format]
@@ -70,6 +71,8 @@ class NewsController < ApplicationController
     if request.post?
       @news.attributes = params[:news]
       if @news.save
+        attachments = Attachment.attach_files(@news, params[:attachments])
+        render_attachment_warning_if_needed(@news)
         flash[:notice] = l(:notice_successful_create)
         redirect_to :controller => 'news', :action => 'index', :project_id => @project
       else
@@ -83,6 +86,8 @@ class NewsController < ApplicationController
 
   def update
     if request.put? and @news.update_attributes(params[:news])
+      attachments = Attachment.attach_files(@news, params[:attachments])
+      render_attachment_warning_if_needed(@news)
       flash[:notice] = l(:notice_successful_update)
       redirect_to :action => 'show', :id => @news
     else
index 763063d13f3fe4da2ec478f1c242a8b213d3bd1a..78dcc0c8c18b7df9635df777bbf15d571d28dcc6 100644 (file)
@@ -24,6 +24,7 @@ class News < ActiveRecord::Base
   validates_length_of :title, :maximum => 60
   validates_length_of :summary, :maximum => 255
 
+  acts_as_attachable :delete_permission => :manage_news
   acts_as_searchable :columns => ['title', 'summary', "#{table_name}.description"], :include => :project
   acts_as_event :url => Proc.new {|o| {:controller => 'news', :action => 'show', :id => o.id}}
   acts_as_activity_provider :find_options => {:include => [:project, :author]},
index a3eb87e364c842af05a0736787c76a05c32193e6..0d181a330fb9e14c3f70d8daea26466a13a02f03 100644 (file)
@@ -3,6 +3,7 @@
 <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' %></p>
+<p id="attachments_form"><%= label_tag('attachments[1][file]', l(:label_attachment_plural))%><%= render :partial => 'attachments/form' %></p>
 </div>
-
+               
 <%= wikitoolbar_for 'news_description' %>
index 182f22fbe87a5d2d98cea78499f34ac985e1c8c1..f00a024c1ad0759e05d73c11bf6cbc4ebe3bf1d0 100644 (file)
@@ -1,6 +1,6 @@
 <h2><%=l(:label_news)%></h2>
 
-<% labelled_form_for @news, :html => { :id => 'news-form', :method => :put } do |f| %>
+<% labelled_form_for @news, :html => { :id => 'news-form', :multipart => true, :method => :put } do |f| %>
 <%= render :partial => 'form', :locals => { :f => f } %>
 <%= submit_tag l(:button_save) %>
 <%= link_to_remote l(:label_preview),
index 4d13d3a32ae3c3f74d8ff885b48acde9f3f075fe..d0370c03f82e14cfd1aef6f0ff622b2c58201c4a 100644 (file)
@@ -8,7 +8,7 @@
 <div id="add-news" style="display:none;">
 <h2><%=l(:label_news_new)%></h2>
 <% labelled_form_for @news, :url => project_news_index_path(@project),
-                                           :html => { :id => 'news-form' } do |f| %>
+                                           :html => { :id => 'news-form', :multipart => true } do |f| %>
 <%= render :partial => 'news/form', :locals => { :f => f } %>
 <%= submit_tag l(:button_create) %>
 <%= link_to_remote l(:label_preview),
index e0092878ed07a7c787892e2234afbec1c911a45b..1c55215b7543c97d63444c881e3845bf37aee704 100644 (file)
@@ -1,7 +1,7 @@
 <h2><%=l(:label_news_new)%></h2>
 
 <% labelled_form_for @news, :url => project_news_index_path(@project),
-                                           :html => { :id => 'news-form' } do |f| %>
+                                           :html => { :id => 'news-form', :multipart => true } do |f| %>
   <%= render :partial => 'news/form', :locals => { :f => f } %>
   <%= submit_tag l(:button_create) %>
   <%= link_to_remote l(:label_preview),
index 2ffed80e0e37884b7ee92d3b4341f336b5802df0..72c18d9a6a0874f7fe1eba1bff0c137e3dfb3d5e 100644 (file)
@@ -17,7 +17,7 @@
 <% if authorize_for('news', 'edit') %>
 <div id="edit-news" style="display:none;">
 <% labelled_form_for :news, @news, :url => news_path(@news),
-                                           :html => { :id => 'news-form', :method => :put } do |f| %>
+                                           :html => { :id => 'news-form', :multipart => true, :method => :put } do |f| %>
 <%= render :partial => 'form', :locals => { :f => f } %>
 <%= submit_tag l(:button_save) %>
 <%= link_to_remote l(:label_preview),
@@ -35,8 +35,9 @@
 <p><% unless @news.summary.blank? %><em><%=h @news.summary %></em><br /><% end %>
 <span class="author"><%= authoring @news.created_on, @news.author %></span></p>
 <div class="wiki">
-<%= textilizable(@news.description) %>
+<%= textilizable(@news, :description) %>
 </div>
+<%= link_to_attachments @news %>
 <br />
 
 <div id="comments" style="margin-bottom:16px;">
index a996c002df1516fcfafd529899674345284e56f8..564f91bdf94d668977877f8839c4def22cfe5e62 100644 (file)
@@ -53,6 +53,16 @@ class NewsControllerTest < ActionController::TestCase
     assert_tag :tag => 'h2', :content => /eCookbook first release/
   end
 
+  def test_show_should_show_attachments
+    attachment = Attachment.first
+    attachment.container = News.find(1)
+    attachment.save!
+
+    get :show, :id => 1
+    assert_response :success
+    assert_tag 'a', :content => attachment.filename
+  end
+
   def test_show_not_found
     get :show, :id => 999
     assert_response 404
@@ -83,6 +93,34 @@ class NewsControllerTest < ActionController::TestCase
     assert_equal 1, ActionMailer::Base.deliveries.size
   end
 
+  def test_post_create_with_attachment
+    set_tmp_attachments_directory
+    @request.session[:user_id] = 2
+    assert_difference 'News.count' do
+      assert_difference 'Attachment.count' do
+        post :create, :project_id => 1,
+          :news => { :title => 'Test', :description => 'This is the description' },
+          :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
+      end
+    end
+    attachment = Attachment.first(:order => 'id DESC')
+    news = News.first(:order => 'id DESC')
+    assert_equal news, attachment.container
+  end
+
+  def test_post_create_with_validation_failure
+    @request.session[:user_id] = 2
+    post :create, :project_id => 1, :news => { :title => '',
+                                            :description => 'This is the description',
+                                            :summary => '' }
+    assert_response :success
+    assert_template 'new'
+    assert_not_nil assigns(:news)
+    assert assigns(:news).new_record?
+    assert_tag :tag => 'div', :attributes => { :id => 'errorExplanation' },
+                              :content => /1 error/
+  end
+
   def test_get_edit
     @request.session[:user_id] = 2
     get :edit, :id => 1
@@ -98,17 +136,18 @@ class NewsControllerTest < ActionController::TestCase
     assert_equal 'Description changed by test_post_edit', news.description
   end
 
-  def test_post_create_with_validation_failure
+  def test_put_update_with_attachment
+    set_tmp_attachments_directory
     @request.session[:user_id] = 2
-    post :create, :project_id => 1, :news => { :title => '',
-                                            :description => 'This is the description',
-                                            :summary => '' }
-    assert_response :success
-    assert_template 'new'
-    assert_not_nil assigns(:news)
-    assert assigns(:news).new_record?
-    assert_tag :tag => 'div', :attributes => { :id => 'errorExplanation' },
-                              :content => /1 error/
+    assert_no_difference 'News.count' do
+      assert_difference 'Attachment.count' do
+        put :update, :id => 1,
+          :news => { :description => 'This is the description' },
+          :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
+      end
+    end
+    attachment = Attachment.first(:order => 'id DESC')
+    assert_equal News.find(1), attachment.container
   end
 
   def test_destroy
index 31298225c8e3251bbb3e742fb2abc59df0c30697..fd0063efb52e16266a40d69ec8180aabe79406f0 100644 (file)
@@ -71,4 +71,19 @@ class NewsTest < ActiveSupport::TestCase
     10.times { projects(:projects_001).news.create(valid_news) }
     assert_equal 5, News.latest(users(:users_004)).size
   end
+
+  def test_attachments_should_be_visible
+    assert News.find(1).attachments_visible?(User.anonymous)
+  end
+
+  def test_attachments_should_be_deletable_with_manage_news_permission
+    manager = User.find(2)
+    assert News.find(1).attachments_deletable?(manager)
+  end
+
+  def test_attachments_should_not_be_deletable_without_manage_news_permission
+    manager = User.find(2)
+    Role.find_by_name('Manager').remove_permission!(:manage_news)
+    assert !News.find(1).attachments_deletable?(manager)
+  end
 end