##// END OF EJS Templates
Allows attachments on news (#1972)....
Jean-Philippe Lang -
r8608:24138187eb13
parent child
Show More
@@ -27,6 +27,7 class NewsController < ApplicationController
27 accept_api_auth :index
27 accept_api_auth :index
28
28
29 helper :watchers
29 helper :watchers
30 helper :attachments
30
31
31 def index
32 def index
32 case params[:format]
33 case params[:format]
@@ -70,6 +71,8 class NewsController < ApplicationController
70 if request.post?
71 if request.post?
71 @news.attributes = params[:news]
72 @news.attributes = params[:news]
72 if @news.save
73 if @news.save
74 attachments = Attachment.attach_files(@news, params[:attachments])
75 render_attachment_warning_if_needed(@news)
73 flash[:notice] = l(:notice_successful_create)
76 flash[:notice] = l(:notice_successful_create)
74 redirect_to :controller => 'news', :action => 'index', :project_id => @project
77 redirect_to :controller => 'news', :action => 'index', :project_id => @project
75 else
78 else
@@ -83,6 +86,8 class NewsController < ApplicationController
83
86
84 def update
87 def update
85 if request.put? and @news.update_attributes(params[:news])
88 if request.put? and @news.update_attributes(params[:news])
89 attachments = Attachment.attach_files(@news, params[:attachments])
90 render_attachment_warning_if_needed(@news)
86 flash[:notice] = l(:notice_successful_update)
91 flash[:notice] = l(:notice_successful_update)
87 redirect_to :action => 'show', :id => @news
92 redirect_to :action => 'show', :id => @news
88 else
93 else
@@ -24,6 +24,7 class News < ActiveRecord::Base
24 validates_length_of :title, :maximum => 60
24 validates_length_of :title, :maximum => 60
25 validates_length_of :summary, :maximum => 255
25 validates_length_of :summary, :maximum => 255
26
26
27 acts_as_attachable :delete_permission => :manage_news
27 acts_as_searchable :columns => ['title', 'summary', "#{table_name}.description"], :include => :project
28 acts_as_searchable :columns => ['title', 'summary', "#{table_name}.description"], :include => :project
28 acts_as_event :url => Proc.new {|o| {:controller => 'news', :action => 'show', :id => o.id}}
29 acts_as_event :url => Proc.new {|o| {:controller => 'news', :action => 'show', :id => o.id}}
29 acts_as_activity_provider :find_options => {:include => [:project, :author]},
30 acts_as_activity_provider :find_options => {:include => [:project, :author]},
@@ -3,6 +3,7
3 <p><%= f.text_field :title, :required => true, :size => 60 %></p>
3 <p><%= f.text_field :title, :required => true, :size => 60 %></p>
4 <p><%= f.text_area :summary, :cols => 60, :rows => 2 %></p>
4 <p><%= f.text_area :summary, :cols => 60, :rows => 2 %></p>
5 <p><%= f.text_area :description, :required => true, :cols => 60, :rows => 15, :class => 'wiki-edit' %></p>
5 <p><%= f.text_area :description, :required => true, :cols => 60, :rows => 15, :class => 'wiki-edit' %></p>
6 <p id="attachments_form"><%= label_tag('attachments[1][file]', l(:label_attachment_plural))%><%= render :partial => 'attachments/form' %></p>
6 </div>
7 </div>
7
8
8 <%= wikitoolbar_for 'news_description' %>
9 <%= wikitoolbar_for 'news_description' %>
@@ -1,6 +1,6
1 <h2><%=l(:label_news)%></h2>
1 <h2><%=l(:label_news)%></h2>
2
2
3 <% labelled_form_for @news, :html => { :id => 'news-form', :method => :put } do |f| %>
3 <% labelled_form_for @news, :html => { :id => 'news-form', :multipart => true, :method => :put } do |f| %>
4 <%= render :partial => 'form', :locals => { :f => f } %>
4 <%= render :partial => 'form', :locals => { :f => f } %>
5 <%= submit_tag l(:button_save) %>
5 <%= submit_tag l(:button_save) %>
6 <%= link_to_remote l(:label_preview),
6 <%= link_to_remote l(:label_preview),
@@ -8,7 +8,7
8 <div id="add-news" style="display:none;">
8 <div id="add-news" style="display:none;">
9 <h2><%=l(:label_news_new)%></h2>
9 <h2><%=l(:label_news_new)%></h2>
10 <% labelled_form_for @news, :url => project_news_index_path(@project),
10 <% labelled_form_for @news, :url => project_news_index_path(@project),
11 :html => { :id => 'news-form' } do |f| %>
11 :html => { :id => 'news-form', :multipart => true } do |f| %>
12 <%= render :partial => 'news/form', :locals => { :f => f } %>
12 <%= render :partial => 'news/form', :locals => { :f => f } %>
13 <%= submit_tag l(:button_create) %>
13 <%= submit_tag l(:button_create) %>
14 <%= link_to_remote l(:label_preview),
14 <%= link_to_remote l(:label_preview),
@@ -1,7 +1,7
1 <h2><%=l(:label_news_new)%></h2>
1 <h2><%=l(:label_news_new)%></h2>
2
2
3 <% labelled_form_for @news, :url => project_news_index_path(@project),
3 <% labelled_form_for @news, :url => project_news_index_path(@project),
4 :html => { :id => 'news-form' } do |f| %>
4 :html => { :id => 'news-form', :multipart => true } do |f| %>
5 <%= render :partial => 'news/form', :locals => { :f => f } %>
5 <%= render :partial => 'news/form', :locals => { :f => f } %>
6 <%= submit_tag l(:button_create) %>
6 <%= submit_tag l(:button_create) %>
7 <%= link_to_remote l(:label_preview),
7 <%= link_to_remote l(:label_preview),
@@ -17,7 +17,7
17 <% if authorize_for('news', 'edit') %>
17 <% if authorize_for('news', 'edit') %>
18 <div id="edit-news" style="display:none;">
18 <div id="edit-news" style="display:none;">
19 <% labelled_form_for :news, @news, :url => news_path(@news),
19 <% labelled_form_for :news, @news, :url => news_path(@news),
20 :html => { :id => 'news-form', :method => :put } do |f| %>
20 :html => { :id => 'news-form', :multipart => true, :method => :put } do |f| %>
21 <%= render :partial => 'form', :locals => { :f => f } %>
21 <%= render :partial => 'form', :locals => { :f => f } %>
22 <%= submit_tag l(:button_save) %>
22 <%= submit_tag l(:button_save) %>
23 <%= link_to_remote l(:label_preview),
23 <%= link_to_remote l(:label_preview),
@@ -35,8 +35,9
35 <p><% unless @news.summary.blank? %><em><%=h @news.summary %></em><br /><% end %>
35 <p><% unless @news.summary.blank? %><em><%=h @news.summary %></em><br /><% end %>
36 <span class="author"><%= authoring @news.created_on, @news.author %></span></p>
36 <span class="author"><%= authoring @news.created_on, @news.author %></span></p>
37 <div class="wiki">
37 <div class="wiki">
38 <%= textilizable(@news.description) %>
38 <%= textilizable(@news, :description) %>
39 </div>
39 </div>
40 <%= link_to_attachments @news %>
40 <br />
41 <br />
41
42
42 <div id="comments" style="margin-bottom:16px;">
43 <div id="comments" style="margin-bottom:16px;">
@@ -53,6 +53,16 class NewsControllerTest < ActionController::TestCase
53 assert_tag :tag => 'h2', :content => /eCookbook first release/
53 assert_tag :tag => 'h2', :content => /eCookbook first release/
54 end
54 end
55
55
56 def test_show_should_show_attachments
57 attachment = Attachment.first
58 attachment.container = News.find(1)
59 attachment.save!
60
61 get :show, :id => 1
62 assert_response :success
63 assert_tag 'a', :content => attachment.filename
64 end
65
56 def test_show_not_found
66 def test_show_not_found
57 get :show, :id => 999
67 get :show, :id => 999
58 assert_response 404
68 assert_response 404
@@ -83,6 +93,34 class NewsControllerTest < ActionController::TestCase
83 assert_equal 1, ActionMailer::Base.deliveries.size
93 assert_equal 1, ActionMailer::Base.deliveries.size
84 end
94 end
85
95
96 def test_post_create_with_attachment
97 set_tmp_attachments_directory
98 @request.session[:user_id] = 2
99 assert_difference 'News.count' do
100 assert_difference 'Attachment.count' do
101 post :create, :project_id => 1,
102 :news => { :title => 'Test', :description => 'This is the description' },
103 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
104 end
105 end
106 attachment = Attachment.first(:order => 'id DESC')
107 news = News.first(:order => 'id DESC')
108 assert_equal news, attachment.container
109 end
110
111 def test_post_create_with_validation_failure
112 @request.session[:user_id] = 2
113 post :create, :project_id => 1, :news => { :title => '',
114 :description => 'This is the description',
115 :summary => '' }
116 assert_response :success
117 assert_template 'new'
118 assert_not_nil assigns(:news)
119 assert assigns(:news).new_record?
120 assert_tag :tag => 'div', :attributes => { :id => 'errorExplanation' },
121 :content => /1 error/
122 end
123
86 def test_get_edit
124 def test_get_edit
87 @request.session[:user_id] = 2
125 @request.session[:user_id] = 2
88 get :edit, :id => 1
126 get :edit, :id => 1
@@ -98,17 +136,18 class NewsControllerTest < ActionController::TestCase
98 assert_equal 'Description changed by test_post_edit', news.description
136 assert_equal 'Description changed by test_post_edit', news.description
99 end
137 end
100
138
101 def test_post_create_with_validation_failure
139 def test_put_update_with_attachment
140 set_tmp_attachments_directory
102 @request.session[:user_id] = 2
141 @request.session[:user_id] = 2
103 post :create, :project_id => 1, :news => { :title => '',
142 assert_no_difference 'News.count' do
104 :description => 'This is the description',
143 assert_difference 'Attachment.count' do
105 :summary => '' }
144 put :update, :id => 1,
106 assert_response :success
145 :news => { :description => 'This is the description' },
107 assert_template 'new'
146 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
108 assert_not_nil assigns(:news)
147 end
109 assert assigns(:news).new_record?
148 end
110 assert_tag :tag => 'div', :attributes => { :id => 'errorExplanation' },
149 attachment = Attachment.first(:order => 'id DESC')
111 :content => /1 error/
150 assert_equal News.find(1), attachment.container
112 end
151 end
113
152
114 def test_destroy
153 def test_destroy
@@ -71,4 +71,19 class NewsTest < ActiveSupport::TestCase
71 10.times { projects(:projects_001).news.create(valid_news) }
71 10.times { projects(:projects_001).news.create(valid_news) }
72 assert_equal 5, News.latest(users(:users_004)).size
72 assert_equal 5, News.latest(users(:users_004)).size
73 end
73 end
74
75 def test_attachments_should_be_visible
76 assert News.find(1).attachments_visible?(User.anonymous)
77 end
78
79 def test_attachments_should_be_deletable_with_manage_news_permission
80 manager = User.find(2)
81 assert News.find(1).attachments_deletable?(manager)
82 end
83
84 def test_attachments_should_not_be_deletable_without_manage_news_permission
85 manager = User.find(2)
86 Role.find_by_name('Manager').remove_permission!(:manage_news)
87 assert !News.find(1).attachments_deletable?(manager)
88 end
74 end
89 end
General Comments 0
You need to be logged in to leave comments. Login now