Skip to content

Instantly share code, notes, and snippets.

View skatkov's full-sized avatar
🏠
Working from home

Stanislav (Stas) Katkov skatkov

🏠
Working from home
View GitHub Profile
@lazaronixon
lazaronixon / _form.html.erb
Last active April 19, 2025 13:32
Hotwire Event-Driven Update Pattern
<%= form_with model: citizen, class: "card flex flex-col gap", data: { controller: "form" } do |form| %>
<div class="flex flex-col gap mb-2">
<div class="flex flex-col gap-half">
<% countries = Country.order(:name) %>
<%= label_tag :country_id, "Country", class: "text-sm font-medium leading-none" %>
<%= select_tag :country_id, options_from_collection_for_select(countries, :id, :name, citizen.country_id), include_blank: "Select one", class: "input", data: { action: "form#submit", form_submitter_param: "on_country_change" } %>
</div>
<div class="flex flex-col gap-half">
<% states = State.where(country_id: citizen.country_id).order(:name) %>
@marckohlbrugge
marckohlbrugge / assistant.rb
Created January 12, 2025 22:56
Overview of my LLM-powered prototype. Very rough code.
# Add these gems to your Gemfile
gem "rails" # not strictly needed, but I use ActiveConcern, etc
gem "raix" # helpful gem to reduce code needed for function calling etc
gem "thor" # to make a CLI app (not needed if you make a web app)
gem "http" # my preferred gem to make API calls
# This is the main app (`app/models/ai_chat/cli.rb`)
module AIChat
class Cli
# Thor
@mxgrn
mxgrn / audit-logs.sql
Created December 9, 2024 10:13
Track Postgres table changes in audit_logs
-- Step 1: Create the audit logs table
CREATE TABLE audit_logs (
id SERIAL PRIMARY KEY,
table_name TEXT NOT NULL,
operation TEXT NOT NULL,
json_diff JSONB,
inserted_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Step 2: Create the trigger function

Implementing Advisory Locks in SQLite3

In my Rails model, I initially had a function to handle advisory locks in PostgreSQL. Here's the code, which works as expected:

# Postgresql version
def with_advisory_lock
  self.class.connection.execute("SELECT pg_advisory_lock(#{self.id})")
  yield
@egeozcan
egeozcan / index.html
Created October 18, 2024 08:40
image viewer
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Fullscreen Image Viewer</title>
<style>
body {
font-family: Arial, sans-serif;
}
@slavingia
slavingia / create_pr.sh
Created September 13, 2024 17:13
Create a (draft) pull request using GitHub CLI
#!/bin/bash
# Create a (draft) pull request using GitHub CLI.
# It assigns the PR to the current user, fills in the title from the first commit,
# and uses the PR template file for the description.
set -euo pipefail
# Colors for output
RED='\033[0;31m'

Ruby: The future of frozen string literals

What is a literal?

In programming languages, literals are textual representations of values in the source code. This is a syntactical concept.

Some examples:

7 # integer literal
name: CI
on: push
jobs:
brakeman:
name: Brakeman
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: ruby/setup-ruby@v1
with:
@hschne
hschne / rate_limit.rb
Created January 11, 2024 17:02
Leaky Bucket Rate Limiter in Ruby
frozen_string_literal: true
# A leaky bucket rate limiter for Ruby
#
# @see https://www.mikeperham.com/2020/11/09/the-leaky-bucket-rate-limiter/
# @see https://en.wikipedia.org/wiki/Leaky_bucket
class RateLimit
class Error < StandardError
attr_accessor :retry_in
@searls
searls / .solargraph.yml
Last active September 5, 2024 17:57 — forked from DRBragg/.solargraph.yml
My config with steps to use solargraph for Rails projects in VS Code (WIP)
---
include:
- ".solargraph_definitions.rb"
- "app/**/*.rb"
- "config/**/*.rb"
- "lib/**/*.rb"
exclude:
- test/**/*
- vendor/**/*
- ".bundle/**/*"