##// END OF EJS Templates
fixed bug when no version is selected in projects/add_file...
Jean-Philippe Lang -
r94:bee3f353fca4
parent child
Show More
@@ -1,518 +1,516
1 1 # redMine - project management software
2 2 # Copyright (C) 2006 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 class ProjectsController < ApplicationController
19 19 layout 'base'
20 20 before_filter :find_project, :authorize, :except => [ :index, :list, :add ]
21 21 before_filter :require_admin, :only => [ :add, :destroy ]
22 22
23 23 helper :sort
24 24 include SortHelper
25 25 helper :custom_fields
26 26 include CustomFieldsHelper
27 27 helper :ifpdf
28 28 include IfpdfHelper
29 29 helper IssuesHelper
30 30 helper :queries
31 31 include QueriesHelper
32 32
33 33 def index
34 34 list
35 35 render :action => 'list' unless request.xhr?
36 36 end
37 37
38 38 # Lists public projects
39 39 def list
40 40 sort_init 'name', 'asc'
41 41 sort_update
42 42 @project_count = Project.count(["is_public=?", true])
43 43 @project_pages = Paginator.new self, @project_count,
44 44 15,
45 45 @params['page']
46 46 @projects = Project.find :all, :order => sort_clause,
47 47 :conditions => ["is_public=?", true],
48 48 :limit => @project_pages.items_per_page,
49 49 :offset => @project_pages.current.offset
50 50
51 51 render :action => "list", :layout => false if request.xhr?
52 52 end
53 53
54 54 # Add a new project
55 55 def add
56 56 @custom_fields = IssueCustomField.find(:all)
57 57 @root_projects = Project.find(:all, :conditions => "parent_id is null")
58 58 @project = Project.new(params[:project])
59 59 if request.get?
60 60 @custom_values = ProjectCustomField.find(:all).collect { |x| CustomValue.new(:custom_field => x, :customized => @project) }
61 61 else
62 62 @project.custom_fields = CustomField.find(@params[:custom_field_ids]) if @params[:custom_field_ids]
63 63 @custom_values = ProjectCustomField.find(:all).collect { |x| CustomValue.new(:custom_field => x, :customized => @project, :value => params["custom_fields"][x.id.to_s]) }
64 64 @project.custom_values = @custom_values
65 65 if @project.save
66 66 flash[:notice] = l(:notice_successful_create)
67 67 redirect_to :controller => 'admin', :action => 'projects'
68 68 end
69 69 end
70 70 end
71 71
72 72 # Show @project
73 73 def show
74 74 @custom_values = @project.custom_values.find(:all, :include => :custom_field)
75 75 @members = @project.members.find(:all, :include => [:user, :role])
76 76 @subprojects = @project.children if @project.children_count > 0
77 77 @news = @project.news.find(:all, :limit => 5, :include => [ :author, :project ], :order => "news.created_on DESC")
78 78 @trackers = Tracker.find(:all)
79 79 end
80 80
81 81 def settings
82 82 @root_projects = Project::find(:all, :conditions => ["parent_id is null and id <> ?", @project.id])
83 83 @custom_fields = IssueCustomField::find_all
84 84 @issue_category ||= IssueCategory.new
85 85 @member ||= @project.members.new
86 86 @roles = Role.find_all
87 87 @users = User.find_all - @project.members.find(:all, :include => :user).collect{|m| m.user }
88 88 @custom_values ||= ProjectCustomField.find(:all).collect { |x| @project.custom_values.find_by_custom_field_id(x.id) || CustomValue.new(:custom_field => x) }
89 89 end
90 90
91 91 # Edit @project
92 92 def edit
93 93 if request.post?
94 94 @project.custom_fields = IssueCustomField.find(@params[:custom_field_ids]) if @params[:custom_field_ids]
95 95 if params[:custom_fields]
96 96 @custom_values = ProjectCustomField.find(:all).collect { |x| CustomValue.new(:custom_field => x, :customized => @project, :value => params["custom_fields"][x.id.to_s]) }
97 97 @project.custom_values = @custom_values
98 98 end
99 99 if @project.update_attributes(params[:project])
100 100 flash[:notice] = l(:notice_successful_update)
101 101 redirect_to :action => 'settings', :id => @project
102 102 else
103 103 settings
104 104 render :action => 'settings'
105 105 end
106 106 end
107 107 end
108 108
109 109 # Delete @project
110 110 def destroy
111 111 if request.post? and params[:confirm]
112 112 @project.destroy
113 113 redirect_to :controller => 'admin', :action => 'projects'
114 114 end
115 115 end
116 116
117 117 # Add a new issue category to @project
118 118 def add_issue_category
119 119 if request.post?
120 120 @issue_category = @project.issue_categories.build(params[:issue_category])
121 121 if @issue_category.save
122 122 flash[:notice] = l(:notice_successful_create)
123 123 redirect_to :action => 'settings', :id => @project
124 124 else
125 125 settings
126 126 render :action => 'settings'
127 127 end
128 128 end
129 129 end
130 130
131 131 # Add a new version to @project
132 132 def add_version
133 133 @version = @project.versions.build(params[:version])
134 134 if request.post? and @version.save
135 135 flash[:notice] = l(:notice_successful_create)
136 136 redirect_to :action => 'settings', :id => @project
137 137 end
138 138 end
139 139
140 140 # Add a new member to @project
141 141 def add_member
142 142 @member = @project.members.build(params[:member])
143 143 if request.post?
144 144 if @member.save
145 145 flash[:notice] = l(:notice_successful_create)
146 146 redirect_to :action => 'settings', :id => @project
147 147 else
148 148 settings
149 149 render :action => 'settings'
150 150 end
151 151 end
152 152 end
153 153
154 154 # Show members list of @project
155 155 def list_members
156 156 @members = @project.members
157 157 end
158 158
159 159 # Add a new document to @project
160 160 def add_document
161 161 @categories = Enumeration::get_values('DCAT')
162 162 @document = @project.documents.build(params[:document])
163 163 if request.post?
164 164 # Save the attachment
165 165 if params[:attachment][:file].size > 0
166 166 @attachment = @document.attachments.build(params[:attachment])
167 167 @attachment.author_id = self.logged_in_user.id if self.logged_in_user
168 168 end
169 169 if @document.save
170 170 flash[:notice] = l(:notice_successful_create)
171 171 redirect_to :action => 'list_documents', :id => @project
172 172 end
173 173 end
174 174 end
175 175
176 176 # Show documents list of @project
177 177 def list_documents
178 178 @documents = @project.documents
179 179 end
180 180
181 181 # Add a new issue to @project
182 182 def add_issue
183 183 @tracker = Tracker.find(params[:tracker_id])
184 184 @priorities = Enumeration::get_values('IPRI')
185 185 @issue = Issue.new(:project => @project, :tracker => @tracker)
186 186 if request.get?
187 187 @issue.start_date = Date.today
188 188 @custom_values = @project.custom_fields_for_issues(@tracker).collect { |x| CustomValue.new(:custom_field => x, :customized => @issue) }
189 189 else
190 190 @issue.attributes = params[:issue]
191 191 @issue.author_id = self.logged_in_user.id if self.logged_in_user
192 192 # Multiple file upload
193 193 params[:attachments].each { |a|
194 194 @attachment = @issue.attachments.build(:file => a, :author => self.logged_in_user) unless a.size == 0
195 195 } if params[:attachments] and params[:attachments].is_a? Array
196 196 @custom_values = @project.custom_fields_for_issues(@tracker).collect { |x| CustomValue.new(:custom_field => x, :customized => @issue, :value => params["custom_fields"][x.id.to_s]) }
197 197 @issue.custom_values = @custom_values
198 198 if @issue.save
199 199 flash[:notice] = l(:notice_successful_create)
200 200 Mailer.deliver_issue_add(@issue) if Permission.find_by_controller_and_action(@params[:controller], @params[:action]).mail_enabled?
201 201 redirect_to :action => 'list_issues', :id => @project
202 202 end
203 203 end
204 204 end
205 205
206 206 # Show filtered/sorted issues list of @project
207 207 def list_issues
208 208 sort_init 'issues.id', 'desc'
209 209 sort_update
210 210
211 211 retrieve_query
212 212
213 213 @results_per_page_options = [ 15, 25, 50, 100 ]
214 214 if params[:per_page] and @results_per_page_options.include? params[:per_page].to_i
215 215 @results_per_page = params[:per_page].to_i
216 216 session[:results_per_page] = @results_per_page
217 217 else
218 218 @results_per_page = session[:results_per_page] || 25
219 219 end
220 220
221 221 if @query.valid?
222 222 @issue_count = Issue.count(:include => [:status, :project], :conditions => @query.statement)
223 223 @issue_pages = Paginator.new self, @issue_count, @results_per_page, @params['page']
224 224 @issues = Issue.find :all, :order => sort_clause,
225 225 :include => [ :author, :status, :tracker, :project ],
226 226 :conditions => @query.statement,
227 227 :limit => @issue_pages.items_per_page,
228 228 :offset => @issue_pages.current.offset
229 229 end
230 230 render :layout => false if request.xhr?
231 231 end
232 232
233 233 # Export filtered/sorted issues list to CSV
234 234 def export_issues_csv
235 235 sort_init 'issues.id', 'desc'
236 236 sort_update
237 237
238 238 retrieve_query
239 239 render :action => 'list_issues' and return unless @query.valid?
240 240
241 241 @issues = Issue.find :all, :order => sort_clause,
242 242 :include => [ :author, :status, :tracker, :project, :custom_values ],
243 243 :conditions => @query.statement
244 244
245 245 ic = Iconv.new('ISO-8859-1', 'UTF-8')
246 246 export = StringIO.new
247 247 CSV::Writer.generate(export, l(:general_csv_separator)) do |csv|
248 248 # csv header fields
249 249 headers = [ "#", l(:field_status), l(:field_tracker), l(:field_subject), l(:field_author), l(:field_created_on), l(:field_updated_on) ]
250 250 for custom_field in @project.all_custom_fields
251 251 headers << custom_field.name
252 252 end
253 253 csv << headers.collect {|c| ic.iconv(c) }
254 254 # csv lines
255 255 @issues.each do |issue|
256 256 fields = [issue.id, issue.status.name, issue.tracker.name, issue.subject, issue.author.display_name, l_datetime(issue.created_on), l_datetime(issue.updated_on)]
257 257 for custom_field in @project.all_custom_fields
258 258 fields << (show_value issue.custom_value_for(custom_field))
259 259 end
260 260 csv << fields.collect {|c| ic.iconv(c.to_s) }
261 261 end
262 262 end
263 263 export.rewind
264 264 send_data(export.read, :type => 'text/csv; header=present', :filename => 'export.csv')
265 265 end
266 266
267 267 # Export filtered/sorted issues to PDF
268 268 def export_issues_pdf
269 269 sort_init 'issues.id', 'desc'
270 270 sort_update
271 271
272 272 retrieve_query
273 273 render :action => 'list_issues' and return unless @query.valid?
274 274
275 275 @issues = Issue.find :all, :order => sort_clause,
276 276 :include => [ :author, :status, :tracker, :project, :custom_values ],
277 277 :conditions => @query.statement
278 278
279 279 @options_for_rfpdf ||= {}
280 280 @options_for_rfpdf[:file_name] = "export.pdf"
281 281 render :layout => false
282 282 end
283 283
284 284 def move_issues
285 285 @issues = @project.issues.find(params[:issue_ids]) if params[:issue_ids]
286 286 redirect_to :action => 'list_issues', :id => @project and return unless @issues
287 287 @projects = []
288 288 # find projects to which the user is allowed to move the issue
289 289 @logged_in_user.memberships.each {|m| @projects << m.project if Permission.allowed_to_role("projects/move_issues", m.role_id)}
290 290 # issue can be moved to any tracker
291 291 @trackers = Tracker.find(:all)
292 292 if request.post? and params[:new_project_id] and params[:new_tracker_id]
293 293 new_project = Project.find(params[:new_project_id])
294 294 new_tracker = Tracker.find(params[:new_tracker_id])
295 295 @issues.each { |i|
296 296 # category is project dependent
297 297 i.category = nil unless i.project_id == new_project.id
298 298 # move the issue
299 299 i.project = new_project
300 300 i.tracker = new_tracker
301 301 i.save
302 302 }
303 303 flash[:notice] = l(:notice_successful_update)
304 304 redirect_to :action => 'list_issues', :id => @project
305 305 end
306 306 end
307 307
308 308 def add_query
309 309 @query = Query.new(params[:query])
310 310 @query.project = @project
311 311 @query.user = logged_in_user
312 312
313 313 params[:fields].each do |field|
314 314 @query.add_filter(field, params[:operators][field], params[:values][field])
315 315 end if params[:fields]
316 316
317 317 if request.post? and @query.save
318 318 flash[:notice] = l(:notice_successful_create)
319 319 redirect_to :controller => 'reports', :action => 'issue_report', :id => @project
320 320 end
321 321 render :layout => false if request.xhr?
322 322 end
323 323
324 324 # Add a news to @project
325 325 def add_news
326 326 @news = News.new(:project => @project)
327 327 if request.post?
328 328 @news.attributes = params[:news]
329 329 @news.author_id = self.logged_in_user.id if self.logged_in_user
330 330 if @news.save
331 331 flash[:notice] = l(:notice_successful_create)
332 332 redirect_to :action => 'list_news', :id => @project
333 333 end
334 334 end
335 335 end
336 336
337 337 # Show news list of @project
338 338 def list_news
339 339 @news_pages, @news = paginate :news, :per_page => 10, :conditions => ["project_id=?", @project.id], :include => :author, :order => "news.created_on DESC"
340 340 render :action => "list_news", :layout => false if request.xhr?
341 341 end
342 342
343 def add_file
344 if request.post?
345 # Save the attachment
346 if params[:attachment][:file].size > 0
347 @attachment = @project.versions.find(params[:version_id]).attachments.build(params[:attachment])
348 @attachment.author_id = self.logged_in_user.id if self.logged_in_user
349 if @attachment.save
350 flash[:notice] = l(:notice_successful_create)
351 redirect_to :controller => 'projects', :action => 'list_files', :id => @project
352 end
343 def add_file
344 @attachment = Attachment.new(params[:attachment])
345 if request.post? and params[:attachment][:file].size > 0
346 @attachment.container = @project.versions.find_by_id(params[:version_id])
347 @attachment.author = logged_in_user
348 if @attachment.save
349 flash[:notice] = l(:notice_successful_create)
350 redirect_to :controller => 'projects', :action => 'list_files', :id => @project
353 351 end
354 352 end
355 353 @versions = @project.versions
356 354 end
357 355
358 356 def list_files
359 357 @versions = @project.versions
360 358 end
361 359
362 360 # Show changelog for @project
363 361 def changelog
364 362 @trackers = Tracker.find(:all, :conditions => ["is_in_chlog=?", true])
365 363 if request.get?
366 364 @selected_tracker_ids = @trackers.collect {|t| t.id.to_s }
367 365 else
368 366 @selected_tracker_ids = params[:tracker_ids].collect { |id| id.to_i.to_s } if params[:tracker_ids] and params[:tracker_ids].is_a? Array
369 367 end
370 368 @selected_tracker_ids ||= []
371 369 @fixed_issues = @project.issues.find(:all,
372 370 :include => [ :fixed_version, :status, :tracker ],
373 371 :conditions => [ "issue_statuses.is_closed=? and issues.tracker_id in (#{@selected_tracker_ids.join(',')}) and issues.fixed_version_id is not null", true],
374 372 :order => "versions.effective_date DESC, issues.id DESC"
375 373 ) unless @selected_tracker_ids.empty?
376 374 @fixed_issues ||= []
377 375 end
378 376
379 377 def activity
380 378 if params[:year] and params[:year].to_i > 1900
381 379 @year = params[:year].to_i
382 380 if params[:month] and params[:month].to_i > 0 and params[:month].to_i < 13
383 381 @month = params[:month].to_i
384 382 end
385 383 end
386 384 @year ||= Date.today.year
387 385 @month ||= Date.today.month
388 386
389 387 @date_from = Date.civil(@year, @month, 1)
390 388 @date_to = (@date_from >> 1)-1
391 389
392 390 @events_by_day = {}
393 391
394 392 unless params[:show_issues] == "0"
395 393 @project.issues.find(:all, :include => [:author, :status], :conditions => ["issues.created_on>=? and issues.created_on<=?", @date_from, @date_to] ).each { |i|
396 394 @events_by_day[i.created_on.to_date] ||= []
397 395 @events_by_day[i.created_on.to_date] << i
398 396 }
399 397 @show_issues = 1
400 398 end
401 399
402 400 unless params[:show_news] == "0"
403 401 @project.news.find(:all, :conditions => ["news.created_on>=? and news.created_on<=?", @date_from, @date_to] ).each { |i|
404 402 @events_by_day[i.created_on.to_date] ||= []
405 403 @events_by_day[i.created_on.to_date] << i
406 404 }
407 405 @show_news = 1
408 406 end
409 407
410 408 unless params[:show_files] == "0"
411 409 Attachment.find(:all, :select => "attachments.*", :joins => "LEFT JOIN versions ON versions.id = attachments.container_id", :conditions => ["attachments.container_type='Version' and versions.project_id=? and attachments.created_on>=? and attachments.created_on<=?", @project.id, @date_from, @date_to] ).each { |i|
412 410 @events_by_day[i.created_on.to_date] ||= []
413 411 @events_by_day[i.created_on.to_date] << i
414 412 }
415 413 @show_files = 1
416 414 end
417 415
418 416 unless params[:show_documents] == "0"
419 417 @project.documents.find(:all, :conditions => ["documents.created_on>=? and documents.created_on<=?", @date_from, @date_to] ).each { |i|
420 418 @events_by_day[i.created_on.to_date] ||= []
421 419 @events_by_day[i.created_on.to_date] << i
422 420 }
423 421 Attachment.find(:all, :select => "attachments.*", :joins => "LEFT JOIN documents ON documents.id = attachments.container_id", :conditions => ["attachments.container_type='Document' and documents.project_id=? and attachments.created_on>=? and attachments.created_on<=?", @project.id, @date_from, @date_to] ).each { |i|
424 422 @events_by_day[i.created_on.to_date] ||= []
425 423 @events_by_day[i.created_on.to_date] << i
426 424 }
427 425 @show_documents = 1
428 426 end
429 427
430 428 render :layout => false if request.xhr?
431 429 end
432 430
433 431 def calendar
434 432 if params[:year] and params[:year].to_i > 1900
435 433 @year = params[:year].to_i
436 434 if params[:month] and params[:month].to_i > 0 and params[:month].to_i < 13
437 435 @month = params[:month].to_i
438 436 end
439 437 end
440 438 @year ||= Date.today.year
441 439 @month ||= Date.today.month
442 440
443 441 @date_from = Date.civil(@year, @month, 1)
444 442 @date_to = (@date_from >> 1)-1
445 443 # start on monday
446 444 @date_from = @date_from - (@date_from.cwday-1)
447 445 # finish on sunday
448 446 @date_to = @date_to + (7-@date_to.cwday)
449 447
450 448 @issues = @project.issues.find(:all, :include => :tracker, :conditions => ["((start_date>=? and start_date<=?) or (due_date>=? and due_date<=?))", @date_from, @date_to, @date_from, @date_to])
451 449 render :layout => false if request.xhr?
452 450 end
453 451
454 452 def gantt
455 453 if params[:year] and params[:year].to_i >0
456 454 @year_from = params[:year].to_i
457 455 if params[:month] and params[:month].to_i >=1 and params[:month].to_i <= 12
458 456 @month_from = params[:month].to_i
459 457 else
460 458 @month_from = 1
461 459 end
462 460 else
463 461 @month_from ||= (Date.today << 1).month
464 462 @year_from ||= (Date.today << 1).year
465 463 end
466 464
467 465 @zoom = (params[:zoom].to_i > 0 and params[:zoom].to_i < 5) ? params[:zoom].to_i : 2
468 466 @months = (params[:months].to_i > 0 and params[:months].to_i < 25) ? params[:months].to_i : 6
469 467
470 468 @date_from = Date.civil(@year_from, @month_from, 1)
471 469 @date_to = (@date_from >> @months) - 1
472 470 @issues = @project.issues.find(:all, :order => "start_date, due_date", :conditions => ["(((start_date>=? and start_date<=?) or (due_date>=? and due_date<=?) or (start_date<? and due_date>?)) and start_date is not null and due_date is not null)", @date_from, @date_to, @date_from, @date_to, @date_from, @date_to])
473 471
474 472 if params[:output]=='pdf'
475 473 @options_for_rfpdf ||= {}
476 474 @options_for_rfpdf[:file_name] = "gantt.pdf"
477 475 render :template => "projects/gantt.rfpdf", :layout => false
478 476 else
479 477 render :template => "projects/gantt.rhtml"
480 478 end
481 479 end
482 480
483 481 private
484 482 # Find project of id params[:id]
485 483 # if not found, redirect to project list
486 484 # Used as a before_filter
487 485 def find_project
488 486 @project = Project.find(params[:id])
489 487 @html_title = @project.name
490 488 rescue
491 489 redirect_to :action => 'list'
492 490 end
493 491
494 492 # Retrieve query from session or build a new query
495 493 def retrieve_query
496 494 if params[:query_id]
497 495 @query = @project.queries.find(params[:query_id])
498 496 else
499 497 if params[:set_filter] or !session[:query] or session[:query].project_id != @project.id
500 498 # Give it a name, required to be valid
501 499 @query = Query.new(:name => "_")
502 500 @query.project = @project
503 501 if params[:fields] and params[:fields].is_a? Array
504 502 params[:fields].each do |field|
505 503 @query.add_filter(field, params[:operators][field], params[:values][field])
506 504 end
507 505 else
508 506 @query.available_filters.keys.each do |field|
509 507 @query.add_short_filter(field, params[field]) if params[field]
510 508 end
511 509 end
512 510 session[:query] = @query
513 511 else
514 512 @query = session[:query]
515 513 end
516 514 end
517 515 end
518 516 end
@@ -1,81 +1,85
1 1 # redMine - project management software
2 2 # Copyright (C) 2006 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require "digest/md5"
19 19
20 20 class Attachment < ActiveRecord::Base
21 21 belongs_to :container, :polymorphic => true
22 22 belongs_to :author, :class_name => "User", :foreign_key => "author_id"
23 23
24 validates_presence_of :filename
24 validates_presence_of :container, :filename
25 25
26 26 def file=(incomming_file)
27 27 unless incomming_file.nil?
28 28 @temp_file = incomming_file
29 29 if @temp_file.size > 0
30 30 self.filename = sanitize_filename(@temp_file.original_filename)
31 31 self.disk_filename = DateTime.now.strftime("%y%m%d%H%M%S") + "_" + self.filename
32 32 self.content_type = @temp_file.content_type
33 33 self.filesize = @temp_file.size
34 34 end
35 35 end
36 36 end
37 37
38 def file
39 nil
40 end
41
38 42 # Copy temp file to its final location
39 43 def before_save
40 44 if @temp_file && (@temp_file.size > 0)
41 45 logger.debug("saving '#{self.diskfile}'")
42 46 File.open(diskfile, "wb") do |f|
43 47 f.write(@temp_file.read)
44 48 end
45 49 self.digest = Digest::MD5.hexdigest(File.read(diskfile))
46 50 end
47 51 end
48 52
49 53 # Deletes file on the disk
50 54 def after_destroy
51 55 if self.filename?
52 56 File.delete(diskfile) if File.exist?(diskfile)
53 57 end
54 58 end
55 59
56 60 # Returns file's location on disk
57 61 def diskfile
58 62 "#{$RDM_STORAGE_PATH}/#{self.disk_filename}"
59 63 end
60 64
61 65 def increment_download
62 66 increment!(:downloads)
63 67 end
64 68
65 69 # returns last created projects
66 70 def self.most_downloaded
67 71 find(:all, :limit => 5, :order => "downloads DESC")
68 72 end
69 73
70 74 private
71 75 def sanitize_filename(value)
72 76 # get only the filename, not the whole path
73 77 just_filename = value.gsub(/^.*(\\|\/)/, '')
74 78 # NOTE: File.basename doesn't work right with Windows paths on Unix
75 79 # INCORRECT: just_filename = File.basename(value.gsub('\\\\', '/'))
76 80
77 81 # Finally, replace all non alphanumeric, underscore or periods with underscore
78 82 @filename = just_filename.gsub(/[^\w\.\-]/,'_')
79 83 end
80 84
81 85 end
@@ -1,14 +1,14
1 1 <h2><%=l(:label_attachment_new)%></h2>
2 2
3 3 <%= error_messages_for 'attachment' %>
4 <%= start_form_tag ({ :action => 'add_file', :project => @project }, :multipart => true) %>
4 <div class="box">
5 <%= start_form_tag ({ :action => 'add_file', :id => @project }, :multipart => true, :class => "tabular") %>
5 6
6 <p><label for="version_id"><%=l(:field_version)%></label><br />
7 <select name="version_id">
8 <%= options_from_collection_for_select @versions, "id", "name" %>
9 </select></p>
7 <p><label for="version_id"><%=l(:field_version)%> <span class="required">*</span></label>
8 <%= select_tag "version_id", options_from_collection_for_select(@versions, "id", "name") %></p>
10 9
11 <p><b><%=l(:label_attachment)%><b><br /><%= file_field 'attachment', 'file' %></p>
12 <br/>
10 <p><label for="attachment_file"><%=l(:label_attachment)%> <span class="required">*</span></label>
11 <%= file_field 'attachment', 'file' %></p>
12 </div>
13 13 <%= submit_tag l(:button_add) %>
14 14 <%= end_form_tag %> No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now