Skip to content

Instantly share code, notes, and snippets.

@jnunemaker
jnunemaker / authentication.rb
Created July 15, 2009 03:33
a starting point for authentication with mongomapper and rails
# include this in application controller
module Authentication
protected
# Inclusion hook to make #current_user and #signed_in?
# available as ActionView helper methods.
def self.included(base)
base.send :helper_method, :current_user, :signed_in?, :authorized? if base.respond_to? :helper_method
end
# Returns true or false if the user is signed in.
@jnunemaker
jnunemaker / gist:468109
Created July 8, 2010 14:52 — forked from jseifer/gist:468075
rvm version and git branch/dirtyness in prompt
function __git_dirty {
git diff --quiet HEAD &>/dev/null
[ $? == 1 ] && echo "!"
}
function __git_branch {
__git_ps1 "(%s)"
}
function __my_rvm_ruby_version {
@jnunemaker
jnunemaker / gist:87fb475129c0601fa72695afe912b02f
Created September 7, 2017 18:48
sha256 hexdigest to number for use in mapping a string to a partition number (not consistent hashing, but just in simple hash => partition)
irb(main):001:0> require "digest"
=> true
irb(main):002:0> Digest::SHA256.hexdigest("asdf")
=> "f0e4c2f76c58916ec258f246851bea091d14d4247a2fc3e18694461b1816e13b"
irb(main):003:0> Digest::SHA256.hexdigest("asdf").to_i(16)
=> 108959270400061671294053818573968651411470832267186275529291850190552309358907
irb(main):004:0> Digest::SHA256.hexdigest("asdf").to_i(16).to_s(16)
=> "f0e4c2f76c58916ec258f246851bea091d14d4247a2fc3e18694461b1816e13b"
irb(main):005:0> Digest::SHA256.hexdigest("asdf").to_i(16) % 10
=> 7

Keybase proof

I hereby claim:

  • I am jnunemaker on github.
  • I am jnunemaker (https://keybase.io/jnunemaker) on keybase.
  • I have a public key whose fingerprint is 2F06 D8EA 2B19 8EAB 40C2 7379 7BF1 39D1 EC76 CA82

To claim this, I am signing this object:

@jnunemaker
jnunemaker / mute_all_unread.js
Last active February 3, 2016 14:41
terrible github web notifications #protip: command + click any notifications you care about to open in new tab, then run the following in js console to mute the rest
$('li.unread button.mute-note').each(function() { this.click() });
@jnunemaker
jnunemaker / blocking_with_check.rb
Created August 9, 2013 19:25
Kestrel blocking client with signal checks for the kestrel-client gem.
module Kestrel
class Client
class BlockingWithCheck < Blocking
attr_accessor :return_check
def get(*args)
count = 0
while count += 1
@jnunemaker
jnunemaker / Gemfile
Created March 11, 2013 17:07
Eventually consistent alternate indexes using cassanity.
source 'https://rubygems.org'
gem 'cassanity'
@jnunemaker
jnunemaker / db.rake
Created March 4, 2013 21:58
db.rake tasks for cassanity + migrations.
# ******************************************************************************
# NOTE: You will need to replace keyspace and migrations_path in the rake tasks
# below with whatever makes sense for your application. You will also need a
# task named environment that loads whatever is need to make cassanity available.
# ******************************************************************************
namespace :db do
desc "Run any pending migrations."
task :migrate => :environment do
require 'cassanity/migrator'
@jnunemaker
jnunemaker / paginate_without_total.rb
Last active December 11, 2015 03:59
Paginate without the count for plucky. Probably doesn't work with will paginate, but if you just have previous / next it works great and avoids expensive count calls.
Plucky::Query.class_eval do
def paginate_without_total(opts={})
page = (opts.delete(:page) || 1).to_i
limit = (opts.delete(:per_page) || per_page).to_i
skip = (page - 1) * limit
all(opts.merge(:limit => limit, :skip => skip))
end
end