Skip to content

Instantly share code, notes, and snippets.

@vaz
vaz / rails-asset-pipeline.md
Last active October 13, 2017 16:54
Breakout lecture notes on the asset pipeline (May 24, 2016)

Rails Asset Pipeline - Overview/Cheatsheet

Notes based on a lecture by vaz at Lighthouse Labs, Vancouver.

Note: this was based on Rails 4, and while most things haven't changed much and this still covers the concepts pretty well, make sure you've got the Rails Guide docs for the version you're actually using and keep in mind some things may have changed.

What is an asset?

@vaz
vaz / merge-sort.hs
Created June 23, 2016 05:12
merge sort in haskell
mergesort :: (Ord t) => [t] -> [t]
mergesort [] = []
mergesort [x] = [x]
mergesort xs = (mergesort ls) `merge` (mergesort rs)
where (ls,rs) = split xs
split (x:y:zs) = let (xs,ys) = split zs in (x:xs,y:ys)
split [x] = ([x],[])
split [] = ([],[])
merge xs [] = xs
merge [] ys = ys
// If the first statement in a file or a function is "use strict"
// then Strict Mode will be activated for that file or function.
//
// You could invoke strict mode for the file by uncommenting this:
// "use strict";
function nonStrictFunction () {
x = 123; // undeclared!
}
@vaz
vaz / ar-or-statesman-error.rb
Created November 15, 2016 19:59
AR or Statesman, error handling patterns in controller (sort of pseudocode)
# ActiveRecordish pattern
def create
@thing = Thing.create!(thing_params)
redirect_to @thing
rescue Thing::Invalid #???
# flash error
# re-render
end
@vaz
vaz / promises-are-fun.js
Created April 19, 2017 22:06
promises are fun.
// this is coming from a library
function delayedValue(x) {
return new Promise((resolve, reject) => {
setTimeout(() => resolve(x), 1000)
})
}
// module doQuery.js
function doQuery() {
return delayedValue({
@vaz
vaz / promise-example.js
Created May 31, 2017 05:45
Promise example - chaining and parallel execution
// Promises example
//
// just a helper, which returns a promise that resolves after
// 500-1000 milliseconds with whatever `args` were passed in
function fakeKnex(...args) {
return new Promise((resolve, reject) => {
setTimeout(
function() { resolve(...args) },
500 + (Math.floor(Math.random() * 500)))
@vaz
vaz / 1.js
Last active June 6, 2017 02:11
request examples
var request = require('request');
var url = 'http://www.google.com';
// basic use
request.get(url, function(err, response, body) {
if (err) {
console.log("Error:", err);
} else {
// response.body == body
@vaz
vaz / vagrant-fixes-for-watch-polling.md
Last active October 31, 2020 12:50
Various fixes for file-watching and live-reloading when running servers in Vagrant

Vagrant fixes for file-watching servers

These snippets show how to enable polling for filesystem changes when using

  • nodemon for Node apps (ExpressJS, etc)
  • rails server
  • webpack-dev-server --watch
  • ember server

You can probably look at these examples and Google to find similar solutions for other dev servers.

@vaz
vaz / 1_README.md
Last active September 29, 2017 01:23
knex migrations: associating records

Knex seeds: associating records

It's easiest if you're associating records between tables if you do it in one seed file. There are other ways but for now this is probably easiest.

PostgreSQL will generate your id fields for you, so don't put them in your seeds! Instead, take advantage of the returning chainable method from knex. See the below seed file and also http://knexjs.org docs.

@vaz
vaz / js-callback-args.js
Created October 9, 2017 21:33
Ruby blocks vs JS callbacks
function benchmark(cb) {
const start_time = 12345;
const result = cb('some message');
console.log("the return value of cb was: ", result);
const end_time = 12456;
return end_time - start_time
}