我有以下帮助方法,用于将一些记录上传到 Dropbox。对于三个模型(日志、飞机和进近)中的每一个,我都在做完全相同的事情。我想让这个更干燥,并且只有一次代码,但我不明白如何以抽象的方式引用模型。
有什么建议吗?
#------------------------------------------------------------
# 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