I think the best way is to handle such cases manually and not alter the
file_column. Something like that:
require 'open-uri'
uri = "http://somewhere.example.com/myimage.gif"
open(uri) do |remote|
uploadedfile = StringIO.new(remote.read)
end
filename = File.basename(uri)
(class << uploadedfile; self; end).class_eval do
define_method(:local_path) { "" }
define_method(:original_filename) { filename }
define_method(:content_type) { "application/octet-stream" }
end
Then assign this uploadedfile to file_column attribute.
Clare wrote:
Thank you very much for replying but sadly I'm not yet good enough to
understand your fancy code.
Sorry i usually have a better crack at solving issues but I don't
understand what is going on in the section that begins (class <<
uploadedfile; self; end).class_eval. If you get time to reply that would
be excellent, if not thanks for giving me something to investigate
further.
"class << uploadedfile; self; end" is a common technique to get
singleton class for that very instance to be able to extend it. I think
it could be done in a more simple way, I just took a code from
file_column's test method that simulate an uploaded file.
In Rails, uploaded file is an IO object or the like (which has "read"
method) extended with some instance methods (local_path,
original_filename, content_type).
Regarding your error, I think you haven't required 'open-uri' library
and this line
uploadedfile = StringIO.new(remote.read)
haven't got executed. That's probably why you've got "undefined
variable" error. I suggest you to put all the code inside open(uri)
block so you'll ensure that nothing will happen if it is unable to open
that uri.