Skip to content

Instantly share code, notes, and snippets.


Owen Peredo Diaz owen2345

View GitHub Profile
owen2345 / config--initializers--sidekiq.rb
Last active Jul 28, 2022
Sidekiq auto finish once zero jobs to process
View config--initializers--sidekiq.rb
# Enable sidekiq auto quit feature (Allows to enable/disable this feature)
# Apply manager patch
require_relative '../lib/sidekiq_manager_patch'
require 'sidekiq/manager'
Sidekiq::Manager.send :prepend, SidekiqManagerPatch
# Add auto quit middleware
require_relative '../lib/sidekiq_quit_when_empty'
owen2345 / remote_file_renamer.rb
Last active Sep 29, 2022
activestorage rename already uploaded files (Rails rename activestorage) Local and AWS storage support
View remote_file_renamer.rb
# frozen_string_literal: true
module Storage
class RemoteFileRenamer < ApplicationService
attr_reader :file_blob, :new_name, :file
# @param file [ActiveStorage::File]
# @param new_name [String]
# @param variation [Symbol] Sample: :thumbnail
def initialize(file, new_name, variation = nil)
owen2345 / activestorage.rb
Created Apr 21, 2022
Rails Activestorage apply variant before uploading. Crop image before uploading. Process image before uploading.
View activestorage.rb
# config/initializers/activestorage.rb
# Ability to auto apply :default variant (if defined) before uploading original image
Rails.application.config.after_initialize do
ActiveStorage::Blob.class_eval do
alias_method :upload_without_unfurling_orig, :upload_without_unfurling
def upload_without_unfurling(io)
variant = attachments.first.send(:variants)
default_variant = variant[:default]
owen2345 / jobs__delayed_uploader_job.rb
Last active Apr 6, 2022
Rails ActiveStorage: Add the ability to upload photos in background
View jobs__delayed_uploader_job.rb
class DelayedUploaderJob < ApplicationJob
queue_as :default
attr_reader :model, :attr_name
def perform(model_klass, id, attr_name)
@model = model_klass.constantize.find(id)
@attr_name = attr_name
upload_photo if tmp_file_data
owen2345 / photo.rb
Last active Apr 6, 2022
Rails active storage: Add the ability to rename uploaded file (supports for amazon and local)
View photo.rb
# == Schema Information
# Table name: photos
# id :integer not null, primary key
# position :integer
# created_at :datetime not null
# updated_at :datetime not null
class Photo < ApplicationRecord
owen2345 / esbuild-custom-configuration.js
Created Feb 18, 2022
Esbuild with the ability to define custom entry points and with ability to copy static assets from node modules
View esbuild-custom-configuration.js
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const esbuild = require('esbuild')
// Scan entrypoints
function scanFiles(dir, options = {}, result = []) {
let { recursive = false, ext = null } = options;
fs.readdirSync(dir).forEach(file => {
owen2345 / gist:26ab63cbe6af0f2194886c21417c49c1
Last active Nov 1, 2020
Configure e2e tests using cypress for dockerized projects
View gist:26ab63cbe6af0f2194886c21417c49c1
#// .github/actions/ruby.yml => Google actions
# Note: Includes error silence to avoid invalid "exit 0" when opening cypress service (cypress bug)
- name: E2e tests
run: docker-compose run test_content_service bash -c "CI=true foreman start -f Procfile-test-e2e 2> /dev/null"
#// Procfile-e2e ==> Start all required services (rails, react app and then init cypress awaiting for react port)
test_api_server: rm -f tmp/pids/; bundle exec rails s -e test -p 3031 -b '' --pid tmp/pids/
frontend: cd frontend/ && PORT=3030 BROWSER=none RAILS_PORT=3031 yarn start test --silent
owen2345 / ruby.yaml
Created Aug 12, 2020
micro services + micro frnotends CI
View ruby.yaml
name: Content System
- master
- develop
- staging
owen2345 / repair_nested_params.rb
Last active Jun 19, 2020
Repair numbers converted into string (Rails 4)
View repair_nested_params.rb
repair_nested_params({id: '11', age: '25'}) # Sample
def repair_nested_params(obj)
obj.each { |key, value| obj[key] = parse_value(value) }
def parse_value(value)
return repair_nested_params(value) if value.is_a?(Hash)
return if value.is_a?(Array)
return value unless value.is_a?(String)
owen2345 / delayed_job_ext.rb
Last active Mar 20, 2020
Delayed job: uniqueness support, sequential execution support, keep successful jobs
View delayed_job_ext.rb
# frozen_string_literal: true
# config/initializers/delayed_job_ext.rb
# unique jobs across workers
# Sample: my_model.delay(across_uniq_key: 'article-19').create
# Sample: my_model.delay(across_uniq_key: 'article-19').update
# Sample: my_model.delay(across_uniq_key: 'article-19').destroy
# ==> the jobs will be processed in serial, not in parallel:
# can not call update before create or run both at the same time
Delayed::Job.class_eval do