Skip to content

Instantly share code, notes, and snippets.

View mieko's full-sized avatar

Mike Owens mieko

View GitHub Profile
{
"$schema": "http://json-schema.org/draft-07/schema",
"title": "RuboCop configuration schema",
"description": "Schema for RuboCop configuration files (e.g. .rubocop.yml), intended for helping IDEs provide autocompletion and validation.",
"type": "object",
"additionalProperties": false,
"definitions": {
"inherit_mode": {
"type": "object",
"minProperties": 1,
@mieko
mieko / sanitized_flash.rb
Created October 14, 2020 03:03
Mixed HTML/Plaintext `flash` messages in Rails 4.1+ JSON cookie serializer
require "rack/utils"
# Rails < 4.1 serialized sessions with Marshal, which could distinguish "html_safe" flash strings
# from unsafe strings because it encoded actual ActiveSupport::SafeBuffer objects. So when the
# flash message came back from the client, its "safe" status was preserved, and it was rendered
# correctly under both circumstances.
#
# This same "magic" ability is also why Rails moved away from it: multiple CVEs due to arbitrary
# object creation. But regardless, that was a *really* nice trick.
#
@mieko
mieko / schema.rake
Created September 3, 2020 01:23
`rake schema FOR=Model` as an alternative to `annotate`
require "shellwords"
def terminal_width
%x(tput cols).chomp.to_i
rescue StandardError
80
end
def sql_table_command(model_class)
table_name = model_class.table_name
@mieko
mieko / try-binstubs-first.sh
Last active August 31, 2020 10:06
Shell functions that always remember to type `./bin/rails` when you type `rails`
# Tries to find a parent directory containing a file: "$1"
find-up() {
local path=$(pwd)
while [[ -n "$path" && ! -e "$path/$1" ]]; do
path=${path%/*}
done
echo "$path"
}
try-binstub() {
@mieko
mieko / MIT-LICENSE
Last active February 24, 2024 15:47
Douglas-Peucker Polyline simplification
Copyright 2016 Mike Owens
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
Software.
@mieko
mieko / switch.rb
Last active September 22, 2016 09:05
switch in Ruby
class Switch < BasicObject
def initialize(&body)
fail ArgumentError, 'switch body required' unless body
@cases = []
@receiver = body.binding.receiver
instance_eval(&body)
end
def given(*spec, &block)

Keybase proof

I hereby claim:

  • I am mieko on github.
  • I am mieko (https://keybase.io/mieko) on keybase.
  • I have a public key whose fingerprint is 2AEC C9D5 4B21 DD55 6B42 214D 22C1 BE53 C942 F1EE

To claim this, I am signing this object:

@mieko
mieko / README.md
Last active March 4, 2016 09:39
Using nginx to allow ActionCable within the same host

This is an example of how to configure nginx to allow ActionCable WebSocket connections to use the same host as your primary site. For example, instead of cable.myapp.dev, you can use myapp.dev/_c/.

In my case, we have a multi-tenanted environment, e.g., bob.myapp.dev and jim.myapp.dev. This lets the ActionCable connection still use the Host: header to select a tenant, and use normal cookies for user identification. wss://bob.myapp.dev/_c/ is authorized as normal. It also piggybacks off of whatever TLS setup is available.

@mieko
mieko / console-finders-readme.md
Last active August 29, 2015 14:27
Rails Console Finders

Rails Quick Console Finders

I use rails console a lot for poking around at models. Exploratory style. A LOT.

I always have seeded data, and typing User.find_by(slug: 'administrator') or User.friendly.find('administrator') gets really annoying.

This shit gets ridiculous in a REPL environment:

@mieko
mieko / marching_squares.rb
Created September 29, 2014 05:09
Marching Squares in Ruby (with Inline C implementation)
class MarchingSquares
STATE = { "0000" => :r, "1100" => :r, "0011" => :l, "1110" => :r,
"0100" => :r, "1000" => :u, "1011" => :u, "1001" => :u,
"0101" => :d, "1010" => :u, "0111" => :l, "0110" => :l,
"0001" => :d, "0010" => :l, "1101" => :d, "1111" => :r }
def self.find_shape(grid, w, h)
edge_pos = grid.index('1')
return nil if edge_pos.nil?