Skip to content

Instantly share code, notes, and snippets.

View npassaro's full-sized avatar

Nuno Passaro npassaro

  • Portugal
View GitHub Profile
@npassaro
npassaro / add_good_companion.sql
Last active May 1, 2020 15:02
ActiveRecord generated SQL inserting a good companion
INSERT INTO "companionships" ("green_id", "companion_type", "companion_id", "created_at", "updated_at")
VALUES ($1, $2, $3, $4, $5)
RETURNING "id" [
["green_id", 1],
-- this line should be GoodCompanion
["companion_type", "Green"],
["companion_id", 14],
["created_at", "2020-05-01 13:05:59.823802"],
["updated_at", "2020-05-01 13:05:59.823802"]
];
class CreateCompanionships < ActiveRecord::Migration[6.0]
def change
create_table :companionships do |t|
t.references :green, null: false, foreign_key: {to_table: :greens}
t.string :companion_type
t.references :companion, null: false, foreign_key: {to_table: :greens}
t.timestamps
end
end
class BadCompanion < Green
has_many :companionships, as: :companion
has_many :greens, through: :companionships
end
@npassaro
npassaro / foreman.plugin.zsh
Last active March 10, 2016 16:48 — forked from danmartens/foreman.plugin.zsh
Foreman ZSH Plugin
### Foreman alias
alias befstart="bundle exec foreman start"
alias befrun="bundle exec foreman run"
### docker machine
function dmdefault() {
if [[ "$@" = "" ]]
then
echo "default"
else
@npassaro
npassaro / find_duplicates.sql
Created November 2, 2015 15:08
Find how many duplicate strings (independently of case) exist in a table
-- Arel
select("COUNT(*)").select("LOWER(name) AS name").group("LOWER(name)").having("COUNT(*) > 1")
-- SQL
SELECT COUNT(*), LOWER(name) AS name FROM "occupations" GROUP BY LOWER(name) HAVING COUNT(*) > 1
@npassaro
npassaro / gist:3ff383d1c71c2cb484d6
Created September 2, 2015 14:08
Put docke IP on /etc/hosts as dockerhost
echo $(docker-machine ip docker-vm) dockerhost | sudo tee -a /etc/hosts
@npassaro
npassaro / null_object_decor
Last active August 29, 2015 14:24
Decorator that kind of implements Null Object pattern
module Utils
class StringDecorator
def initialize(string)
@string = string
end
def =~(regex)
self.to_s =~ regex
end
@npassaro
npassaro / .bashrc
Created October 30, 2014 22:46
.bashrc prompt showing git branch and coloring
# Custom bash prompt via kirsle.net/wizards/ps1.html
source /usr/share/git-core/contrib/completion/git-prompt.sh
export PS1="[\[$(tput setaf 5)\]\u\[$(tput setaf 2)\]@\h\[$(tput setaf 6)\]\[$(tput bold)\] \W\[$(tput sgr0)\]\$(__git_ps1)]\\$ \[$(tput sgr0)\]"
@npassaro
npassaro / gist:7118985
Created October 23, 2013 13:40
jstatd - Command to start jstatd.
jstatd -J-Djava.security.policy=/usr/java/default/bin/jstatd.all.policy
# jstatd.all.policy has to be created with the following code:
#
# grant codebase "file:/usr/java/jdk1.7.0_09/lib/tools.jar" {
# permission java.security.AllPermission;
# };
@npassaro
npassaro / gist:307a23f40287482a5275
Last active December 19, 2015 15:09
How to use activerecord standalone
require 'active_record'
require 'sqlite3'
require 'logger'
ActiveRecord::Base.logger = Logger.new('debug.log')
ActiveRecord::Base.configurations = YAML::load(IO.read('database.yml'))
ActiveRecord::Base.establish_connection('development')
class Schema < ActiveRecord::Migration
def change