FactoryGirl - Faker image for CarrierWave uploader - carrierwave

How can I use Faker's avatar generation with a FactoryGirl Factory ?
Faker::Avatar.image returns a URL with an image, can I somehow try to download it during a FactoryGirl create action, so I can use it as a Carrierwave image (and fallback to no image of the download fails) ?

I have a factory which is uploading faker images to Cloudinary using Carrierwave. It's not exactly what you described, but maybe helps?
Anyway, the attribute in my database is called image_location but I have to edit this in the factory (and in my seeds) to remote_[ATTRIBUTE NAME]_url
So, my factory has
remote_image_location_url Faker::Avatar.image

Related

file size validation without a gem in ruby on rails

I am new to ruby on rails.
How to validate the size of a file uploaded with file_field form without using paperclip, carrierwave or other gems in ruby on rails?
To do this, you will have to write a custom validation in your model (the model which has the file), as follow:
validate :image_size_conformance
private
def image_size_conformance
errors[:image] << "should be less than 500KB" if image.size > <the max size you want>
end
Note that the :image in errors[:image] should be the name you are calling your image column ( well, I guess this will naturally be image).
Also note that the validation example I have above only checks for the size. You can use it as a guide to write validation for any other property you want. Hope this helps...

Fabricate Paperclip attachment

I'm trying to test my photo uploads with Fabricator and paperclip, but I'm having trouble using fabricator to create a paperclip object.
My current thought process is to include this module:
http://room118solutions.com/2011/05/25/stubbing-paperclip-during-testing/
After including, I should be able to fabricate on it??
Unfortunately, I don't quite know enough about Fabricator to do this.
If you want just to test something related to upload logic, and your test suite does not include fabricating lot of objects with attachments, you can do this:
Fabricator(:fabricator_name) do
image { File.open(File.join(Rails.root, 'spec', 'fabricators', 'assets', 'image.jpg'))}
end
And place any small image named image.jpg to spec/fabricators/assets/ folder. As noted here in question.

carrierwave: point to existing image

In my rails app, I'm using Carrierwave to upload images on Amazon S3. I'd like to point to existing Amazon S3 images without having to re-upload the image. For example, if I have an existing Amazon S3 image at http://test.s3.amazonaws.com/image/path/0001/image.jpg, can I update an image's path to point to this image? I don't want to use the remote upload option because I really just want to use the same exact image that's already there (but save it in my record's "path" attribute).
In the console, I've tried:
image.update_attributes(:path=> "http://test.s3.amazonaws.com/image/path/0001/image.jpg")
but this fails to override the image's path.
Chiming in, better late than never! Caveat: This is for rails 4, and I am testing on rails 4.1 only at the moment.
This is harder than it should be, methinks! The reason this was absolutely crucial to me was that I am attaching 100MB+ MP3 files, which I cannot receive on my host, due to CloudFlare SSL limitations (and common sense). Fortunately, AWS supports preauthorized uploads, and I got carrierwave to do the right thing for me:
Step 1: get carrierwave to tell me where it would store a file if it could:
m.raw_write_attribute('file','file.mp3');
url = m.file.url
signed = aws_presigned_url(url)
raw_write_attribute does not save anything, it just bypasses carrierwave when setting the value. This makes the object act as if it read 'file.mp3' out of the database. Then you can ask Carrierwave "where the file lives". I then upload the file directly from the client to S3. When that's done, I make another API call to Rails, which performs the following code:
m.raw_write_attribute('file','file.mp3');
m.update_attribute('file','file.mp3');
These two paired get around Carrierwave. The first makes carrierwave think that the 'file' column is set to 'file.mp3', the second explicitly tells rails to persist 'file.mp3' to the DB. Because of the raw_write_attribute call, Carrierwave allows the second through un-changed.
In my case update_column and update_columns worked great:
model.update_columns file_1: 'filename.txt'
Update column is with comma:
model.update_column :file_1, 'filename.txt'
This will not run any callback and set column to filename.txt.
When I do model.file_1.url I get the right S3 URL.
I am a bit late to the party, but you can use Active Record's raw_write_attribute method something like:
#image.raw_write_attribute(:path, "http://test.s3.amazonaws.com/image/path/0001/image.jpg")
I found that you can actually do this, for example if your mount_uploader is :path, then:
image.remote_path_url = "http://test.s3.amazonaws.com/image/path/0001/image.jpg"
image.save

Carrierwave + RSpec testing

I'm testing that my file uploads are correct with models with RSpec by setting Model#filename = File.open(etc)
When my specs run, all is grand. But the files still remain in my public/uploads directory after the testsuite finishes.
How do I get the files to delete on the end of running along with the records?
Thanks!
https://github.com/jnicklas/carrierwave/wiki/How-to:-Cleanup-after-your-Rspec-tests
One way is to delete them at the end of a test using an instance method exposed by carrierwave. An example where your instance is #user and your carrierwave file attribute is avatar would be: #user.remove_avatar!

New paperclip fingerprint after modifying style?

It looks like Paperclip generates a fingerprint for an attachment only when the original style is changed. I want to keep the original style unchanged but crop one of my other styles (let's call it "listing"). Is there a way to force Paperclip to regenerate a fingerprint if the "listing" style is cropped but the "original" style is not?
I found generate_fingerprint no longer works with the latest Paperclip (4.1.1) but the refresh rake task does: e.g. rake paperclip:refresh CLASS=Dog.
In my case, I wanted the fingerprint to be created automatically when I ran the the migration that added the fingerprint, so I dug into the code and found you can call reprocess! on the attachment and that'll also do the trick.
I recently added an image_fingerprint column to an existing model that has Paperclip images attached.
I forced Paperclip to generate fingerprints for the existing images with the generate_fingerprint method on Paperclip::Attachment:
class Dog
has_attached_file :image
...
end
Dog.all.each do |dog|
dog.image_fingerprint = dog.image.generate_fingerprint(dog.image)
...
end

Resources