Skip to content

Instantly share code, notes, and snippets.

View tylerhunt's full-sized avatar

Tyler Hunt tylerhunt

View GitHub Profile
@tylerhunt
tylerhunt / active_record_jsonb_key_exists.rb
Created January 22, 2024 19:36
Using PostgreSQL's `?` jsonb operator with Active Record
# PostgreSQL’s question mark (`?`) jsonb operator can pose an issue when using
# Active Record, since `?` is used to denote bind variable substitution. One
# way to work around this is to use named bind variable substitution with a
# hash instead.
Entity.where('metadata ? :key', key: 'profile_url')
@tylerhunt
tylerhunt / Gemfile
Last active October 11, 2023 14:44
ActiveAdmin association autocomplete without complicated plugins.
gem 'active_model_serializers'
gem 'activeadmin'
gem 'jquery-ui-rails'
@tylerhunt
tylerhunt / status_line.rb
Last active February 7, 2023 18:43
A simple task status output helper for Ruby.
class StatusLine
MAX_LENGTH = 72
FORMAT = "[ ] %.#{MAX_LENGTH}s"
BUSY = 'BUSY'
DONE = 'DONE'
FAIL = 'FAIL'
COLOR="\033[%s;%sm"
@tylerhunt
tylerhunt / README.md
Created March 30, 2021 18:26
Generates a 2FA QR code for Instagram.

Instagram 2FA QR Code Generator

Instagram doesn’t make it easy to enable 2FA unless you’re using Google Authenticator or Duo Security. If you want to use it with another authenticator app—like 1Password—this script will generate the QR code you need to easily set that up.

Usage

The Ruby script makes use of Bundler’s inline functionality and will automatically install the depenedncies as system gems.

ruby qr.rb
@tylerhunt
tylerhunt / capitalize_headers.ex
Created December 1, 2021 19:19
Cowboy lowercases all the headers by default. This stream handler can be used to capitalize the headers. The implementation is in Elixir, and a configuration example showing its use in a Phoenix application is shown.
defmodule AppWeb.CapitalizeHeaders do
def init(stream_id, req, opts) do
:cowboy_stream_h.init(stream_id, req, opts)
end
def data(stream_id, is_fin, data, state) do
:cowboy_stream_h.data(stream_id, is_fin, data, state)
end
def info(stream_id, {:response, _status, headers, _body} = info, state) do
@tylerhunt
tylerhunt / README.md
Last active May 14, 2021 17:03
OAuth Redirect Server for Local Development

OAuth Redirect Server for Local Development

Some OAuth providers (like Google), limit what hosts can be used for redirect URIs. This can be problematic when using a hostname-based local development server (like Pow or Invoker). This very simple redirection server can be used to work around this restriction.

Usage

@tylerhunt
tylerhunt / active_record_decorator.rb
Created March 19, 2015 18:52
An abstract decorator that allows decorated Active Record records to be assigned to associations without raising an ActiveRecord::AssociationTypeMismatch error.
require 'delegate'
# An abstract decorator useful for decorating Active Record objects.
class ActiveRecordDecorator < SimpleDelegator
# A proxy for the decorator class to allow the delegation of certain class
# methods to the decorated object's class.
class ClassProxy < SimpleDelegator
def initialize(decorator_class, decorated_class)
super decorator_class
self.decorated_class = decorated_class
@tylerhunt
tylerhunt / README.md
Created September 10, 2014 06:51
Static error pages from dynamically generated assets.

The guide Asset Pipeline Error Pages now no longer works, since non-digest assets aren't generated in newer Rails apps. To fix this, I've replaced the config/initializers/exceptions.rb initializer that swaps out the middleware with the Rake task here, which will be run after Rails assets:precompile task.

@tylerhunt
tylerhunt / rendering_helper.rb
Created March 20, 2015 15:48
Override Rails' #render helper to fix an issue with rendering partials based on an object within a namespace.
module RenderingHelper
# Override Rails' #render helper to fix an issue with it not honoring objects
# with #to_partial_path definitions that return absolute paths, which is
# problematic when rendering partials within a namespaced controller.
def render(options={}, locals={}, &block)
return super unless options.respond_to?(:to_partial_path)
object = options
path = object.to_partial_path
@tylerhunt
tylerhunt / Gemfile
Created February 7, 2014 00:54
Monkey-patch for Bundler's release Rake task to push to Gemfury instead of RubyGems.org.
source 'https://rubygems.org/'
gem 'gemfury', '~> 0.4.20'