Skip to content

Instantly share code, notes, and snippets.

@bogdanRada
bogdanRada / duplicate_active_storage.rb
Created February 25, 2022 11:06 — forked from mihaic195/duplicate_active_storage.rb
Concern which duplicates attachments into ActiveStorage
module DuplicateActiveStorage
extend ActiveSupport::Concern
included do
after_commit do
duplicate_active_storage unless @is_migration
end
after_destroy :duplicate_active_storage
end
@bogdanRada
bogdanRada / paperclip.rake
Created February 25, 2022 11:06 — forked from mihaic195/paperclip.rake
Paperclip to ActiveStorage migration rake task
namespace :paperclip do
task migrate: :environment do
arr = Array.new
ApplicationRecord.descendants.reject(&:abstract_class?).each { |klass| arr.push(klass) }
while model = arr.pop
begin
attachments = model.column_names.filter { |column| column.match?(/(.+)_file_name$/) }.map { |column| column[/(?<name>\w*)_file_name/, :name] }
next if attachments.blank?
@bogdanRada
bogdanRada / sql_query.rb
Created February 25, 2022 11:05 — forked from mihaic195/sql_query.rb
Simple sql query for extracting the direct URL of an attachment
SELECT blobs.storage_url
FROM active_storage_blobs AS blobs
INNER JOIN active_storage_attachments AS attachments ON attachments.blob_id = blobs.id
WHERE attachments.record_type = 'Model1'
AND attachments.name = 'logo'
LIMIT 10
class CreateActiveStorageTables < ActiveRecord::Migration[5.2]
def change
create_table :active_storage_blobs do |t|
t.string :key, null: false
t.string :filename, null: false
t.string :content_type
t.text :metadata
t.bigint :byte_size, null: false
t.string :checksum, null: false
t.string :storage_url
@bogdanRada
bogdanRada / model.rb
Created February 25, 2022 11:04 — forked from mihaic195/model.rb
Example of paperclip model validation
validates_attachment :avatar, presence: true,
content_type: "image/jpeg",
size: { in: 0..10.kilobytes }

Docx (and other Office doc) previewer support for ActiveStorage

Converts all document formats using LibreOffice first to PDF and then to an image, using build-in pdf previwer of ActiveStorage

  • needs: unoconv, a command line tool which uses libreoffice, e.g. apt install unoconv
  • needs a PDF converter, because libreoffice will make a pdf from the office document, Mupdf oder Poppler are already integrated into ActiveStorage, and poppler is free to use, e.g. apt install poppler-utils
@bogdanRada
bogdanRada / setup-sendmail-gmail-on-ubuntu14.md
Created October 14, 2021 12:46 — forked from egatjens/setup-sendmail-gmail-on-ubuntu14.md
Configuring Gmail as a Sendmail email relay | Ubuntu 14
Setup sendmail

Prefix all commands with 'sudo'

  1. Install prerequisites
$ apt-get install sendmail mailutils
  1. Configure hosts file correctly:
@bogdanRada
bogdanRada / google-drive-mime-types.md
Created August 13, 2021 10:18 — forked from javan/google-drive-mime-types.md
Google Drive's undocumented MIME types

The Google Drive API supports these MIME types, but if you try to filter using them, you may find you're not getting the document types back that you expect. Especially when using setMimeTypes() with Google's JavaScript Picker.

Here are the poorly documented or completely undocumented MIME types I discovered:

If you want application/vnd.google-apps.document, also use application/vnd.google-apps.kix. Source.

If you want application/vnd.google-apps.spreadsheet, also use application/vnd.google-apps.ritz. Via a private email from a Google employee.

If you want application/vnd.google-apps.presentation, also use application/vnd.google-apps.punch. Source.

@bogdanRada
bogdanRada / Jenkinsfile
Created June 7, 2021 05:22 — forked from JohnnyChiang/Jenkinsfile
Extract parameters from a jenkins previous build (Declarative pipeline example)
#!groovy
pipeline {
agent any
parameters {
string(name: 'CAUSE', defaultValue: 'anonymous', description: 'Build Cause')
}
stages {
stage('Test Previous Build Status') {
steps {
@bogdanRada
bogdanRada / rvm-amazon-linux.bash
Created April 6, 2021 19:03 — forked from kainam00/rvm-amazon-linux.bash
Install RVM on Amazon Linux
#!/bin/bash
# Install prerequisites
yum install -y gcc openssl-devel libyaml-devel libffi-devel readline-devel zlib-devel gdbm-devel ncurses-devel ruby-devel gcc-c++ jq git
# Import key
curl -sSL https://rvm.io/mpapis.asc | gpg2 --import -
# Install RVM
curl -sSL https://get.rvm.io | bash -s stable --ruby