Skip to content

Instantly share code, notes, and snippets.

View alassek's full-sized avatar

Adam Lassek alassek

View GitHub Profile
@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.

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
Response.AddHeader("Refresh", Convert.ToString((Session.Timeout * 60) - 10));
}
</script>
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"
@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);
});
@alassek
alassek / ModernButton.cs
Created February 24, 2009 22:37
ASP.Net Button control that renders with Button tag instead of input
[ParseChildren(false)]
public class ModernButton : WebControl
{
//Fields
private static readonly object EventClick;
private static readonly object EventCommand;
//Events
public event EventHandler Click;
public event CommandEventHandler Command;

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 / 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 () {