Skip to content

Instantly share code, notes, and snippets.

View brenogazzola's full-sized avatar

Breno Gazzola brenogazzola

View GitHub Profile
@brenogazzola
brenogazzola / propshaft.rb
Created October 30, 2023 21:00
Enhance Propshaft to compress files during precompilation
require "rake"
Rake::Task["assets:precompile"].enhance do
assembly = Rails.application.assets
output_path = assembly.config.output_path
assembly.load_path.assets.each do |asset|
asset_path = output_path.join(asset.digested_path)
compressed_path = output_path.join(asset.digested_path.to_s + ".br")
@brenogazzola
brenogazzola / direct_upload.js
Created February 6, 2023 20:30
Manual Multiple File Direct Upload
import { DirectUpload } from '../rails/activestorage'
export default class {
constructor (input) {
this.input = input
this.submit = input.form.querySelector('.btn-submit') // The submit button. We show loading progress here
this.progress = this.submit.querySelector('.btn-submit-progress') // The progress bar inside the submit
this.loader = this.submit.querySelector('.btn-submit-loader') // The loader animation inside the submit
// Remove all drafts from your drafts view
// Navigate to drafts
// F12 to raise dev console
// Paste the below
(async function(x) {
for (const e of [...document.querySelectorAll('[type="trash"]')]) {
e.click();
await new Promise(resolve => setTimeout(resolve, 500))
document.querySelector('[data-qa="drafts_page_draft_delete_confirm"]').click();
await new Promise(resolve => setTimeout(resolve, 1500))
@brenogazzola
brenogazzola / rbenv_uninstall_gems.sh
Created October 17, 2022 17:44 — forked from fijimunkii/rbenv_uninstall_gems.sh
rbenv: uninstall all gems
#!/usr/bin/env bash
uninstall() {
list=`gem list --no-versions`
for gem in $list; do
gem uninstall $gem -aIx
done
gem list
gem install bundler
}
@brenogazzola
brenogazzola / fix_action_text.rb
Last active February 14, 2022 20:29
Fix ActionText Images
# After active storage urls are changed, use this to recreate all trix attachments
def self.refresh_trixes
ActionText::RichText.where.not(body: nil).find_each do |trix|
Trix::RefreshJob.perform_later(trix)
end
end
# After active storage urls are changed, use this to recreate a specific strix attachments
def self.refresh_trix(trix)
return unless trix.embeds.size.positive?
@brenogazzola
brenogazzola / direct_upload.js
Created January 20, 2022 23:41
Active Storage Direct Upload Progress
import Rails from '@rails/ujs'
const DirectUpload = {
start () {
document.addEventListener('direct-upload:initialize', DirectUpload.initialize)
document.addEventListener('direct-upload:start', DirectUpload.begin)
document.addEventListener('direct-upload:progress', DirectUpload.progress)
document.addEventListener('direct-upload-error', DirectUpload.error)
document.addEventListener('direct-upload:end', DirectUpload.end)
},
@brenogazzola
brenogazzola / middleware.rb
Last active November 11, 2021 03:51
Datadog
# frozen_string_literal: true
require "datadog/statsd"
class QueueTime
def initialize(app)
@app = app
end
def call(env)
@brenogazzola
brenogazzola / util_controller.js
Created October 5, 2021 15:45
Stimulus Controller
import { Controller } from 'stimulus'
export default class extends Controller {
// ============================================================================
// Public
// ============================================================================
/*
* Removes an element from the DOM
*/
@brenogazzola
brenogazzola / propshaft-bench-normalize.rb
Last active October 2, 2021 14:57
Benching fastest way to grab only the important part of the urls
require 'benchmark/ips'
content = %(
.prop 1 { background: url(file.jpg); }
.prop 2 { background: url( file.jpg ); }
.prop 3 { background: url("file.jpg"); }
.prop 4 { background: url('file.jpg'); }
.prop 5 { background: url('/file.jpg'); }
.prop 6 { background: url('./file.jpg'); }
.prop 7 { background: url('./images/file.jpg'); }
@brenogazzola
brenogazzola / migrate_im_to_vips.md
Last active October 10, 2024 11:16
Steps to migrate ImageMagick to Libvips

For apps built before the image_processing gem became the default, the migration will involve two steps:

  1. Migrating to the image processing syntax while still using ImageMagick;
  2. Switching to Vips and updating the compression options;

Migrate to the image processing syntax

Before changing from ImageMagick to Vips, it's better to first test the new syntax and ensure everything is still working.

1. Move everything that has to do with compression to a saver hash:

variant(format: :jpg, strip: true, quality: 80)