Skip to content

Instantly share code, notes, and snippets.

View owen2345's full-sized avatar

Owen Peredo Diaz owen2345

View GitHub Profile
@owen2345
owen2345 / config--initializers--sidekiq.rb
Last active July 28, 2022 12:36
Sidekiq auto finish once zero jobs to process
# Enable sidekiq auto quit feature (Allows to enable/disable this feature)
# ENV['SIDEKIQ_QUIT_WHEN_EMPTY'] = true/false
# 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
owen2345 / remote_file_renamer.rb
Last active March 10, 2024 16:11
activestorage rename already uploaded files (Rails rename activestorage) Local and AWS storage support
# 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
owen2345 / activestorage.rb
Created April 21, 2022 10:41
Rails Activestorage apply variant before uploading. Crop image before uploading. Process image before uploading.
# 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
owen2345 / jobs__delayed_uploader_job.rb
Last active April 6, 2022 15:56
Rails ActiveStorage: Add the ability to upload photos in background
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
end
@owen2345
owen2345 / photo.rb
Last active April 6, 2022 12:56
Rails active storage: Add the ability to rename uploaded file (supports for amazon and local)
# == 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
owen2345 / esbuild-custom-configuration.js
Created February 18, 2022 20:08
Esbuild with the ability to define custom entry points and with ability to copy static assets from node modules
#!/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
owen2345 / gist:26ab63cbe6af0f2194886c21417c49c1
Last active November 1, 2020 11:27
Configure e2e tests using cypress for dockerized projects
#// .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"
if: ${{env.FRONTEND_CHANGED}}
#// Procfile-e2e ==> Start all required services (rails, react app and then init cypress awaiting for react port)
test_api_server: rm -f tmp/pids/server-test.pid; bundle exec rails s -e test -p 3031 -b '0.0.0.0' --pid tmp/pids/server-test.pid
frontend: cd frontend/ && PORT=3030 BROWSER=none RAILS_PORT=3031 yarn start test --silent
@owen2345
owen2345 / ruby.yaml
Created August 12, 2020 15:59
micro services + micro frnotends CI
name: Content System
on:
push:
branches:
- master
- develop
- staging
pull_request:
@owen2345
owen2345 / repair_nested_params.rb
Last active June 19, 2020 15:21
Repair numbers converted into string (Rails 4)
repair_nested_params({id: '11', age: '25'}) # Sample
def repair_nested_params(obj)
obj.each { |key, value| obj[key] = parse_value(value) }
end
def parse_value(value)
return repair_nested_params(value) if value.is_a?(Hash)
return value.map(&method(:parse_value)) if value.is_a?(Array)
return value unless value.is_a?(String)
@owen2345
owen2345 / delayed_job_ext.rb
Last active March 20, 2020 10:36
Delayed job: uniqueness support, sequential execution support, keep successful jobs
# 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