Skip to content
logicielsjpb edited this page May 9, 2017 · 15 revisions

Paperclip Attachments with Spree::Digital

Spree Digitals is an extension enabling the storage of digital assets against a Product. These can usually be downloaded instantly by customers, upon purchase, so for example covers mp3s, images, videos, documents, apps etc.

The datashift cli covering bulk upload of paperclip attachments is :

thor datashift:paperclip:attach

Documentation on this task and all parameters is here but the key fields specify the class names of the attachment and the class to attach to.

    --attachment-klass # Ruby Class name of the Attachment e.g Image, Icon, Digital
    --attach-to-klas   # Ruby Class that has a relationship with the attachment (has_many, has_one, belongs_to)
    --attach-to-field  # Attachment belongs to field e.g Product.image, Blog.digital
    --attach-to-find-by-field  # The field to use to find the :attach_to_klass record

Applying these parameter requirements to Spree and Spree::Digitals we get

datashift:paperclip:attach -a Spree::Digital -k Spree::Variant -f digitals --attach-to-find-by-field sku

In this example, our glob selects only zip files to process (with -g)

datashift:paperclip:attach -g *.zip --input /some/path -a Spree::Digital -k Spree::Variant -f digitals -l sku

Our attachment class -i.e the paperclip object type is Spree::Digital

Find the right Variant

The owning or attach_to class is a Spree Variant, which we try to find based on the sku i.e filename must contain the sku somewhere within it.

There are options available to aid this, such as --split-file-name-on which you can use to set the delimiter to progressively split the filename up, as we scan for the search term

e.g Given filename "upload_001myvar_2013.pdf" and SKU 001_myvar use --split-file-name-on "_"

--case-sensitive will use a case sensitive where clause to find :attach_to_klass

--use-like will force a loose lookup, i.e the where clause would be x LIKE 'string%' instead of ```x = 'string'`

If a Variant found with a matching SKU, we attach the SpreeDigtal to it's 'digitals' association i.e we populate

Variant.digitals << "upload_001myvar_2013.pdf"

Dummy Run

To test the bulk upload out, without actually changing the database, you can run the -d option to perform a dummy run - any changes are rolled back at the end.

Clone this wiki locally