Skip to content

Instantly share code, notes, and snippets.

View virolea's full-sized avatar

Vincent Rolea virolea

View GitHub Profile
@virolea
virolea / deploy.yml
Created December 27, 2023 17:43
Example kamal deploy.yml file for a standard Rails setup on Digital Ocean
# Name of your application. Used to uniquely configure containers.
service: your-app-name
# Name of the container image.
image: your-org/your-app-name
# Deploy to these servers.
servers:
web:
- REPLACE_WITH_YOUR_WEB_DROPLET_IP
@virolea
virolea / client_middleware.rb
Created April 25, 2023 19:29
Code for the "A simple and pragmatic approach to fixing a memory leak" talk at RailsConf 2023
# This middleware dynamically switch a job queue based on its class name.
module TroubleshootMemoryMiddleware
module Client
class CustomQueueName
QUARANTINE_QUEUE = "quarantine"
def call(worker_class, job, queue, redis_pool)
# Jobs enqueued without ActiveJob are not wrapped.
job_class_name = if job["wrapped"].present?
@virolea
virolea / url_validator.rb
Last active February 16, 2023 17:01
Rails Custom URL validator
# Use this validator like this
#
# class User < ApplicationRecord
# validates :profile_link, url: true
# end
class UrlValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
unless valid_url?(value)
record.errors.add(attribute, :invalid_url)
@virolea
virolea / input_error.rb
Created January 21, 2022 10:16
Custom input field error for Rails forms
ActionView::Base.field_error_proc = proc do |html_tag, instance_tag|
fragment = Nokogiri::HTML.fragment(html_tag)
field = fragment.at("input,select,textarea")
html = if field
field["class"] = "#{field["class"]} is-invalid"
html = <<-HTML
#{fragment}
<p class="invalid-feedback">#{instance_tag.error_message.to_sentence}</p>
HTML
# config/routes.rb
resources :documents do
scope module: 'documents' do
resources :versions do
post :restore, on: :member
end
resource :lock
end
end
@virolea
virolea / index.js
Created October 23, 2019 09:40
Configure axios to automatically set the Rails CSRF token
import axios from 'axios'
const tokenEl = document.getElementsByName('csrf-token')[0]
if (tokenEl) {
const token = tokenEl.getAttribute('content')
axios.defaults.headers.common['X-CSRF-Token'] = token
}
axios.defaults.headers.common['Accept'] = 'application/json'
axios.defaults.headers.post['Content-Type'] = 'application/json'
@virolea
virolea / basic_auth.rb
Last active July 28, 2020 16:41
HTTP Basic Auth for Rack applications - Protect Rails app staging environment with HTTP Basic Auth
MyApp::Application.configure do
# Restrict access with HTTP Basic Auth for staging environment
if ENV['STAGING_AUTH']
config.middleware.use Rack::Auth::Basic do |username, password|
ENV['STAGING_AUTH'].split(':') == [username, password]
end
end
end
@virolea
virolea / config.yml
Created April 10, 2018 10:02
Circleci 2.0 config.yml - Rails - Postgresql - Redis
# Ruby CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-ruby/ for more details
#
version: 2
jobs:
build:
docker:
# specify the version you desire here
- image: circleci/ruby:2.4.1-node-browsers
@virolea
virolea / settings.json
Created January 17, 2018 10:36
VScode config file - iTerm - Atom keybindings - Editor settings
{
"atomKeymap.promptV3Features": true,
"editor.multiCursorModifier": "ctrlCmd",
"editor.formatOnPaste": true,
"files.insertFinalNewline": true,
"files.trimTrailingWhitespace": true,
"editor.renderWhitespace": "all",
"editor.tabSize": 2,
"explorer.confirmDragAndDrop": false,
"terminal.external.osxExec": "iTerm.app",
@virolea
virolea / upload.js
Last active March 15, 2024 13:45
Tracking file upload progress using axios
upload(files) {
const config = {
onUploadProgress: function(progressEvent) {
var percentCompleted = Math.round((progressEvent.loaded * 100) / progressEvent.total)
console.log(percentCompleted)
}
}
let data = new FormData()
data.append('file', files[0])