-
Notifications
You must be signed in to change notification settings - Fork 75
Import paperclip facilities
Paperclip is one of the premier file attachment gems for Rails.
Datashift provides facilities to create paperclip attachments from directories of files, such as images, and also attach to a parent or owning Model.
This is specifically for the use case where the paperclip attachments are stored in a class - such as Image, Icon, Asset - and this class has a relationship, such as belongs_to, with another class, such as Product, User, Document.
The instance of the owning Model (to attach TO), is found based on characteristics of the filename, so for example to attach to a parent with an attribute title of** "WarAndPeace"**, the filename should contain somewhere in it the string 'warandpeace' e.g 001_warandpeace_digital_copy.pdf
Each matching file is used to create an instance of the paperclip attachment, given by :attachment_klass.
The class with the relationship, can be specified via :attach_to_klass
Examples
Owner has_many pdfs and mp3 files as Digitals
:attachment_klass = Digital and :attach_to_klass = Owner
User has a single Image used as an avatar
:attachment_klass = Image and :attach_to_klass = User
The file name is used to lookup the instance of :attach_to_klass to assign the new attachment to, via :attach_to_find_by_field
So say we have a file called smithj_avatar.gif, and we want to lookup Users by login
:attach_to_find_by_field = login => Run a loookup based on find_by_login == 'smithj'
Once instance of :attach_to_klass found, the new attachment is assigned.
The attribute to assign new attachment to is given by :attach_to_field
Examples
:attach_to_field => digitals : Owner.digitals = attachment(Digital)
:attach_to_field => avatar : User.avatar = attachment(Image)
For a real world use case se bulk loading Spree Digital assets : https://github.com/autotelik/datashift_spree/wiki/Spree::Digitals
Thor Tasks
thor datashift:paperclip:attach
Options:
-i, --input=INPUT # The input path containing images
-g, [--glob=GLOB] # The glob to use to find files e.g. '{*.jpg,*.gif,*.png}'
-r, [--recursive] # Scan sub directories of input for images
-a, --attachment-klass=ATTACHMENT_KLASS # Ruby Class name of the Attachment e.g Image, Icon
-k, --attach-to-klass=ATTACH_TO_KLASS # A class that has a relationship with the attachment (has_many, has_one, belongs_to)
-l, --attach-to-find-by-field=ATTACH_TO_FIND_BY_FIELD # The field to use to find the :attach_to_klass record
-f, --attach-to-field=ATTACH_TO_FIELD # Attachment belongs to field e.g Product.image, Blog.digital
[--split-file-name-on=SPLIT_FILE_NAME_ON] # delimiter to progressivley split filename for lookup
# Default:
[--case-sensitive] # Use case sensitive where clause to find :attach_to_klass
[--use-like] # Use :lookup_field LIKE 'string%' instead of :lookup_field = 'string' in where clauses to find :attach_to_klass
-d, [--dummy] # Dummy run, do not actually save attachment
-x, [--skip-when-assoc] # Do not process if :attach_to_klass already has an attachment
-v, [--verbose] # Verbose logging
Examples
Owner has_many pdfs and mp3 files as Digitals ....
:attachment_klass = Digital and :attach_to_klass = Owner
User has a single Image used as an avatar ...
attachment_klass = Image and :attach_to_klass = User