2013-01-09 144 views
0

我使用以下幫助程序方法將一些記錄上傳到Dropbox。對於三種模型(日誌,飛機和方法)中的每一種,我都在做同樣的事情。我想讓這個更幹,只有一次代碼,但我不明白如何以抽象的方式引用模型。DRY在Ruby on Rails中使用模型

有什麼建議嗎?

#------------------------------------------------------------ 
# Upload entries to Dropbox 
#------------------------------------------------------------ 
def upload_items(items, folder, client) 

    # Go through each item and upload it to Dropbox 
    items.each do |item| 

    if folder == 'logbook' 
     # Get the file from the database to upload 
     @logbook = current_user.logbooks.find_by_sync_id(item) 
     # Upload it 
     uploaded_file = client.put_file("/logbook/#{item}.json",@logbook.to_json, overwrite = true) 
     # Reset the updated_flag in the database 
     @logbook.update_attributes(updated_flag: 0) 

    elsif folder == 'aircraft' 
     # Get the file from the database to upload 
     @aircraft = current_user.aircrafts.find_by_sync_id(item) 
     # Upload it 
     uploaded_file = client.put_file("/aircraft/#{item}.json",@aircraft.to_json, overwrite = true) 
     # Reset the updated_flag in the database 
     @aircraft.update_attributes(updated_flag: 0) 

    elsif folder == 'approaches' 
     # Get the file from the database to upload 
     @approach = current_user.approaches.find_by_sync_id(item) 
     # Upload it 
     uploaded_file = client.put_file("/approaches/#{item}.json",@approach.to_json, overwrite = true) 
     # Reset the updated_flag in the database 
     @approach.update_attributes(updated_flag: 0) 

    end 
    end 
end 

的Ruby 1.9.3,Rails的3.2.8

回答

1

這將做的工作。

下面的關鍵部分是current_user.public_send(folder.to_sym)它將文件夾名稱並將其轉換爲消息發送到current_user。

#------------------------------------------------------------ 
# Upload entries to Dropbox 
#------------------------------------------------------------ 
def upload_items(items, folder, client) 

    # Go through each item and upload it to Dropbox 
    items.each do |item| 
    # Get the file from the database to upload 
    resource = current_user.public_send(folder.pluralize.to_sym).find_by_sync_id(item) 
    # Upload it 
    uploaded_file = client.put_file("/#{folder}/#{item}.json",resource.to_json, overwrite = true) 
    # Reset the updated_flag in the database 
    resource.update_attributes(updated_flag: 0) 
    end 
end 
+0

這看起來太棒了。儘管我在'resource = current_user'這一行發生了一個錯誤。 * NoMethodError(未定義方法'logbook'爲#<用戶:0x007fcd7bcd81d8>): app/helpers/sync_helper.rb:118:在'public_send'* –

+0

我的錯誤,文件夾「日誌」需要符號:日誌,所以.pluralize會照顧你。固定在上面。 –

+0

我也是這麼認識的。 :) 謝謝! –

0

這應該是方向:

控制器:

#------------------------------------------------------------ 
# Upload entries to Dropbox 
#------------------------------------------------------------ 
def upload_items(items, folder, client) 

    # Go through each item and upload it to Dropbox 
    items.each do |item| 
    upload_to_dropbox(items, folder, client) 
    end 
end 

的ApplicationController:

def upload_to_dropbox(item, folder, client) 
    # Get the file from the database to upload 
    @model = current_user.send(folder).find_by_sync_id(item) 
    # Upload it 
    uploaded_file = client.put_file("/aircraft/#{item}.json",@model.to_json, overwrite = true) 
    # Reset the updated_flag in the database 
    @model.update_attributes(updated_flag: 0) 
end 
+0

這完全錯了,anty-MVC。它應該是幫手或模型。 – Hauleth

1

我會寫這樣那樣:

class DropboxUploader 
    attr_reader :folder, :client 

    def initializer(folder, client) 
    @folder = folder.to_sym 
    @client = client 
    end 

    def upload(items) 
    items.each { |item| update item } 
    end 

    def resource_name 
    folder.to_s.pluralize.to_sym 
    end 

    def file_path 
    "/#{folder}/#{item}.json" 
    end 

    private 
    def find_resource_for(item) 
    current_user.public_send(resource_name).find_by_sync_id(item) 
    end 

    def update(item) 
    resource = find_resource_for item 
    client.put_file(file_path, @aircraft.to_json, true) 
    resource.update_attributes(updated_flag: 0) 
    end 
end 

不僅乾燥但更客觀也更容易測試。

編輯: 使用方法如下:

uploader = DropboxUploader.new(:aircraft, @client) 
uploader.upload(@items) 
+0

有趣。這是否會成爲我的模型中的助手類?我如何在控制器中使用它?對不起,我是RoR的新手。 :) –

+0

這看起來也是一個很好的解決方案。我希望我能將這個和另一個標記爲答案。 –

+0

它更大,因爲它會使測試變得簡單而有趣。它不僅是乾的,而且還是KISS。 – Hauleth