benol
July 7, 2009, 11:59am
1
Hi,
Rails 2.3.2, paperclip 2.2.9.2
How can I unit test my model that has a paperclip attachment?
I add to my model:
— CODE START —
has_attached_file :image, :default_url => ‘’
attr_protected :image_file_name, :image_content_type, :image_image_size
— CODE END —
Than I type a UNIT test:
— CODE START —
foo=FooModel.new({:image => File.new(‘test/fixtures/img/an_image.gif’)})
— CODE END —
This works fine until… in that model I have a validator that accesses
the “image” attribute:
— CODE START —
image.path
— CODE END —
But there is no image under that path, since it points to
RAILS_ROOT/public/system/images/… What should I do now?
benol
July 7, 2009, 1:03pm
2
Yep, you actually need to upload the picture actually.
Take a look at the following code snippet I wrote some time back,
for uploading image, resizing. (Ignore the code which is for image
cropping)
129954’s gists · GitHub
Thanks
Srinivas
On Jul 7, 2:59 pm, Michal B. [email protected]
benol
July 7, 2009, 1:36pm
3
Thanks!
How do you process the image in the model?
Before the save of the model object to database I want to add another
image to it, that is a modified version of the “image” attribute.
I do:
has_attached_file :image
has_attached_file :image2
after_validation :prepare_image2
And:
def cprepare_image2
(…)
path = image.queued_for_write[:original].path
self.image2 = some_rmagic_processing(path)
(…)
end
Is there any other way to access the uploaded image?
“image.queued_for_write[:original].path” does not look nice
May be use an other callback?