Skip to content

Instantly share code, notes, and snippets.

@hemju
Last active August 29, 2015 14:03
Show Gist options
  • Save hemju/94713cacdbac9d08071d to your computer and use it in GitHub Desktop.
Save hemju/94713cacdbac9d08071d to your computer and use it in GitHub Desktop.
Blog: Dynamic Ruby Example Example
def import_file_data(file_path, content, import_type, strategy_name, additional_attributes = {}, options = {})
# probably file_path is a String and the result file_name as well
file_name = File.basename(file_path)
# no idea what tempfile is, a string?
tempfile = write_to_temp_file(content, file_name)
uploaded_file = OpenStruct.new(:original_filename => file_name, :tempfile => tempfile)
# Is this now an instance of ResourceImport or a result of a resource import?
resource_import = ResourceImport.import!(user, project, uploaded_file, file_path, import_type, strategy_name, additional_attributes)
# Ok, nothing ambiguous here
resource_uploader = ResourceUploader.new(resource_import)
resource_uploader.store!(tempfile)
if options.fetch(:schedule, true)
ResourceImportsService.new.schedule_import(resource_import)
else
import_resource_import(resource_import)
end
# maybe reload already returns the resource_import new and we don't need the last line at all? resource_import.reload
# implicit returns always felt 'dirty' to me. For example ActiveRecord callbacks which return false
# break the chain, so you have to know that to not break it unintentionally
resource_import.reload
resource_import
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment