Skip to content

Instantly share code, notes, and snippets.

View alassek's full-sized avatar

Adam Lassek alassek

View GitHub Profile
require "global_id"
module Sidekiq
module GlobalIDMiddleware
class Processor
def call(_worker_class, job, *)
job["args"].map!(&method(:process))
yield
end
def Redis.Mutex(name, expires:, redis: Redis.current, &block)
raise ArgumentError, "block required" unless block_given?
if block.arity.zero?
success = block
block = proc do |on|
on.success(&success)
on.failure(&proc{})
end
end
type Maybedoer struct {
err error
}
func (m Maybedoer) Maybe(f func() error) {
if m.err == nil {
c.err = f()
}
}
@alassek
alassek / db.rake
Created June 2, 2017 00:11
Rails db:refresh task
namespace :db do
desc "Do a clean reset of your DB from a backup file"
task refresh: [:environment, :check_protected_environments] do
filename = ARGV.last
jobs = ENV.fetch('JOBS', 2)
database = ActiveRecord::Base.connection.current_database
abort "Usage: rails db:refresh filename" if filename == 'db:refresh'
abort "Can't find: #{filename}" unless File.exists? filename
abort "Can't find pg_restore, make sure it is in your $PATH" unless system "which", "pg_restore"

Keybase proof

I hereby claim:

  • I am alassek on github.
  • I am alassek (https://keybase.io/alassek) on keybase.
  • I have a public key ASCER3RZ4eL9jqSNbXVviFayarCOAT2JXki5SwZ6hhHopwo

To claim this, I am signing this object:

@alassek
alassek / 01_Annotated_Source.md
Last active November 19, 2023 11:53
Extending Arel to support @> operator

I've been doing some work lately with JSON documents in PostgreSQL using jsonb columns. You can query these documents using a GIN index in PG, but the syntax can be a little cumbersome

SELECT "events".* FROM "events" WHERE "events"."body" @> '{"shift":{"user_id":2}}'

You have to construct the right side of the query as a JSON string, which is a bit annoying. So I wondered if I could adapt Arel to do the tedious stuff for me.

@alassek
alassek / application_controller.rb
Created May 5, 2014 22:00
SSL Auth for Services
class ApplicationController < ActionController::Base
before_filter :require_authentication
private
def require_authentication
unless current_certificate.verify(public_key)
head :forbidden
end
end
@alassek
alassek / location.js
Last active December 10, 2015 20:58
window.location sucks
/**
* `window.location` is a BAD api. Doesn't have a prototype, 'too much recursion' error
* if you try to inspect the constructor. Monkey-patching causes random disappearances
* of the monkey-patched function in Chrome, cloning causes 'too much recursion' in FF.
*
* This is what I'm reduced to. ಠ_ಠ
**/
(function () {
function Location () {
# By Henrik Nyh <http://henrik.nyh.se> 2008-01-30.
# Free to modify and redistribute with credit.
require "rubygems"
require "hpricot"
module TextHelper
# Like the Rails _truncate_ helper but doesn't break HTML tags or entities.
def truncate_html(text, max_length = 30, ellipsis = "...")
@alassek
alassek / extendEach.js
Created September 19, 2011 22:19
Easy multiple-inheritance in Backbone.js
(function () {
function extendEach () {
var args = Array.prototype.slice.call(arguments),
child = this;
_.each(args, function (proto) {
child = child.extend(proto);
});