Skip to content

Instantly share code, notes, and snippets.

View glaszig's full-sized avatar

glaszig

  • available for hire. contact me.
  • remote
View GitHub Profile
@luizkowalski
luizkowalski / cloudflare_waf.rb
Created April 7, 2026 13:46
Block scanners from hitting the app and messing with the metrics
#!/usr/bin/env ruby
# frozen_string_literal: true
require "http"
require "json"
ZONE_ID = ENV.fetch("CF_ZONE_ID")
ZONE_TOKEN = ENV.fetch("CF_ZONE_TOKEN")
PHASE = "http_request_firewall_custom"
DESCRIPTION = "Block application scanners"
@stefanvermaas
stefanvermaas / separator.rb
Created March 10, 2026 18:55
Automatically detect the column separator in a CSV file
require "csv"
class CSV
class Separator
DEFAULT_SEPARATOR = ","
class Error < StandardError; end
class DetectionError < Error
def message
@joeywang
joeywang / unscope.md
Last active April 22, 2026 04:50
The Power of Unscope in Ruby on Rails

The Power of Unscope in Ruby on Rails

In Ruby on Rails, scopes are a convenient way to encapsulate common query patterns, making it easier to write clean and reusable code. However, there are times when you might need to deviate from these patterns, and that's where the unscope method comes in.

Understanding unscope

The unscope method allows you to selectively remove certain scopes from your ActiveRecord queries. This can be useful when you need to perform a query that doesn't conform to your usual patterns or when you need to override a default scope.

Default Scope Example

@franzwong
franzwong / README.md
Last active March 20, 2026 21:49
Start VM with QEMU on MacOS (Apple silicon cpu)

Start VM with QEMU on MacOS

  1. Install qemu
brew install qemu
  1. Download QEMU_EFI.fd
@loilo
loilo / -alpine-select.md
Last active October 19, 2025 07:15
Select Dropdown with Alpine.js

Alpine.js x-select Directive

x-select is an Alpine.js directive which provides a styled enhancement over a native <select> element.

Its main goal is to be a progressive enhancement adding no actual features to a select dropdown apart from making it more visually appealing to use (especially for select fields with multiple).

Further goals include:

  • Accessibility: Appropriate ARIA roles are set, expected keyboard shortcuts can be used.
  • Easy stylability: Custom properties exist for the main features.
@michelson
michelson / async_method_job.rb
Created April 23, 2024 01:50
Asynchronously Concern
class AsyncMethodJob < ApplicationJob
queue_as :default
def perform(target:, method_name:, args:, queue_name: :default)
self.class.queue_as(queue_name)
# `target` could be either an instance or a class
target = target.constantize if target.is_a?(String) # Convert class name to class object if needed
target.send(method_name, *args)
end
end
@nikokozak
nikokozak / template.ex
Created January 13, 2022 22:09
Naive re-implementation of Phoenix's templating macros.
defmodule Lector.Template do
@moduledoc """
Defines helper functions for rendering Templates.
What this allows is a naive re-implementation of how Phoenix handles views/templates.
When 'using' `Lector.Template`, files sharing the same module name as the view being used get pre-compiled
into named render functions. In other words, `Lector.Templates.Home` will pre-compile a `home.html.eex` file
in the same folder into a function of the same name (`Lector.Templates.Home.home(assigns)`), which renders
the template.
@Envek
Envek / login_helpers.rb
Created October 11, 2021 06:42
Signing-in user for integration tests via cookie-only session with Rails, Devise, Capybara, and Cuprite
# spec/system/support/login_helpers.rb
# See this blog post for setup guide: https://evilmartians.com/chronicles/system-of-a-test-setting-up-end-to-end-rails-testing
module LoginHelpers
def login_as(user)
# Craft session cookie to make request authenticated (to pass even routing constraints)
# Compilation of these:
# - https://dev.to/nejremeslnici/migrating-selenium-system-tests-to-cuprite-42ah#faster-signin-in-tests
# - https://turriate.com/articles/2011/feb/how-to-generate-signed-rails-session-cookie
# - https://github.com/rails/rails/blob/43e29f0f5d54294ed61c31ddecdf76c2e1a474f7/actionpack/test/dispatch/cookies_test.rb#L350
import * as React from "react";
import { useMousePosition } from "~/hooks/useMousePosition";
/** Component to cover the area between the mouse cursor and the sub-menu, to allow moving cursor to lower parts of sub-menu without the sub-menu disappearing. */
export function MouseSafeArea(props: { parentRef: React.RefObject<HTMLDivElement> }) {
const { x = 0, y = 0, height: h = 0, width: w = 0 } = props.parentRef.current?.getBoundingClientRect() || {};
const [mouseX, mouseY] = useMousePosition();
const positions = { x, y, h, w, mouseX, mouseY };
return (
<div
defmodule Acme.Repo do
use Ecto.Repo,
otp_app: :acme,
adapter: Ecto.Adapters.Postgres
def with_prefix(prefix) do
module_atom = Module.concat([Acme, Repo, WithPrefix, Macro.camelize(prefix)])
# We could not find a better way to see if this module already existed
if !Kernel.function_exported?(module_atom, :prefix, 0) do