Skip to content

Instantly share code, notes, and snippets.

@christopher-b
Created November 8, 2016 20:14
Show Gist options
  • Select an option

  • Save christopher-b/072369296c4a6ea78b6f73edad5d278b to your computer and use it in GitHub Desktop.

Select an option

Save christopher-b/072369296c4a6ea78b6f73edad5d278b to your computer and use it in GitHub Desktop.

Revisions

  1. christopher-b created this gist Nov 8, 2016.
    27 changes: 27 additions & 0 deletions cleanup.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    Attachment.where(workflow_state:'zipped').find_each do |attachment|
    delete_attachment attachment
    };nil

    def delete_attachment(attachment)
    # Don't delete parent Attachments
    return if attachment.children.present?

    # Delete associated ContentExports
    content_export_for(attachment).try(:delete)

    # Delete the db row. Trigger callbacks, then remove the row
    attachment.destroy
    attachment.delete

    # Delete file on disk and containing folder, unless there is a parent Attachment
    unless attachment.root_attachment_id
    path = File.dirname(attachment.full_filename)
    FileUtils.rm_rf(path) if File.directory? path
    end

    true
    end

    def content_export_for(attachment)
    ContentExport.where(attachment_id:attachment.id).first
    end