Skip to content

Instantly share code, notes, and snippets.

View mihaic195's full-sized avatar
🎯
Focusing

Mihai C mihaic195

🎯
Focusing
View GitHub Profile
@mihaic195
mihaic195 / assertions.ts
Last active July 6, 2021 14:02
Utils used for validating certain input. Typeguards: https://gist.github.com/mihaic195/d69191314ec79584afbff7ad38b9d0d1
import { isNonEmptyString, isUuidV4, UuidV4 } from './typeguards';
export function assertNonEmptyString(
value: unknown,
msgOrExceptionFactory?: string | (() => Error) | Error,
/** auto-trim, default true */
trim?: boolean
): asserts value is string {
const autoTrim = trim ?? true;
if (isNonEmptyString(value, autoTrim)) {
@mihaic195
mihaic195 / typeguards.ts
Created July 6, 2021 14:01
Utils used for validating certain input
import is from '@sindresorhus/is';
import { validate as uuidValidate } from 'uuid';
import { version as uuidVersion } from 'uuid';
export function assertNever(value: never, msg?: string): never {
throw new Error(isNonEmptyString(msg) ? msg : `Unexpected value: ${value}`);
}
export function isString(value: unknown): value is string {
return typeof value === 'string';
@mihaic195
mihaic195 / index.js
Created May 12, 2020 16:24
Understanding Webpack: Step 1
myPage();
@mihaic195
mihaic195 / my-page.js
Created May 12, 2020 16:24
Understanding Webpack: Step 1
function myPage() {
console.log("My page function");
}
@mihaic195
mihaic195 / index.html
Created May 12, 2020 16:21
Understanding Webpack: Step 1
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>My Page</title>
</head>
<body>
<script type="text/javascript" src="./src/index.js"></script>
<script type="text/javascript" src="./src/my-page.js"></script>
</body>
@mihaic195
mihaic195 / duplicate_active_storage.rb
Created September 10, 2019 09:27
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
@mihaic195
mihaic195 / paperclip.rake
Created September 10, 2019 09:25
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?
@mihaic195
mihaic195 / sql_query.rb
Created September 10, 2019 09:23
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
@mihaic195
mihaic195 / 20190808082353_remove_paperclip_columns_from_models.rb
Created September 5, 2019 12:29
Migration to remove paperclip columns from models
class RemovePaperclipColumnsFromModels < ActiveRecord::Migration[5.2]
def up
remove_column :model, :logo_content_type
remove_column :model, :logo_file_name
remove_column :model, :logo_file_size
remove_column :model, :logo_fingerprint
remove_column :model, :logo_updated_at
end
def down
@mihaic195
mihaic195 / blob_observer.rb
Created September 3, 2019 14:09
Blob observer
class BlobObserver < ActiveRecord::Observer
observe ActiveStorage::Blob
def after_save(blob)
blob.update_columns(storage_url: "https://s3-#{ENV['AWS_AS_S3_REGION']}.amazonaws.com/#{ENV['AWS_AS_S3_BUCKET']}/" + blob.key)
end
end