Search code examples
ruby-on-railsuploaddownloadmongoiddocuments

Rails file storage with generate/download/upload and state


My web platform helps our association manage projects. Many documents are auto-generated by the platform, then our members are expected to generate them, and eventually download-check-reupload them. We also need to keep track of different information like date of signature, of reading, etc.

Currently, a base class is used to represent such documents, which is then inherited to define every document type we use (for example documents are auto-generatable or not, can be uploaded, etc.). The files need to be store outside the rails app folder. The complete filename/path/generation is defined in every inherited class

Is there an easy way to achieve this with paperclip/carrierwave ? Or should I rather implement this myself (eventually drawing some inspiration from the source code of aforementionned gems ?)

I'm afraid these gems may be too limited for what I want to do. For example : when autogenerating the file, eventually the existing file will be overwritten, is this likely to mess up with paperclip/carrierwave ? Or do I need to do extra things to make it work as intended ?

Note : I am using rails 4.1 with mongoid.


Solution

  • So I decided to use Carrierwave. As I expected, it was not enough to just generate files using other gems, because then they weren't recognised.

    So in my controller, after the file is generated, I must explicitely tell carrierwave to overwrite the same file at the same place :

    def generate_document
        if @doc.generate_document # Generate the file and store it
            flash.notice = "Sir yes sir ! #{t(@doc.human_name)} successfully generated !"
    
            # Must do this because otherwise file isn't recognised by carrierwave :
            @doc.document_file = File.open(@doc.full_path)
    
            @doc.save!
        else
            flash.alert = "Krap, it failed..."
        end
        redirect_to ...
    end
    

    Where document_file is mount_uploader :document_file, DocumentFileUploader of my document class

    Unless someone provides a better answer, I'll select mine.