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
@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 April 26, 2024 08:27
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 May 10, 2024 16:55
linux-setup.sh
# CLI
sudo apt update -y
sudo apt install -y \
git curl \
docker.io docker-buildx \
build-essential pkg-config autoconf bison rustc cargo clang \
libssl-dev libreadline-dev zlib1g-dev libyaml-dev libreadline-dev libncurses5-dev libffi-dev libgdbm-dev libjemalloc2 \
libvips imagemagick libmagickwand-dev mupdf mupdf-tools \
redis-tools sqlite3 libsqlite3-0 libmysqlclient-dev \
rbenv apache2-utils
@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
@donnfelker
donnfelker / attached_validator.rb
Last active October 28, 2023 02:51
Custom Active Storage Validator
class AttachedValidator < ActiveModel::EachValidator
# Active Storage validator to ensure that an attachment is attached.
#
# usage:
# validates :upload, attached: true
#
def validate_each(record, attribute, _value)
return if record.send(attribute).attached?
errors_options = {}
@peterc
peterc / embedding_store.rb
Last active December 28, 2023 06:27
Using SQLite to store OpenAI vector embeddings from Ruby
# Example of using SQLite VSS with OpenAI's text embedding API
# from Ruby.
# Note: Install/bundle the sqlite3, sqlite_vss, and ruby-openai gems first
# OPENAI_API_KEY must also be set in the environment
# Other embeddings can be used, but this is the easiest for a quick demo
# More on the topic at
# https://observablehq.com/@asg017/introducing-sqlite-vss
# https://observablehq.com/@asg017/making-sqlite-extension-gem-installable
@kddnewton
kddnewton / json.rb
Last active August 7, 2023 19:54
Faster JSON parser in Ruby
# frozen_string_literal: true
require "bundler/inline"
gemfile do
source "https://rubygems.org"
gem "json_pure"
gem "benchmark-ips"
end
@skatkov
skatkov / test.rb
Last active January 3, 2024 18:20
speed up testsuit by using unlogged tables in PG
# config/environments/test.rb
ActiveSupport.on_load(:active_record_postgresqladapter) do
# For this optimization to work, you need to recreate your test database
self.create_unlogged_tables = true
end