Downloading a Remote Image and Saving it with AttachmentFu

Filed Under (Ruby) by admin on 07-06-2008

Tagged Under : ,

I had some issues this week figuring out how to save a file using AttachmentFu. It is well suited to handling files uploaded via a Web form, but when you load a file in programmatically, it isn't obvious what object properties need to be set. Here is what I ended up with.

response = HttpClient.default.request(url)

mugshot = Mugshot.new

temp_file_name = mugshot.write_to_temp_file(response.body)

mugshot.temp_path = temp_file_name

mugshot.content_type = "image/#{content_type}"

mugshot.filename = "imported_photo.#{content_type}"

if person.mugshot

# remove existing database records of files for this person

thumbnail = Mugshot.find_by_parent_id(person.mugshot.id)

thumbnail.destroy

person.mugshot.destroy

end

mugshot.person = person

mugshot.save

Q: How do you stub a method call for a rails unit test such that it returns a…

Filed Under (Ruby) by admin on 07-06-2008

Tagged Under : ,

Q: How do you stub a method call for a rails unit test such that it returns a different value the second time it is called? A:  stubs(:foo).returns(1,2) returns 1 the first call, 2 the second, etc.