Files module: makes version field non required (#1053).

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@2117 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Jean-Philippe Lang 2008-12-09 18:00:27 +00:00
parent 5d2899ee1b
commit 66ff4cb7de
8 changed files with 116 additions and 21 deletions

View File

@ -188,10 +188,13 @@ class ProjectsController < ApplicationController
def add_file def add_file
if request.post? if request.post?
@version = @project.versions.find_by_id(params[:version_id]) container = (params[:version_id].blank? ? @project : @project.versions.find_by_id(params[:version_id]))
attachments = attach_files(@version, params[:attachments]) attachments = attach_files(container, params[:attachments])
Mailer.deliver_attachments_added(attachments) if !attachments.empty? && Setting.notified_events.include?('file_added') if !attachments.empty? && Setting.notified_events.include?('file_added')
Mailer.deliver_attachments_added(attachments)
end
redirect_to :controller => 'projects', :action => 'list_files', :id => @project redirect_to :controller => 'projects', :action => 'list_files', :id => @project
return
end end
@versions = @project.versions.sort @versions = @project.versions.sort
end end
@ -199,7 +202,8 @@ class ProjectsController < ApplicationController
def list_files def list_files
sort_init "#{Attachment.table_name}.filename", "asc" sort_init "#{Attachment.table_name}.filename", "asc"
sort_update sort_update
@versions = @project.versions.find(:all, :include => :attachments, :order => sort_clause).sort.reverse @containers = [ Project.find(@project.id, :include => :attachments, :order => sort_clause)]
@containers += @project.versions.find(:all, :include => :attachments, :order => sort_clause).sort.reverse
render :layout => !request.xhr? render :layout => !request.xhr?
end end

View File

@ -73,6 +73,9 @@ class Mailer < ActionMailer::Base
added_to = '' added_to = ''
added_to_url = '' added_to_url = ''
case container.class.name case container.class.name
when 'Project'
added_to_url = url_for(:controller => 'projects', :action => 'list_files', :id => container)
added_to = "#{l(:label_project)}: #{container}"
when 'Version' when 'Version'
added_to_url = url_for(:controller => 'projects', :action => 'list_files', :id => container.project_id) added_to_url = url_for(:controller => 'projects', :action => 'list_files', :id => container.project_id)
added_to = "#{l(:label_version)}: #{container.name}" added_to = "#{l(:label_version)}: #{container.name}"

View File

@ -44,6 +44,8 @@ class Project < ActiveRecord::Base
:association_foreign_key => 'custom_field_id' :association_foreign_key => 'custom_field_id'
acts_as_tree :order => "name", :counter_cache => true acts_as_tree :order => "name", :counter_cache => true
acts_as_attachable :view_permission => :view_files,
:delete_permission => :manage_files
acts_as_customizable acts_as_customizable
acts_as_searchable :columns => ['name', 'description'], :project_key => 'id', :permission => nil acts_as_searchable :columns => ['name', 'description'], :project_key => 'id', :permission => nil

View File

@ -4,10 +4,13 @@
<div class="box"> <div class="box">
<% form_tag({ :action => 'add_file', :id => @project }, :multipart => true, :class => "tabular") do %> <% form_tag({ :action => 'add_file', :id => @project }, :multipart => true, :class => "tabular") do %>
<p><label for="version_id"><%=l(:field_version)%> <span class="required">*</span></label> <% if @versions.any? %>
<%= select_tag "version_id", options_from_collection_for_select(@versions, "id", "name") %></p> <p><label for="version_id"><%=l(:field_version)%></label>
<%= select_tag "version_id", content_tag('option', '') +
options_from_collection_for_select(@versions, "id", "name") %></p>
<% end %>
<p><label><%=l(:label_attachment_plural)%></label><%= render :partial => 'attachments/form' %></p> <p><label><%=l(:label_attachment_plural)%></label><%= render :partial => 'attachments/form' %></p>
</div> </div>
<%= submit_tag l(:button_add) %> <%= submit_tag l(:button_add) %>
<% end %> <% end %>

View File

@ -8,36 +8,33 @@
<table class="list"> <table class="list">
<thead><tr> <thead><tr>
<th><%=l(:field_version)%></th>
<%= sort_header_tag("#{Attachment.table_name}.filename", :caption => l(:field_filename)) %> <%= sort_header_tag("#{Attachment.table_name}.filename", :caption => l(:field_filename)) %>
<%= sort_header_tag("#{Attachment.table_name}.created_on", :caption => l(:label_date), :default_order => 'desc') %> <%= sort_header_tag("#{Attachment.table_name}.created_on", :caption => l(:label_date), :default_order => 'desc') %>
<%= sort_header_tag("#{Attachment.table_name}.filesize", :caption => l(:field_filesize), :default_order => 'desc') %> <%= sort_header_tag("#{Attachment.table_name}.filesize", :caption => l(:field_filesize), :default_order => 'desc') %>
<%= sort_header_tag("#{Attachment.table_name}.downloads", :caption => l(:label_downloads_abbr), :default_order => 'desc') %> <%= sort_header_tag("#{Attachment.table_name}.downloads", :caption => l(:label_downloads_abbr), :default_order => 'desc') %>
<th>MD5</th> <th>MD5</th>
<% if delete_allowed %><th></th><% end %> <th></th>
</tr></thead> </tr></thead>
<tbody> <tbody>
<% for version in @versions %> <% @containers.each do |container| %>
<% unless version.attachments.empty? %> <% next if container.attachments.empty? -%>
<tr><th colspan="7" align="left"><span class="icon icon-package"><b><%= version.name %></b></span></th></tr> <% if container.is_a?(Version) -%>
<% for file in version.attachments %> <tr><th colspan="6" align="left"><span class="icon icon-package"><b><%=h container %></b></span></th></tr>
<% end -%>
<% container.attachments.each do |file| %>
<tr class="<%= cycle("odd", "even") %>"> <tr class="<%= cycle("odd", "even") %>">
<td></td>
<td><%= link_to_attachment file, :download => true, :title => file.description %></td> <td><%= link_to_attachment file, :download => true, :title => file.description %></td>
<td align="center"><%= format_time(file.created_on) %></td> <td align="center"><%= format_time(file.created_on) %></td>
<td align="center"><%= number_to_human_size(file.filesize) %></td> <td align="center"><%= number_to_human_size(file.filesize) %></td>
<td align="center"><%= file.downloads %></td> <td align="center"><%= file.downloads %></td>
<td align="center"><small><%= file.digest %></small></td> <td align="center"><small><%= file.digest %></small></td>
<% if delete_allowed %>
<td align="center"> <td align="center">
<%= link_to image_tag('delete.png'), {:controller => 'attachments', :action => 'destroy', :id => file}, <%= link_to(image_tag('delete.png'), {:controller => 'attachments', :action => 'destroy', :id => file},
:confirm => l(:text_are_you_sure), :method => :post %> :confirm => l(:text_are_you_sure), :method => :post) if delete_allowed %>
</td> </td>
<% end %>
</tr> </tr>
<% end <% end
reset_cycle %> reset_cycle %>
<% end %>
<% end %> <% end %>
</tbody> </tbody>
</table> </table>

View File

@ -85,4 +85,28 @@ attachments_007:
filename: archive.zip filename: archive.zip
author_id: 1 author_id: 1
content_type: application/octet-stream content_type: application/octet-stream
attachments_008:
created_on: 2006-07-19 21:07:27 +02:00
container_type: Project
container_id: 1
downloads: 0
disk_filename: 060719210727_project_file.zip
digest: b91e08d0cf966d5c6ff411bd8c4cc3a2
id: 8
filesize: 320
filename: project_file.zip
author_id: 2
content_type: application/octet-stream
attachments_009:
created_on: 2006-07-19 21:07:27 +02:00
container_type: Version
container_id: 1
downloads: 0
disk_filename: 060719210727_version_file.zip
digest: b91e08d0cf966d5c6ff411bd8c4cc3a2
id: 9
filesize: 452
filename: version_file.zip
author_id: 2
content_type: application/octet-stream

View File

@ -97,6 +97,23 @@ class AttachmentsControllerTest < Test::Unit::TestCase
@request.session[:user_id] = 2 @request.session[:user_id] = 2
assert_difference 'Attachment.count', -1 do assert_difference 'Attachment.count', -1 do
post :destroy, :id => 3 post :destroy, :id => 3
assert_response 302
end
end
def test_destroy_project_attachment
@request.session[:user_id] = 2
assert_difference 'Attachment.count', -1 do
post :destroy, :id => 8
assert_response 302
end
end
def test_destroy_version_attachment
@request.session[:user_id] = 2
assert_difference 'Attachment.count', -1 do
post :destroy, :id => 9
assert_response 302
end end
end end

View File

@ -23,7 +23,8 @@ class ProjectsController; def rescue_action(e) raise e end; end
class ProjectsControllerTest < Test::Unit::TestCase class ProjectsControllerTest < Test::Unit::TestCase
fixtures :projects, :versions, :users, :roles, :members, :issues, :journals, :journal_details, fixtures :projects, :versions, :users, :roles, :members, :issues, :journals, :journal_details,
:trackers, :projects_trackers, :issue_statuses, :enabled_modules, :enumerations, :boards, :messages :trackers, :projects_trackers, :issue_statuses, :enabled_modules, :enumerations, :boards, :messages,
:attachments
def setup def setup
@controller = ProjectsController.new @controller = ProjectsController.new
@ -112,12 +113,56 @@ class ProjectsControllerTest < Test::Unit::TestCase
assert_redirected_to 'admin/projects' assert_redirected_to 'admin/projects'
assert_nil Project.find_by_id(1) assert_nil Project.find_by_id(1)
end end
def test_add_file
set_tmp_attachments_directory
@request.session[:user_id] = 2
Setting.notified_events << 'file_added'
ActionMailer::Base.deliveries.clear
assert_difference 'Attachment.count' do
post :add_file, :id => 1, :version_id => '',
:attachments => {'1' => {'file' => test_uploaded_file('testfile.txt', 'text/plain')}}
end
assert_redirected_to 'projects/list_files/ecookbook'
a = Attachment.find(:first, :order => 'created_on DESC')
assert_equal 'testfile.txt', a.filename
assert_equal Project.find(1), a.container
mail = ActionMailer::Base.deliveries.last
assert_kind_of TMail::Mail, mail
assert_equal "[eCookbook] New file", mail.subject
assert mail.body.include?('testfile.txt')
end
def test_add_version_file
set_tmp_attachments_directory
@request.session[:user_id] = 2
Setting.notified_events << 'file_added'
assert_difference 'Attachment.count' do
post :add_file, :id => 1, :version_id => '2',
:attachments => {'1' => {'file' => test_uploaded_file('testfile.txt', 'text/plain')}}
end
assert_redirected_to 'projects/list_files/ecookbook'
a = Attachment.find(:first, :order => 'created_on DESC')
assert_equal 'testfile.txt', a.filename
assert_equal Version.find(2), a.container
end
def test_list_files def test_list_files
get :list_files, :id => 1 get :list_files, :id => 1
assert_response :success assert_response :success
assert_template 'list_files' assert_template 'list_files'
assert_not_nil assigns(:versions) assert_not_nil assigns(:containers)
# file attached to the project
assert_tag :a, :content => 'project_file.zip',
:attributes => { :href => '/attachments/download/8/project_file.zip' }
# file attached to a project's version
assert_tag :a, :content => 'version_file.zip',
:attributes => { :href => '/attachments/download/9/version_file.zip' }
end end
def test_changelog def test_changelog