Skip to content

Instantly share code, notes, and snippets.

View owen2345's full-sized avatar

Owen Peredo Diaz owen2345

View GitHub Profile
@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)
class CommentsController < ApplicationController
def users_comments
# SOLUTION 1: a bit complicated to add pagination and its sql performance is bad when there is a lot of comments,
# because the sql query is returning all matched comments
@user_comments = Post.all.includes(:comments).eager_load(comments: :author)
.where(author: {username: params[:username]}).map(&:comments)
.flatten
# SOLUTION 2: easy to add pagination and its sql performance is much better
@user_comments = Comment.joins(:post, :author).where(author: {username: params[:username]})
@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 / 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 / main_helper.rb
Created June 15, 2016 15:41
Perspective Theme (one click install). Template url: https://p.w3layouts.com/demos/perspective/web/index.html
module Themes::PerspectiveTheme::MainHelper
def self.included(klass)
klass.helper_method [:perspective_social_networks] rescue "" # here your methods accessible from views
end
def perspective_theme_settings(theme)
# callback to save custom values of fields added in my_theme/views/admin/settings.html.erb
end
# callback called after theme installed
@owen2345
owen2345 / index.html
Created June 24, 2014 16:39
html5 encode using bootstrap 2
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<meta name="description" content="Html5 encode using bootstrap, Lesscss"/>
<meta name="keywords" content="Bootstrap, Lesscss, HTML5, CSS3, Crossbrowser"/>
<meta name="author" content="Owen Peredo Diaz"/>
<meta charset="utf-8"/>
<link rel="icon" href="img/favicon.ico" />
<title>Cruceros</title>
@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