Skip to content

Instantly share code, notes, and snippets.

View alexpchin's full-sized avatar

Alex Chin alexpchin

View GitHub Profile
@alexpchin
alexpchin / Mongo-Nested-or-Embedded.md
Last active August 29, 2015 14:28
Mongo Nested / Embedded

This is more an art than a science. The [Mongo Documentation on Schemas][1] is a good reference, but here are some things to consider:

  • Put as much in as possible

The joy of a Document database is that it eliminates lots of Joins. Your first instinct should be to place as much in a single document as you can. Because MongoDB documents have structure, and because you can efficiently query within that structure there is no immediate need to normalize data like you would in SQL. In particular any data that is not useful apart from its parent document should be part of the same document.

  • Separate data that can be referred to from multiple places into its own collection.

This is not so much a "storage space" issue as it is a "data consistency" issue. If many records will refer to the same data it is more efficient and less error prone to update a single record and keep references to it in other places.

@alexpchin
alexpchin / fade_in_backgroun_image
Created April 28, 2015 10:05
Fade in background image
// HTML
<div class="background-image"></div>
// CSS
@-webkit-keyframes fade-in {
0% { opacity: 0; }
100% { opacity: 1; }
}
.background-image {
@alexpchin
alexpchin / angularjs_resource_tokenhandler.js
Last active September 3, 2015 22:58 — forked from nblumoe/angularjs_resource_tokenhandler.js
AngularJS service to send auth token with $resource requests
.factory('TokenHandler', function() {
var tokenHandler = {};
var token = "none";
tokenHandler.set = function( newToken ) {
token = newToken;
};
tokenHandler.get = function() {
return token;
@alexpchin
alexpchin / gist:86bc4fe40a409814d153
Created October 27, 2015 12:07
Run Rails in Production
rails s -e production
@alexpchin
alexpchin / gist:8f175cad3e4864c894a3
Created November 1, 2015 20:45
Wordpress Setup on mac
Start Mamp
Start MySQL with:
```
sudo /usr/local/mysql/support-files/mysql.server start
sudo /usr/local/mysql/support-files/mysql.server stop
sudo /usr/local/mysql/support-files/mysql.server restart
```
module.exports = function(grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
sass: {
dist: {
files: {
'public/css/style.css' : 'public/scss/style.scss'
}
}
},
@alexpchin
alexpchin / serialize.js
Created December 14, 2015 14:35
Serialize Javascript
function serialize(obj) {
var str = [];
for(var p in obj)
if (obj.hasOwnProperty(p)) {
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
}
return str.join("&");
}
module.exports = serialize;
@alexpchin
alexpchin / oauth_twit_consume_example.js
Created January 6, 2016 11:46 — forked from tomshaw/oauth_twit_consume_example.js
Some Node.js Express, Jade, examples using Node-OAuth querying protected Twitter consumer resources.
function consumer() {
return new oauth.OAuth(
"https://twitter.com/oauth/request_token",
"https://twitter.com/oauth/access_token",
keys.twitterConsumerKey,
keys.twitterConsumerSecret,
"1.0A",
"http://localhost:3000/sessions/callback",
"HMAC-SHA1"
);
@alexpchin
alexpchin / gulpfile.js
Created January 16, 2016 17:45 — forked from vincent-zurczak/gulpfile.js
Copy minified Bower dependencies with Gulp (better solution)
// Include our plug-ins
var gulp = require('gulp');
var mainBowerFiles = require('main-bower-files');
var exists = require('path-exists').sync;
// Create some task
gulp.task( 'copy-bower-dep', function() {
// Replace files by their minified version when possible
var bowerWithMin = mainBowerFiles().map( function(path, index, arr) {
@alexpchin
alexpchin / rspec_rails_cheetsheet.rb
Created February 9, 2016 18:42 — forked from them0nk/rspec_rails_cheetsheet.rb
Rspec Rails cheatsheet (include capybara matchers)
#Model
@user.should have(1).error_on(:username) # Checks whether there is an error in username
@user.errors[:username].should include("can't be blank") # check for the error message
#Rendering
response.should render_template(:index)
#Redirecting
response.should redirect_to(movies_path)