Skip to content

Instantly share code, notes, and snippets.

View runlevel5's full-sized avatar

Trung Lê runlevel5

View GitHub Profile
@runlevel5
runlevel5 / gist:9364308
Last active August 29, 2015 13:57
It's always good thing to use Arel symbol syntax than string SQL - examples with Rails 4's AR
names = []
# Using SQL string
Bike.where("name IN (?)", names).to_sql
=> "SELECT \"bikes\".* FROM \"bikes\" WHERE (name IN (NULL))"
Bike.where("name NOT IN (?)", names).to_sql
=> "SELECT \"bikes\".* FROM \"bikes\" WHERE (name NOT IN (NULL))"
@runlevel5
runlevel5 / abused_claim.rb
Last active August 29, 2015 13:57
Abusing STI example code
class Claim < ActiveRecord::Base
belongs_to :client
validate :check_eligibility, on: :create
before_save :set_claim_amount
def eligible_amount
# to be overriden
end
@runlevel5
runlevel5 / actionview_patch.rb
Created April 11, 2014 01:03
Monkey-patching ActiveView to generate dash for ID instead of underscore
# place it it application.rb or initializers
module ActionView
module RecordIdentifier
JOIN = '-'.freeze
end
module Helpers
module Tags # :nodoc:

How to install Fedora 20 via Network PXE with OSX 10.9

  1. Install DHCP server first: brew install isc-dhcp
  2. Follow the instructions after the installation complete, that is copying plist file to /Library/LaunchDaemons/homebrew.mxcl.isc-dhcp.plist
  3. Create new config file /usr/local/etc/dhcpd.conf:
default-lease-time 600;
max-lease-time 7200;
@runlevel5
runlevel5 / gist:ada0c38dd9cd9ec1f210
Created June 3, 2014 05:41
OS X 10.10 (2/6/2014) - default Ruby has bug
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/yaml.rb:4:in `<top (required)>':
It seems your ruby installation is missing psych (for YAML output).
To eliminate this warning, please install libyaml and reinstall your ruby.
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/yaml.rb:5:in `require': cannot load such file -- psych (LoadError)
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/yaml.rb:5:in `<top (required)>'
from /opt/boxen/repo/.bundle/ruby/2.0.0/gems/facter-2.0.1-universal-darwin/lib/facter/util/parser.rb:6:in `require'
from /opt/boxen/repo/.bundle/ruby/2.0.0/gems/facter-2.0.1-universal-darwin/lib/facter/util/parser.rb:6:in `<top (required)>'
from /opt/boxen/repo/.bundle/ruby/2.0.0/gems/facter-2.0.1-universal-darwin/lib/facter/util/directory_loader.rb:18:in `require'
from /opt/boxen/repo/.bundle/ruby/2.0.0/gems/facter-2.0.1-universal-darwin/lib/facter/util/directory_loader.rb:18:in `<top (required)>'
from /opt/bo
@runlevel5
runlevel5 / gist:4b6e0799c9e9bcb1994c
Created June 5, 2014 12:17
Failed to pull images
Guest OS: uname -sr # Darwin 14.0.0
Run docker via CoreOS: vagrant box list # coreos-alpha (virtualbox, 338.0.0)
$ vagrant ssh
$ docker pull ubuntu
Pulling repository ubuntu
cc0067db4f11: Error pulling image (precise) from ubuntu, endpoint: https://cdn-registry-1.docker.io/v1/, Get https://cdn-registry-1.docker.io/v1/images/cc0067db4f11198ef6fd0435f96a973e559b1cebfceb2bc8e4fe527b32045c2f/ancestry: dial tcp: lookup cdn-registry-1.dcc0067db4f11: Error pulling image (precise) from ubuntu, Get https://cdn-registry-1.docker.io/v1/images/cc0067db4f11198ef6fd0435f96a973e559b1cebfceb2bc8e4fe527b32045c2f/ancestry: dial tcp: lookup cdn-registry-1.docker.io on [10.0.2.3]:53: too many redirects
6006e6343fad: Error pulling image (quantal) from ubuntu, endpoint: https://cdn-registry-1.docker.io/v1/, Get https://cdn-registry-1.docker.io/v1/images/6006e6343fadaaeb5dd98436a3cd17eac8c03cabca1ed4c8778d039e72ebcc9c/ancestry: dial tcp: lookup cdn-registry-1.d6006e6343fad: Error pulling image (quantal) from ubuntu, Get https://c
@runlevel5
runlevel5 / freaking_ruby_float.md
Last active August 29, 2015 14:02
Ruby Float must be very broken!

A friend of mine was asking why Ruby could not get a plus of 2 float correct..

A simple C++ code:

#include <iostream>
int main(int argc, const char* argv[])
{       
  float a = 3.151;
 float b = 3.023;

Things in the documentation we could use further help with:

Extensions Tutorial

"If you are using a 2.1.x version of Spree, the above line will work fine. If you’re using a 2.0.x version of Spree, you’ll need to change the “branch” option to point to the “2-0-stable” branch. If you’re using the “master” branch of Spree, change the “branch” argument for “spree_fancy” to be “master” as well."

Q. Where can we see an overview of the branch structure in Spree?

A. Spree releases are kept in git branches, you can clone the spree and do git branch to see all branches.

@runlevel5
runlevel5 / destroy_exited_docker_containers.sh
Created June 19, 2014 04:00
Destroy exited docker containers
#!/bin/bash
CONTAINER_IDS=$(docker ps -a -q)
if [ -n "$CONTAINER_IDS" ]; then
docker stop $CONTAINER_IDS
docker rm $CONTAINER_IDS
fi
@runlevel5
runlevel5 / lotus_console.md
Last active August 29, 2015 14:02
A simple rake task that replicates the `rails console` for Lotus Framework

Lotus Console

A simple rake task that replicates the rails console for Lotus Framework

How to?

I assume that your config/application.rb is the place where you initialize all Lotus boot config

below is my config/application.rb for your interest