diff --git a/app/controllers/attachments_controller.rb b/app/controllers/attachments_controller.rb index 976772b8..ff28a647 100644 --- a/app/controllers/attachments_controller.rb +++ b/app/controllers/attachments_controller.rb @@ -16,7 +16,7 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. class AttachmentsController < ApplicationController - before_filter :find_project, :except => :upload + before_filter :find_project, :except => [:upload, :autocomplete] before_filter :file_readable, :read_authorize, :only => [:show, :thumbnail]#Modified by young before_filter :delete_authorize, :only => :destroy before_filter :authorize_global, :only => :upload @@ -131,6 +131,12 @@ class AttachmentsController < ApplicationController end end + def autocomplete + respond_to do |format| + format.js + end + end + private def find_project @attachment = Attachment.find(params[:id]) diff --git a/app/helpers/attachments_helper.rb b/app/helpers/attachments_helper.rb index a9080771..8691f547 100644 --- a/app/helpers/attachments_helper.rb +++ b/app/helpers/attachments_helper.rb @@ -85,4 +85,17 @@ module AttachmentsHelper Attachment.tagged_with(tag_name).order('created_on desc') end + def render_attachments_for_new_project(project) + # 查询条件 + filename_condition = "trustie" #params[:q] + # 当前项目所有资源 + # attachments = Attachment.find_all_by_container_type_and_container_id(project.class, project.id) + attachments = Attachment.where("container_type = '#{project.class}' and container_id = #{project.id}") + # 除去当前项目的所有资源 + nobelong_attach = Attachment.where("container_type <> '#{project.class}' and container_id <> #{project.id}") + # 搜索到的资源 + searched_attach = nobelong_attach.where("filename like '%#{filename_condition}%' ") + + return searched_attach.to_yaml + end end diff --git a/app/models/attachment.rb b/app/models/attachment.rb index e669428f..768be578 100644 --- a/app/models/attachment.rb +++ b/app/models/attachment.rb @@ -169,7 +169,7 @@ class Attachment < ActiveRecord::Base def deletable?(user=User.current) if container_id - container && container.attachments_deletable?(user) + container && (container.is_a?(Project) ? container.attachments_deletable?(user) : true ) else author == user end diff --git a/app/views/attachments/_links.html.erb b/app/views/attachments/_links.html.erb index 81eb416a..c135ee21 100644 --- a/app/views/attachments/_links.html.erb +++ b/app/views/attachments/_links.html.erb @@ -9,14 +9,14 @@ <%= h(" - #{attachment.description}") unless attachment.description.blank? %> (<%= number_to_human_size attachment.filesize %>) <% if options[:deletable] %> - <% unless attachment.container_type == 'HomeworkAttach' %> - <%= link_to image_tag('delete.png'), attachment_path(attachment), + <% if attachment.container_type == 'HomeworkAttach' %> + <%= link_to image_tag('delete.png'), {:controller => 'attachments', :action => 'delete_homework', :id => attachment.id}, :data => {:confirm => l(:text_are_you_sure)}, :method => :delete, :class => 'delete', :title => l(:button_delete) %> <% else %> - <%= link_to image_tag('delete.png'), {:controller => 'attachments', :action => 'delete_homework', :id => attachment.id}, + <%= link_to image_tag('delete.png'), attachment_path(attachment), :data => {:confirm => l(:text_are_you_sure)}, :method => :delete, :class => 'delete', diff --git a/app/views/attachments/autocomplete.js.erb b/app/views/attachments/autocomplete.js.erb new file mode 100644 index 00000000..1ff7227f --- /dev/null +++ b/app/views/attachments/autocomplete.js.erb @@ -0,0 +1,2 @@ +<% a= render_attachments_for_new_project(Project.find(2)) %> +<%=a%> \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 882544a5..51ac10d8 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -385,6 +385,8 @@ RedmineApp::Application.routes.draw do get 'attachments/download/:id/:filename', :to => 'attachments#download', :id => /\d+/, :filename => /.*/, :as => 'download_named_attachment' get 'attachments/download/:id', :to => 'attachments#download', :id => /\d+/ get 'attachments/thumbnail/:id(/:size)', :to => 'attachments#thumbnail', :id => /\d+/, :size => /\d+/, :as => 'thumbnail' + get 'attachments/autocomplete' + match 'attachments/autocomplete', :to => 'attachments#autocomplete', via: [:get, :post] resources :attachments, :only => [:show, :destroy] resources :groups do diff --git a/test/fixtures/attachments.yml b/test/fixtures/attachments.yml index 6384c8ee..33bd6015 100644 --- a/test/fixtures/attachments.yml +++ b/test/fixtures/attachments.yml @@ -267,3 +267,31 @@ attachments_020: filename: root_attachment.txt filesize: 54 author_id: 2 +attachments_021: + content_type: text/plain + downloads: 0 + created_on: 2012-05-12 16:14:50 +09:00 + disk_filename: 120512161450_project_test.txt + disk_directory: + container_id: 2 + digest: b0fe2abdb2599743d554a61d7da7ff74 + id: 21 + container_type: Project + description: "" + filename: project_test.txt + filesize: 54 + author_id: 2 +attachments_022: + content_type: text/plain + downloads: 0 + created_on: 2012-05-12 16:14:50 +09:00 + disk_filename: 120512161450_course_test.txt + disk_directory: + container_id: 1 + digest: b0fe2abdb2599743d554a61d7da7ff74 + id: 22 + container_type: Project + description: "" + filename: course_test.txt + filesize: 54 + author_id: 2 \ No newline at end of file diff --git a/test/unit/attachment_test.rb b/test/unit/attachment_test.rb new file mode 100644 index 00000000..bd3f4659 --- /dev/null +++ b/test/unit/attachment_test.rb @@ -0,0 +1,20 @@ +# encoding: utf-8 +require 'test_helper' + +class AttachmentsTest < ActiveSupport::TestCase + include AttachmentsHelper + + def setup + @file_belong_to = attachments(:attachments_021) + @file_not_belong_to = attachments(:attachments_022) + @project = @file_belong_to.container + end + + test "can be find file not belong to project" do + params = {:q => @file_not_belong_to} + found_file = render_attachments_for_new_project(@project) + assert found_file.include?(@file_not_belong_to) + assert_not found_file.include?(@file_belong_to) + end + +end