Process -> Process in CarrierWave

20 April 2012 by virendra 5 comments
Well, this one is for all those who ever used CarrierWave for uploading their files(no matter which files). Now how many of us had actually extended Carrierwave to process a file (i.e writing you own processing for the file e.g In term of image reducing or increasing the quality of image)I happen to stumble upon this as all I want was to process a CSV file which is suppose to be stored in Amazon S3(on upload) using CarrierWave .

Now the aim was to extract all the information from the CSV (sound simple right ). Yes, it is but I resist the thought of putting the file in Amazon S3 server(first) and then downloading the file and to my server and then extract the data from the CSV file and dump it into the database. I want it to be more real time(since the file is small size file) I want the user to know that the data in CSV is proper or not and can or cannot data in the CSV can be dumped into database citing validations.

So I realized back when I used to consider PaperClip as my first options, paperclip used to provide me options for processing the file here the link if you want to do it for paperclip Good Luck.

Yes I knew now I have to achieve something of this sort , but howWell, looking at the uploader file generated by CarrierWave I found out that there is process method, ok now what how do you apply it.
Inspecting the CarrierWave source code ( for default resize process ) I found out that all you need is to define a method wrapped inside a module .

Now it got it.

So to define a custom processing all you need is to define method wrapped in module and include that module in CarrierWave and pass your process method as an argument to CarrierWave process method define in uploader file
Here how I achieve this.
Inside CarrierWave Uploader

class AssetUploader < CarrierWave::Uploader::Base
   include CarrierWave::CSVProcessor
   process :process_csv
end

That it that all you need to do to define your own file processing.
Just go ahead an define your process_csv method inside module define in file inside config/initializers Voila mission accomplished.
Here how I did it (my file config/initializers/csv_processor.rb)

module CarrierWave
  module CSVProcessor
    def process_csv
      ....
      ....
      ....
    end
  end
end

Go hack now with CarrierWave.

By the way if you want to explore the thought of providing custom error message for CarrierWave here a link that you might want to look at

Hope that it help you in someway or other

virendra

Follow me on Twitter

5 thoughts on “Process -> Process in CarrierWave”

  1. Hi Virendra,

    Great article, but what are your reasons for storing the process_csv logic in an initializer, wouldn’t this logic be better placed in your lib/ or app/ folder?

    David

    1. Hi David,
      Make sense I specified config/initializer/ just as an example in the post but if you feel like placing the file under lib/ or app/ go ahead .I generally prefer lib/ would be ideal place for it.

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe To Our Blog

Get access to proven marketing ideas, latest trends and best practices.

Next up home

Contact

Lets build cool stuff

Share your contact information & we will get in touch!

I want (Tell us more about your dream project)