Skip to content

Instantly share code, notes, and snippets.

View benshimmin's full-sized avatar
🇪🇺
Bare ruin'd choirs, where late the sweet birds sang

Ben Shimmin benshimmin

🇪🇺
Bare ruin'd choirs, where late the sweet birds sang
View GitHub Profile
@benshimmin
benshimmin / gist:9401836
Created March 6, 2014 23:20
How to raise a validation error in Rails after save
# This probably isn't a good thing to want to do, but it came up for me,
# so in the spirit of helping others with weird problems (and because this
# seems to be documented almost nowhere):
after_save do
if some_failing_condition
errors.add(:something, "some failure happened.")
raise ActiveRecord::RecordInvalid.new(self)
end
@benshimmin
benshimmin / pathformer.js
Last active January 24, 2024 06:54
Turn Vivus' pathformer into a tiny CLI utility

You need Node, obviously, and JSDom. Then use it like this:

$ ./pathformer.js test.svg > test.out.svg

@benshimmin
benshimmin / gist:4088493
Created November 16, 2012 16:03
Scale to fit and centre-align an image with FPDF
<?php
/* Caveat: I'm not a PHP programmer, so this may or may
* not be the most idiomatic code...
*
* FPDF is a free PHP library for creating PDFs:
* http://www.fpdf.org/
*/
require("fpdf.php");
class PDF extends FPDF {
@benshimmin
benshimmin / gist:3238456
Created August 2, 2012 16:36
How to extract all links and link text from a Markdown file using pandoc
-- adapted from <http://johnmacfarlane.net/pandoc/scripting.html>
import Text.Pandoc
import Text.Pandoc.Shared (stringify)
extractURL :: Inline -> [String]
extractURL (Link txt (u,_)) = [stringify txt ++ " : <" ++ u ++ ">"]
extractURL (Image _ (u,_)) = [u]
extractURL _ = []
extractURLs :: Pandoc -> [String]
@benshimmin
benshimmin / pdf.coffee
Last active December 23, 2021 01:19
How to render a web page into a multi-page PDF with html2canvas and jsPDF
# Assumptions:
# 1. $("#pdf-download") is something you can click on (not really required).
# 2. $(".result-page") is something in your existing page that you want to turn into a PDF.
# 3. You want to add a class of `pdf-rendered` to it and then specify some CSS which applies when that class is added.
# 4. Your CSS for `.pdf-rendered` specifies a useful width for a PDF (795px is pretty good) and anything else you like
# (this is for portrait ("p") PDFs; obviously you'll need to make a few modifications for landscape PDFs!).
# How it works:
# 1. Take the requested element in your existing page, clone it, add a "pdf-rendered" class to it, and append to
# the body.
@benshimmin
benshimmin / gist:4443000
Created January 3, 2013 12:03
Stop Chrome popping up those "Confirm form resubmission" prompts when resubmitting POST data
$ /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome -disable-prompt-on-repost &

Keybase proof

I hereby claim:

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

To claim this, I am signing this object:

@benshimmin
benshimmin / enums.rb
Last active April 18, 2016 07:36
Enums with Rails Admin
# This might not be the best way, it might be improved in more
# recent versions of Rails Admin, but this seems to work okay
# for me.
class SomeModel < ActiveRecord::Base
enum :some_enum => {
:some_key => 0
}
@benshimmin
benshimmin / queue.js
Last active April 6, 2016 09:55
Asychronous queues with RequireJS and JavaScript (and no yucky promises)
// Sometimes you care about the order in which RequireJS-loaded modules
// are retrieved, and wish to execute a callback for each module in the
// order in which you originally specified them, rather than the order
// in which they are loaded. You can do this with a queue.
var Queue = function() {
var q,
callCount = 0;
@benshimmin
benshimmin / gist:8164688
Created December 28, 2013 21:51
Use scoped queries to select N records at random with ActiveRecord
class Foo < ActiveRecord::Base
scope :random_n, -> (n) { limit(n).order("RANDOM()") }
end
Foo.random_n(5) # -> five randomly chosen Foos
# For extra points, put this sort of thing into a Concern.