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 / paginate.js
Created October 25, 2014 22:55
Clientside pagination in not many lines of code
// If you really need to do pagination clientside (ie. you have
// everything you want to paginate already in the page), then
// with a little jQuery and Underscore it needn't be a big deal.
// Assuming you have a couple of ULs with a bunch of LIs in them
// that you wish to paginate over, let's say one called `users`
// and one called `orders`, then this will do the trick:
(function paginate() {
_.each($("ul.users, ul.orders"), function(ul) {
@benshimmin
benshimmin / eventable.js
Created August 12, 2014 06:42
Make any function have "on", "off", and "trigger" functionality
// Put this in a utility file somewhere. (Requires Underscore.)
var Eventable = {
on : function(event, callback) {
this.events[event] = callback;
return this;
},
@benshimmin
benshimmin / slider.css
Created August 10, 2014 19:53
About the simplest way to make a draggable slider
/* This will give you something to start with, anyway */
.slider-container {
position : relative;
}
.track, .thumb {
position : absolute;
top : 0;
}
@benshimmin
benshimmin / clock.js
Last active August 29, 2015 14:05
Simple canvas-based clock
// Depends on Underscore, nothing else.
// You could easily extend this to have proper hands, markers, numbers, etc.
var Clock = function(_params) {
var Utils = {
Maths : {
degreesToRadians : function(degrees) {
return degrees * Math.PI / 180;
@benshimmin
benshimmin / content.scss
Last active August 29, 2015 14:02
Sass 3.2+, media queries, and @content
// We had a use-case where we wanted to apply some styles at a certain
// breakpoint *and* to any IEs less than 9 (using the standard .lt-ie9
// class).
//
// The developer tasked with this decided Sass couldn't do it,
// wrote a snarky comment, and then copied and pasted the code twice,
// sort of like this:
@media only screen and (max-width: 500px) {
a { ... }
@benshimmin
benshimmin / loopy.js
Created March 25, 2014 18:39
Breaking out of loops with Underscore
// If you've ever written code like this:
var result;
_.each(somethings, function(something) {
if (something === particularSomething) {
result = something;
}
});
@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 / 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.
@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 / spec.js
Last active December 21, 2015 20:49
Selenium and Jasmine and other such fun
var webdriver = require("selenium-webdriver");
var driver = new webdriver.Builder().
usingServer("http://localhost:4444/wd/hub").
withCapabilities(webdriver.Capabilities.phantomjs()).
build();
describe("Get the title of the Google homepage", function() {
it("should be 'Google'", function(done) {