Skip to content

Instantly share code, notes, and snippets.

Avatar

Mike Owens mieko

View GitHub Profile
@mieko
mieko / sanitized_flash.rb
Created Oct 14, 2020
Mixed HTML/Plaintext `flash` messages in Rails 4.1+ JSON cookie serializer
View sanitized_flash.rb
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 Sep 3, 2020
`rake schema FOR=Model` as an alternative to `annotate`
View schema.rake
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 Aug 31, 2020
Shell functions that always remember to type `./bin/rails` when you type `rails`
View try-binstubs-first.sh
# 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 Sep 17, 2021
Douglas-Peucker Polyline simplification
View MIT-LICENSE
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 Sep 22, 2016
switch in Ruby
View switch.rb
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)
View keybase.md

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 Mar 4, 2016
Using nginx to allow ActionCable within the same host
View README.md

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 Aug 29, 2015
Rails Console Finders
View console-finders-readme.md

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 Sep 29, 2014
Marching Squares in Ruby (with Inline C implementation)
View marching_squares.rb
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?
@mieko
mieko / marching_squares.cc
Created Sep 29, 2014
Marching Squares in C++
View marching_squares.cc
#include "marching-squares.hh"
#include "point.hh"
#include "polygon.hh"
#include "douglas-peucker.hh"
#include <ps/canvas/canvas.hh>
#include <cstring>
#include <vector>