2011-03-28 117 views
0
def restore_download_delete_file 
    begin 
     case params[:submit] 
     when "restore" 
     restore_status = restore_file(params[:file_names]) 
     raise if restore_status != 0 
     flash[:notice] = "File Successfully Restored." 
     redirect_to :action => "database_settings" 
     when "download" 
     download_status = download_file(params[:file_names]) 
     raise if download_status != 0 
     when "delete" 
     delete_status = delete_file(params[:file_names]) 
     raise if delete_status != 0 
     flash[:notice] = "File Successfully Deleted." 
     redirect_to :action => "database_settings" 
     end 
    rescue Exception => e 
     flash[:error] = "Error with #{params[:submit]}! Please retry." 
     redirect_to :action => "database_settings" 
    end 
    end 

我該如何改進這種方法?我該如何幹燥這種方法?

回答

1

嘗試這樣

begin 
    status = send("#{params[:submit]}_file", params[:file_names]) 
    raise unless status == 0 
    if params[:submit] == 'restore' || params[:submit] == 'delete' 
    flash[:notice] = "File Successfully #{params[:submit].capitalize}d" 
    end 
rescue Exception => e 
    flash[:error] = "Error with #{params[:submit]}! Please retry." 
ensure 
    redirect_to :action => "database_settings" unless params[:submit] == 'download' 
end 
+0

它看起來不錯,但我不想在任何頁面上重定向下載場景。 – 2011-03-28 12:48:51

+0

下載支票已添加重定向 – Ashish 2011-03-28 12:51:58

4

您可以通過將其分爲四個進行清理:一個用於恢復,一個用於刪除,一個用於下載,另一個用於調用合適的處理異常。

def restore_download_delete_file 
    begin 
    self.send "#{params[:submit]}" 
    rescue Exception => e 
    flash[:error] = "Error with #{params[:submit]}! Please retry." 
    redirect_to :action => "database_settings" 
    end 
end 

def restore 
    restore_status = restore_file(params[:file_names]) 
    raise if restore_status != 0 
    flash[:notice] = "File Successfully Restored." 
    redirect_to :action => "database_settings" 
end 

def download 
    download_status = download_file(params[:file_names]) 
    raise if download_status != 0 
end 

def delete 
    delete_status = delete_file(params[:file_names]) 
    raise if delete_status != 0 
    flash[:notice] = "File Successfully Deleted." 
    redirect_to :action => "database_settings" 
end 

此外,幾個注意事項:

  • 提高適當的異常,而不是零。
  • 不要拯救所有例外。代之以拯救那些你正在養的人。
+0

您正在覆蓋現有的方法名稱,如restore_file,download_file等。 – Ashish 2011-03-28 12:25:00

+0

@Ashish:現有的方法是什麼? – kikito 2011-03-28 12:28:05

+0

在您的restore_file方法中,restore_file(params [:file_names])調用存在。這意味着它已經在某個地方定義了。 – Ashish 2011-03-28 12:36:14

0
def restore_download_delete_file 
    submit = params[:submit] 
    if not restore_file(params[:file_names]).zero? 
     flash[:error] = "Error with #{submit}! Please retry." 
    elsif submit != "download" 
     flash[:notice] = "File Successfully #{submit.capitalize}d." 
    else 
     return 
    end 
    redirect_to :action => "database_settings" 
end 
0

您可以隨時將您的巨型法進一點解釋:

private 
FILE_CMDS = { 
    'delete' => { 
     :action => lambda { |names| delete_file(names) }, 
     :notice => 'File Successfully Deleted.', 
     :redirect => 'database_settings', 
    }, 
    'download' => { 
     :action => lambda { |names| download_file(names) }, 
    }, 
    'restore' => { 
     :action => lamba { |names| restore_file(names) }, 
     :notice => 'File Successfully Restored.', 
     :redirect => 'database_settings', 
    }, 
} 

public 
def restore_download_delete_file 
    begin 
     cmd = FILE_CMDS[params[:submit]] 
     status = cmd[:action].call(params[:file_names]) 
     raise if(status != 0) 
     flash[:notice] = cmd[:notice] if(cmd[:notice]) 
     redirect_to :action => cmd[:redirect] if(cmd[:redirect]) 
    rescue Exception => e 
     flash[:error] = "Error with #{params[:submit]}! Please retry." 
     redirect_to :action => "database_settings" 
    end 
end 

但我認爲egarcia的做法是很有道理的;沒有必要將所有這些東西混合到一個方法中。共同點 確實非常小,因此一個控制器中的四個方法使得 更有意義:一個動作,一個方法。 DRY只是一個指導方針,並不是不惜一切代價遵循教條。