Skip to content

Instantly share code, notes, and snippets.

@crwang
crwang / cross_site_controller.rb
Created November 25, 2014 08:14
Cross Origin Rails 4 API Controller
module Api::V1
# Subclass from our normal api controller base class.
class CrossSiteController < ApiController
after_filter :cors_set_access_control_headers
def route_options
cors_preflight_check
end
@crwang
crwang / simple-node-server.md
Last active October 14, 2015 14:39
Setting up simple node server
@crwang
crwang / app_collections_base.js
Last active August 29, 2015 14:09
Link Headers in Rendr
var RendrBase = require('rendr/shared/base/collection'),
_ = require('underscore'),
syncer = require('../shared/syncer');
_.extend(RendrBase.prototype, syncer);
module.exports = RendrBase.extend({});
@crwang
crwang / rails_rescue_output
Created October 10, 2014 01:35
Rails Rescue output
begin
some_operation_here_that_may_throw_an_exception
rescue
puts $!.message
end
@crwang
crwang / bootstrap_alert_fade_in_out.md
Created October 7, 2014 01:16
Bootstrap alert fade in and out

None of the current answers worked for me. I'm using Bootstrap 3.

I liked what Rob Vermeer was doing and started from his response.

For a fade in and then fade out effect, I just used wrote the following function and used jQuery:

Html on my page to add the alert(s) to:

@crwang
crwang / tab-through-css.less
Last active August 29, 2015 14:04
Set tab active somewhat dynamically through css
@import "vendor/mixins/for.less";
/*
// https://github.com/seven-phases-max/less.curious/blob/master/src/for.less
// ............................................................
// .for
.for(@i, @n) {.-each(@i)}
.for(@n) when (isnumber(@n)) {.for(1, @n)}
.for(@i, @n) when not (@i = @n) {
.for((@i + (@n - @i) / abs(@n - @i)), @n);
@crwang
crwang / ul-css-csv.md
Last active August 29, 2015 14:03
Unordered list css to csv

Html

<ul class="list-csv">
  <li>one</li>
  <li>two</li>
  <li>three</li>
</ul>

LESS

@crwang
crwang / post_complex_json_to_rails.md
Last active August 29, 2015 14:03
Post Complex JSON to Rails

Post from command line

curl -H "Content-Type:application/json" -H "Accept:application/json" -d "{\"commute\":{\"minutes\":0, 
\"startTime\":\"Wed May 06 22:14:12 EDT 2009\", 
\"locations\":[{\"latitude\":\"40.4220061\",
\"longitude\":\"40.4220061\"}, {\"latitude\": \"30\", \"longitude\":\"40\"}]}}"  http://localhost:3000/test

In Rails

function average (arr)
{
return _.reduce(arr, function(memo, num)
{
return memo + num;
}, 0) / arr.length;
}
@crwang
crwang / js-class-example.js
Last active August 29, 2015 14:00
JS Class example
// Class definition / constructor
var Vehicle = (function() {
/** @const */ var SPEED_LIMIT = 5;
/**
* A vehicle.
*
* @constructor
* @this {Vehicle}