redirect to new files with the same path as deleted files
Change-Id: I7b5e9736bc06901dde4f16a493f15e5c5f5124ae Reviewed-on: https://gerrit.instructure.com/5038 Tested-by: Hudson <hudson@instructure.com> Reviewed-by: Brian Palmer <brianp@instructure.com> Reviewed-by: Brian Whitmer <brian@instructure.com>
This commit is contained in:
parent
8f4265e860
commit
0e3265f26f
|
@ -158,6 +158,7 @@ class FilesController < ApplicationController
|
|||
end
|
||||
|
||||
def show
|
||||
original_params = params.dup
|
||||
params[:id] ||= params[:file_id]
|
||||
get_context
|
||||
if @context && !@context.is_a?(User)
|
||||
|
@ -171,6 +172,13 @@ class FilesController < ApplicationController
|
|||
@context = UserProfile.new(@context) if @context == @current_user
|
||||
add_crumb(t('#crumbs.files', "Files"), named_context_url(@context, :context_files_url)) unless @skip_crumb
|
||||
if @attachment.deleted?
|
||||
# before telling them it's deleted, try to find another active attachment with the same full path
|
||||
if new_attachment = Folder.find_attachment_in_context_with_path(@context, @attachment.full_display_path)
|
||||
original_params[:id] = new_attachment.id
|
||||
redirect_to original_params
|
||||
return
|
||||
end
|
||||
|
||||
flash[:notice] = t 'notices.deleted', "The file %{display_name} has been deleted", :display_name => @attachment.display_name
|
||||
if params[:preview] && @attachment.mime_class == 'image'
|
||||
redirect_to '/images/blank.png'
|
||||
|
@ -255,17 +263,7 @@ class FilesController < ApplicationController
|
|||
end
|
||||
end
|
||||
|
||||
if !@attachment
|
||||
# The relative path is for a different file, try to find it
|
||||
components = path.split('/')
|
||||
component = components.shift
|
||||
@context.folders.active.find_all_by_parent_folder_id(nil).each do |folder|
|
||||
if folder.name == component
|
||||
@attachment = folder.find_attachment_with_components(components.dup)
|
||||
break if @attachment
|
||||
end
|
||||
end
|
||||
end
|
||||
@attachment ||= Folder.find_attachment_in_context_with_path(@context, path)
|
||||
|
||||
raise ActiveRecord::RecordNotFound if !@attachment
|
||||
params[:id] = @attachment.id
|
||||
|
|
|
@ -291,6 +291,18 @@ class Folder < ActiveRecord::Base
|
|||
end
|
||||
end
|
||||
|
||||
def self.find_attachment_in_context_with_path(context, path)
|
||||
components = path.split('/')
|
||||
component = components.shift
|
||||
context.folders.active.find_all_by_parent_folder_id(nil).each do |folder|
|
||||
if folder.name == component
|
||||
attachment = folder.find_attachment_with_components(components.dup)
|
||||
return attachment if attachment
|
||||
end
|
||||
end
|
||||
nil
|
||||
end
|
||||
|
||||
def find_attachment_with_components(components)
|
||||
component = components.shift
|
||||
if components.empty?
|
||||
|
|
|
@ -253,6 +253,22 @@ describe FilesController do
|
|||
@module.evaluate_for(@user, true, true).state.should eql(:unlocked)
|
||||
end
|
||||
|
||||
it "should redirect to an existing attachment with the same path as a deleted attachment" do
|
||||
course_with_student_logged_in(:active_all => true)
|
||||
old_file = course_file
|
||||
old_file.display_name = 'holla'
|
||||
old_file.save
|
||||
old_file.destroy
|
||||
|
||||
new_file = course_file
|
||||
new_file.display_name = 'holla'
|
||||
new_file.save
|
||||
|
||||
get 'show', :course_id => @course.id, :id => old_file.id
|
||||
response.should be_redirect
|
||||
response.redirected_to['id'].should == new_file.id
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
describe "GET 'show_relative'" do
|
||||
|
|
Loading…
Reference in New Issue