Commit 9daf39ec authored by Jean-Philippe Lang's avatar Jean-Philippe Lang

Adds an optional description to attachments.

git-svn-id: http://redmine.rubyforge.org/svn/trunk@1180 e93f8b46-1217-0410-a6f0-8f06a7374b81
parent 4b15dc10
......@@ -160,16 +160,20 @@ class ApplicationController < ActionController::Base
end
# TODO: move to model
def attach_files(obj, files)
attachments = []
if files && files.is_a?(Array)
files.each do |file|
next unless file.size > 0
a = Attachment.create(:container => obj, :file => file, :author => User.current)
attachments << a unless a.new_record?
def attach_files(obj, attachments)
attached = []
if attachments && attachments.is_a?(Hash)
attachments.each_value do |attachment|
file = attachment['file']
next unless file && file.size > 0
a = Attachment.create(:container => obj,
:file => file,
:description => attachment['description'].to_s.strip,
:author => User.current)
attached << a unless a.new_record?
end
end
attachments
attached
end
# Returns the number of objects that should be displayed
......
......@@ -21,6 +21,8 @@ class DocumentsController < ApplicationController
before_filter :find_document, :except => [:index, :new]
before_filter :authorize
helper :attachments
def index
@sort_by = %w(category date title author).include?(params[:sort_by]) ? params[:sort_by] : 'category'
documents = @project.documents.find :all, :include => [:attachments, :category]
......
......@@ -396,7 +396,7 @@ module ApplicationHelper
def labelled_tabular_form_for(name, object, options, &proc)
options[:html] ||= {}
options[:html].store :class, "tabular"
options[:html][:class] = 'tabular' unless options[:html].has_key?(:class)
form_for(name, object, options.merge({ :builder => TabularFormBuilder, :lang => current_language}), &proc)
end
......
<p id="attachments_p"><label for="attachment_file"><%=l(:label_attachment)%>
<%= image_to_function "add.png", "addFileField();return false" %></label>
<%= file_field_tag 'attachments[]', :size => 30 %> <em>(<%= l(:label_max_size) %>: <%= number_to_human_size(Setting.attachment_max_size.to_i.kilobytes) %>)</em></p>
<span id="attachments_fields">
<%= file_field_tag 'attachments[1][file]', :size => 30, :id => nil -%>
<%= text_field_tag 'attachments[1][description]', '', :size => 60, :id => nil %>
<em><%= l(:label_optional_description) %></em>
</span>
<br />
<small><%= link_to l(:label_add_another_file), {}, :onclick => 'addFileField(); return false;' %>
(<%= l(:label_max_size) %>: <%= number_to_human_size(Setting.attachment_max_size.to_i.kilobytes) %>)
</small>
<div class="attachments">
<% for attachment in attachments %>
<p><%= link_to attachment.filename, {:controller => 'attachments', :action => 'download', :id => attachment }, :class => 'icon icon-attachment' %>
(<%= number_to_human_size attachment.filesize %>)
<% unless options[:no_author] %>
<span class="author"><%= attachment.author.name %>, <%= format_date(attachment.created_on) %></span>
<% end %>
<p><%= link_to attachment.filename, {:controller => 'attachments', :action => 'download', :id => attachment }, :class => 'icon icon-attachment' -%>
<%= h(" - #{attachment.description}") unless attachment.description.blank? %>
<span class="size">(<%= number_to_human_size attachment.filesize %>)</span>
<% if options[:delete_url] %>
<%= link_to image_tag('delete.png'), options[:delete_url].update({:attachment_id => attachment}), :confirm => l(:text_are_you_sure), :method => :post %>
<%= link_to image_tag('delete.png'), options[:delete_url].update({:attachment_id => attachment}),
:confirm => l(:text_are_you_sure),
:method => :post,
:class => 'delete',
:title => l(:button_delete) %>
<% end %>
<% unless options[:no_author] %>
<span class="author"><%= attachment.author %>, <%= format_time(attachment.created_on) %></span>
<% end %>
</p>
<% end %>
......
<p id="attachments_p">
<label for="attachment_file"><%=l(:label_attachment)%>
<%= image_to_function "add.png", "addFileField();return false" %></label>
<%= file_field_tag 'attachments[]', :size => 30 %>
<em>(<%= l(:label_max_size) %>: <%= number_to_human_size(Setting.attachment_max_size.to_i.kilobytes) %>)</em>
</p>
......@@ -10,7 +10,7 @@
<% form_tag({:controller => 'documents', :action => 'new', :project_id => @project}, :class => "tabular", :multipart => true) do %>
<%= render :partial => 'documents/form' %>
<div class="box">
<%= render :partial => 'common/attachments_form'%>
<p><label><%=l(:label_attachment_plural)%></label><%= render :partial => 'attachments/form' %></p>
</div>
<%= submit_tag l(:button_create) %>
<%= link_to l(:button_cancel), "#", :onclick => 'Element.hide("add-document")' %>
......
......@@ -4,7 +4,7 @@
<%= render :partial => 'documents/form' %>
<div class="box">
<%= render :partial => 'common/attachments_form'%>
<p><label><%=l(:label_attachment_plural)%></label><%= render :partial => 'attachments/form' %></p>
</div>
<%= submit_tag l(:button_create) %>
......
......@@ -10,29 +10,17 @@
<div class="wiki">
<%= textilizable @document.description, :attachments => @document.attachments %>
</div>
<br />
<h3><%= l(:label_attachment_plural) %></h3>
<ul class="documents">
<% for attachment in @attachments %>
<li>
<div class="contextual">
<%= link_to_if_authorized l(:button_delete), {:controller => 'documents', :action => 'destroy_attachment', :id => @document, :attachment_id => attachment}, :confirm => l(:text_are_you_sure), :method => :post, :class => 'icon icon-del' %>
</div>
<%= link_to attachment.filename, :action => 'download', :id => @document, :attachment_id => attachment %>
(<%= number_to_human_size attachment.filesize %>)<br />
<span class="author"><%= authoring attachment.created_on, attachment.author %></span><br />
<%= lwr(:label_download, attachment.downloads) %>
</li>
<% end %>
</ul>
<br />
<%= link_to_attachments @attachments, :delete_url => (authorize_for('documents', 'destroy_attachment') ? {:controller => 'documents', :action => 'destroy_attachment', :id => @document} : nil) %>
<% if authorize_for('documents', 'add_attachment') %>
<p><%= toggle_link l(:label_attachment_new), "add_attachment_form" %></p>
<% form_tag({ :controller => 'documents', :action => 'add_attachment', :id => @document }, :multipart => true, :class => "tabular", :id => "add_attachment_form", :style => "display:none;") do %>
<%= render :partial => 'attachments/form' %>
<p><%= link_to l(:label_attachment_new), {}, :onclick => "Element.show('add_attachment_form'); Element.hide(this); Element.scrollTo('add_attachment_form'); return false;",
:id => 'attach_files_link' %></p>
<% form_tag({ :controller => 'documents', :action => 'add_attachment', :id => @document }, :multipart => true, :id => "add_attachment_form", :style => "display:none;") do %>
<div class="box">
<p><%= render :partial => 'attachments/form' %></p>
</div>
<%= submit_tag l(:button_add) %>
<% end %>
<% end %>
......
<% labelled_tabular_form_for :issue, @issue,
:url => {:action => 'edit', :id => @issue},
:html => {:id => 'issue-form',
:class => nil,
:multipart => true} do |f| %>
<%= error_messages_for 'issue' %>
<div class="box">
<% if @edit_allowed || !@allowed_statuses.empty? %>
<fieldset>
<legend><%= l(:label_change_properties) %>
<fieldset class="tabular"><legend><%= l(:label_change_properties) %>
<% if !@issue.new_record? && !@issue.errors.any? && @edit_allowed %>
<small>(<%= link_to l(:label_more), {}, :onclick => 'Effect.toggle("issue_descr_fields", "appear", {duration:0.3}); return false;' %>)</small>
<% end %>
......@@ -15,7 +15,7 @@
</fieldset>
<% end %>
<% if authorize_for('timelog', 'edit') %>
<fieldset><legend><%= l(:button_log_time) %></legend>
<fieldset class="tabular"><legend><%= l(:button_log_time) %></legend>
<% fields_for :time_entry, @time_entry, { :builder => TabularFormBuilder, :lang => current_language} do |time_entry| %>
<div class="splitcontentleft">
<p><%= time_entry.text_field :hours, :size => 6, :label => :label_spent_time %> <%= l(:field_hours) %></p>
......@@ -32,9 +32,7 @@
<%= text_area_tag 'notes', @notes, :cols => 60, :rows => 10, :class => 'wiki-edit' %>
<%= wikitoolbar_for 'notes' %>
<p id="attachments_p"><label><%=l(:label_attachment_new)%>
<%= image_to_function 'add.png', 'addFileField();return false;' %></label>
<%= file_field_tag 'attachments[]', :size => 30 %> <em>(<%= l(:label_max_size) %>: <%= number_to_human_size(Setting.attachment_max_size.to_i.kilobytes) %>)</em></p>
<p><%=l(:label_attachment_plural)%><br /><%= render :partial => 'attachments/form' %></p>
</fieldset>
</div>
......
......@@ -44,9 +44,7 @@
<%= render :partial => 'form_custom_fields', :locals => {:values => @custom_values} %>
<% if @issue.new_record? %>
<p id="attachments_p"><label for="attachment_file"><%=l(:label_attachment)%>
<%= image_to_function "add.png", "addFileField();return false" %></label>
<%= file_field_tag 'attachments[]', :size => 30 %> <em>(<%= l(:label_max_size) %>: <%= number_to_human_size(Setting.attachment_max_size.to_i.kilobytes) %>)</em></p>
<p><label><%=l(:label_attachment_plural)%></label><%= render :partial => 'attachments/form' %></p>
<% end %>
<%= wikitoolbar_for 'issue_description' %>
......
......@@ -4,7 +4,7 @@
<div class="box">
<!--[form:message]-->
<p><label><%= l(:field_subject) %></label><br />
<%= f.text_field :subject, :required => true, :size => 120 %>
<%= f.text_field :subject, :size => 120 %>
<% if !replying && User.current.allowed_to?(:edit_messages, @project) %>
<label><%= f.check_box :sticky %> Sticky</label>
......@@ -12,11 +12,10 @@
<% end %>
</p>
<p><%= f.text_area :content, :required => true, :cols => 80, :rows => 15, :class => 'wiki-edit', :id => 'message_content' %></p>
<p><%= f.text_area :content, :cols => 80, :rows => 15, :class => 'wiki-edit', :id => 'message_content' %></p>
<%= wikitoolbar_for 'message_content' %>
<!--[eoform:message]-->
<span class="tabular">
<%= render :partial => 'attachments/form' %>
</span>
<p><%= l(:label_attachment_plural) %><br />
<%= render :partial => 'attachments/form' %></p>
</div>
......@@ -7,7 +7,7 @@
<p><label for="version_id"><%=l(:field_version)%> <span class="required">*</span></label>
<%= select_tag "version_id", options_from_collection_for_select(@versions, "id", "name") %></p>
<%= render :partial => 'attachments/form' %>
<p><label><%=l(:label_attachment_plural)%></label><%= render :partial => 'attachments/form' %></p>
</div>
<%= submit_tag l(:button_add) %>
<% end %>
\ No newline at end of file
......@@ -23,7 +23,8 @@
<% for file in version.attachments %>
<tr class="<%= cycle("odd", "even") %>">
<td></td>
<td><%= link_to file.filename, :controller => 'versions', :action => 'download', :id => version, :attachment_id => file %></td>
<td><%= link_to(file.filename, {:controller => 'versions', :action => 'download', :id => version, :attachment_id => file},
:title => file.description) %></td>
<td align="center"><%= format_date(file.created_on) %></td>
<td align="center"><%= number_to_human_size(file.filesize) %></td>
<td align="center"><%= file.downloads %></td>
......
......@@ -25,10 +25,14 @@
<%= link_to_attachments @page.attachments, :delete_url => (authorize_for('wiki', 'destroy_attachment') ? {:controller => 'wiki', :action => 'destroy_attachment', :page => @page.title} : nil) %>
<% if authorize_for('wiki', 'add_attachment') %>
<p><%= toggle_link l(:label_attachment_new), "add_attachment_form" %></p>
<% form_tag({ :controller => 'wiki', :action => 'add_attachment', :page => @page.title }, :multipart => true, :class => "tabular", :id => "add_attachment_form", :style => "display:none;") do %>
<%= render :partial => 'attachments/form' %>
<p><%= link_to l(:label_attachment_new), {}, :onclick => "Element.show('add_attachment_form'); Element.hide(this); Element.scrollTo('add_attachment_form'); return false;",
:id => 'attach_files_link' %></p>
<% form_tag({ :controller => 'wiki', :action => 'add_attachment', :page => @page.title }, :multipart => true, :id => "add_attachment_form", :style => "display:none;") do %>
<div class="box">
<p><%= render :partial => 'attachments/form' %></p>
</div>
<%= submit_tag l(:button_add) %>
<%= link_to l(:button_cancel), {}, :onclick => "Element.hide('add_attachment_form'); Element.show('attach_files_link'); return false;" %>
<% end %>
<% end %>
......
class AddAttachmentsDescription < ActiveRecord::Migration
def self.up
add_column :attachments, :description, :string
end
def self.down
remove_column :attachments, :description
end
end
......@@ -600,3 +600,5 @@ label_date_range: Date range
label_last_week: last week
label_yesterday: yesterday
label_last_month: last month
label_add_another_file: Add another file
label_optional_description: Optional description
......@@ -600,3 +600,5 @@ label_date_range: Date range
label_last_week: last week
label_yesterday: yesterday
label_last_month: last month
label_add_another_file: Add another file
label_optional_description: Optional description
......@@ -600,3 +600,5 @@ label_date_range: Date range
label_last_week: last week
label_yesterday: yesterday
label_last_month: last month
label_add_another_file: Add another file
label_optional_description: Optional description
......@@ -497,6 +497,8 @@ label_scm: SCM
label_plugins: Plugins
label_ldap_authentication: LDAP authentication
label_downloads_abbr: D/L
label_optional_description: Optional description
label_add_another_file: Add another file
button_login: Login
button_submit: Submit
......
......@@ -603,3 +603,5 @@ label_date_range: Date range
label_last_week: last week
label_yesterday: yesterday
label_last_month: last month
label_add_another_file: Add another file
label_optional_description: Optional description
......@@ -604,3 +604,5 @@ label_date_range: Date range
label_last_week: last week
label_yesterday: yesterday
label_last_month: last month
label_add_another_file: Add another file
label_optional_description: Optional description
......@@ -497,6 +497,8 @@ label_scm: SCM
label_plugins: Plugins
label_ldap_authentication: Authentification LDAP
label_downloads_abbr: D/L
label_optional_description: Description facultative
label_add_another_file: Ajouter un autre fichier
button_login: Connexion
button_submit: Soumettre
......
......@@ -600,3 +600,5 @@ label_date_range: Date range
label_last_week: last week
label_yesterday: yesterday
label_last_month: last month
label_add_another_file: Add another file
label_optional_description: Optional description
......@@ -600,3 +600,5 @@ label_date_range: Date range
label_last_week: last week
label_yesterday: yesterday
label_last_month: last month
label_add_another_file: Add another file
label_optional_description: Optional description
......@@ -601,3 +601,5 @@ label_date_range: Date range
label_last_week: last week
label_yesterday: yesterday
label_last_month: last month
label_add_another_file: Add another file
label_optional_description: Optional description
......@@ -600,3 +600,5 @@ label_date_range: Date range
label_last_week: last week
label_yesterday: yesterday
label_last_month: last month
label_add_another_file: Add another file
label_optional_description: Optional description
......@@ -601,3 +601,5 @@ label_date_range: Date range
label_last_week: last week
label_yesterday: yesterday
label_last_month: last month
label_add_another_file: Add another file
label_optional_description: Optional description
......@@ -601,3 +601,5 @@ label_date_range: Date range
label_last_week: last week
label_yesterday: yesterday
label_last_month: last month
label_add_another_file: Add another file
label_optional_description: Optional description
......@@ -600,3 +600,5 @@ label_date_range: Date range
label_last_week: last week
label_yesterday: yesterday
label_last_month: last month
label_add_another_file: Add another file
label_optional_description: Optional description
......@@ -600,3 +600,5 @@ label_date_range: Date range
label_last_week: last week
label_yesterday: yesterday
label_last_month: last month
label_add_another_file: Add another file
label_optional_description: Optional description
......@@ -600,3 +600,5 @@ label_date_range: Date range
label_last_week: last week
label_yesterday: yesterday
label_last_month: last month
label_add_another_file: Add another file
label_optional_description: Optional description
......@@ -600,3 +600,5 @@ label_date_range: Date range
label_last_week: last week
label_yesterday: yesterday
label_last_month: last month
label_add_another_file: Add another file
label_optional_description: Optional description
......@@ -604,3 +604,5 @@ label_date_range: Date range
label_last_week: last week
label_yesterday: yesterday
label_last_month: last month
label_add_another_file: Add another file
label_optional_description: Optional description
......@@ -601,3 +601,5 @@ label_date_range: Date range
label_last_week: last week
label_yesterday: yesterday
label_last_month: last month
label_add_another_file: Add another file
label_optional_description: Optional description
......@@ -601,3 +601,5 @@ label_date_range: Date range
label_last_week: last week
label_yesterday: yesterday
label_last_month: last month
label_add_another_file: Add another file
label_optional_description: Optional description
......@@ -602,3 +602,5 @@ label_date_range: Date range
label_last_week: last week
label_yesterday: yesterday
label_last_month: last month
label_add_another_file: Add another file
label_optional_description: Optional description
......@@ -600,3 +600,5 @@ label_date_range: Date range
label_last_week: last week
label_yesterday: yesterday
label_last_month: last month
label_add_another_file: Add another file
label_optional_description: Optional description
......@@ -603,3 +603,5 @@ label_date_range: Date range
label_last_week: last week
label_yesterday: yesterday
label_last_month: last month
label_add_another_file: Add another file
label_optional_description: Optional description
......@@ -10,15 +10,24 @@ function checkAll (id, checked) {
}
}
var fileFieldCount = 1;
function addFileField() {
if (fileFieldCount >= 10) return false
fileFieldCount++;
var f = document.createElement("input");
f.type = "file";
f.name = "attachments[]";
f.name = "attachments[" + fileFieldCount + "][file]";
f.size = 30;
p = document.getElementById("attachments_p");
var d = document.createElement("input");
d.type = "text";
d.name = "attachments[" + fileFieldCount + "][description]";
d.size = 60;
p = document.getElementById("attachments_fields");
p.appendChild(document.createElement("br"));
p.appendChild(f);
p.appendChild(d);
}
function showTab(name) {
......
......@@ -222,7 +222,11 @@ width: 200px;
.required {color: #bb0000;}
.summary {font-style: italic;}
#attachments_fields input[type=text] {margin-left: 8px; }
div.attachments p { margin:4px 0 2px 0; }
div.attachments img { vertical-align: middle; }
div.attachments span.author { font-size: 0.9em; color: #888; }
/***** Flash & error messages ****/
#errorExplanation, div.flash, .nodata {
......
......@@ -44,7 +44,7 @@ class DocumentsControllerTest < Test::Unit::TestCase
:document => { :title => 'DocumentsControllerTest#test_post_new',
:description => 'This is a new document',
:category_id => 2},
:attachments => [ ActionController::TestUploadedFile.new(Test::Unit::TestCase.fixture_path + '/files/testfile.txt', 'text/plain') ]
:attachments => {'1' => {'file' => test_uploaded_file('testfile.txt', 'text/plain')}}
assert_redirected_to 'projects/ecookbook/documents'
......
......@@ -300,7 +300,7 @@ class IssuesControllerTest < Test::Unit::TestCase
post :edit,
:id => 1,
:notes => '',
:attachments => [ test_uploaded_file('testfile.txt', 'text/plain') ]
:attachments => {'1' => {'file' => test_uploaded_file('testfile.txt', 'text/plain')}}
assert_redirected_to 'issues/show/1'
j = Issue.find(1).journals.find(:first, :order => 'id DESC')
assert j.notes.blank?
......
......@@ -50,13 +50,14 @@ class IssuesTest < ActionController::IntegrationTest
post 'issues/edit/1',
:notes => 'Some notes',
:attachments => ([] << ActionController::TestUploadedFile.new(Test::Unit::TestCase.fixture_path + '/files/testfile.txt', 'text/plain'))
:attachments => {'1' => {'file' => test_uploaded_file('testfile.txt', 'text/plain'), 'description' => 'This is an attachment'}}
assert_redirected_to "issues/show/1"
# make sure attachment was saved
attachment = Issue.find(1).attachments.find_by_filename("testfile.txt")
assert_kind_of Attachment, attachment
assert_equal Issue.find(1), attachment.container
assert_equal 'This is an attachment', attachment.description
# verify the size of the attachment stored in db
#assert_equal file_data_1.length, attachment.filesize
# verify that the attachment was written to disk
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment