Skip to content

Instantly share code, notes, and snippets.

View adrienpoly's full-sized avatar
💭
I may be slow to respond.

Adrien Poly adrienpoly

💭
I may be slow to respond.
View GitHub Profile
@kyrylo
kyrylo / colorized_logger.rb
Last active January 8, 2025 07:07
Nice colorized logs for Rails apps! With this initializer, you can instantly colorize your Rails development logs. Just copy and paste the code, and it’ll work. https://x.com/kyrylosilin/status/1852308566201237815
# frozen_string_literal: true
# config/initializers/colorized_logger.rb
# This initializer adds color to the Rails logger output. It's a nice way to
# visually distinguish log levels.
module ColorizedLogger
COLOR_CODES = {
debug: "\e[36m", # Cyan
info: "\e[32m", # Green
warn: "\e[33m", # Yellow
@jeremedia
jeremedia / ruby-structured-outputs-v4.rb
Last active March 4, 2025 19:35
Ruby implementation of OpenAI structured outputs
require 'json'
require 'dry-schema'
require 'openai'
require 'ostruct'
module StructuredOutputs
# Schema class for defining JSON schemas
class Schema
MAX_OBJECT_PROPERTIES = 100
MAX_NESTING_DEPTH = 5
class BaseClient
class APINotFound < StandardError; end
attr_reader :auth_strategy
def initialize(auth_strategy: :headers, headers: {})
@auth_strategy = auth_strategy
@headers = headers
end
@josefarias
josefarias / _nav.html.erb
Last active May 30, 2024 20:26
Rails dropdown component using helpers and Tailwind
<nav class="flex justify-end w-full px-4">
<%= dropdown do %>
<%= dropdown_link_to "Profile", edit_user_registration_path %>
<%= dropdown_link_to "Password", edit_account_password_path %>
<%= dropdown_link_to "Social Accounts", social_accounts_path %>
<%= dropdown_link_to "Accounts", accounts_path %>
<%= dropdown_link_to "Billing", subscriptions_path %>
<%= dropdown_link_to "Other Settings", other_settings_path %>
<%= dropdown_button_to "Log out", destroy_user_session_path, method: :delete %>
@markahesketh
markahesketh / cell_component.rb
Created April 25, 2024 13:43
ViewComponent Table Example
class Admin::DataTable::CellComponent < ApplicationComponent
def initialize(tag: :td, header: false, collapse: false, padding: true, numeric: false, centered: true, **args)
@args = args
@args[:tag] = tag
@args[:class] = class_names(
@args[:class],
"text-end" => numeric,
"w-1" => collapse,
"text-center" => centered,
@ryanb
ryanb / application.html.erb
Created April 20, 2024 05:21
Simple GetText solution for JavaScript for use with translation.io
<%# app/views/layouts/application.html.erb %>
...
<%= javascript_include_tag "locales/#{I18n.locale}", nonce: true %>
@julianrubisch
julianrubisch / convert_to_webp.rb
Last active October 4, 2024 19:46
Ruby Oneliners to convert images to webp and generate thumbnails
require 'fileutils'
# Loop through all .jpg and .png files in the current directory
Dir.glob("{*.jpg,*.png}").each do |img|
# Construct the output filename with .webp extension
output_filename = "#{File.basename(img, File.extname(img))}.webp"
# Execute ffmpeg command to convert the image
system("ffmpeg -i '#{img}' '#{output_filename}'")
end
@dhh
dhh / linux-setup.sh
Last active February 28, 2025 16:19
linux-setup.sh
# THIS LINUX SETUP SCRIPT HAS MORPHED INTO A WHOLE PROJECT: HTTPS://OMAKUB.ORG
# PLEASE CHECKOUT THAT PROJECT INSTEAD OF THIS OUTDATED SETUP SCRIPT.
#
#
# Libraries and infrastructure
sudo apt update -y
sudo apt install -y \
docker.io docker-buildx \
build-essential pkg-config autoconf bison rustc cargo clang \
@yann120
yann120 / terminals.json
Created January 17, 2024 14:31
terminals.json file to automatically start your commands on start of your workflow
{
"autorun": true,
"autokill": true,
"terminals": [{
"name": "Server",
"description": "Rails Server",
"open": true,
"focus": true,
"commands": ["bundle install", "rails db:migrate", "rails server"]
}, {
@fractaledmind
fractaledmind / has_many_attached.rb
Created October 25, 2023 20:21
Vanilla Rails isomorphic attachment validations
# /app/models/concerns/has_many_attached.rb
module HasManyAttached
extend ActiveSupport::Concern
class_methods do
def has_many_attached(name, dependent: :purge_later, service: nil, strict_loading: false, **options)
super(name, dependent: :purge_later, service: nil, strict_loading: false)
if options[:file_types].any?
validate "validate_#{name}_file_types".to_sym