Skip to content

Instantly share code, notes, and snippets.

@crwang
crwang / index.html
Created March 3, 2020 22:02
Sticky footer / sidebar layout
<html>
<head>
<style>
.main-layout {
margin: 0;
display: flex;
min-height: 100vh;
flex-direction: column;
}
@crwang
crwang / postgres-cheatsheet.md
Created January 22, 2019 19:54 — forked from Kartones/postgres-cheatsheet.md
PostgreSQL command line cheatsheet

PSQL

Magic words:

psql -U postgres

Some interesting flags (to see all, use -h or --help depending on your psql version):

  • -E: will describe the underlaying queries of the \ commands (cool for learning!)
  • -l: psql will list all databases and then exit (useful if the user you connect with doesn't has a default database, like at AWS RDS)

Everything I knew to do here, I learned from this pluralsight course:https://app.pluralsight.com/library/courses/postgresql-playbook/table-of-contents and this blog post: http://www.craigkerstiens.com/2012/10/01/understanding-postgres-performance/

I used cg run -e <env> bash and then psql $DATABASE_URL to connect to the DB

I used the postgres query here to find big tables with low rates of using indexes, see “Understanding Index Usage” : http://www.craigkerstiens.com/2012/10/01/understanding-postgres-performance/

googledrivelti=> SELECT
googledrivelti->   relname,
googledrivelti->   100 * idx_scan / (seq_scan + idx_scan) percent_of_times_index_used,
@crwang
crwang / .bash_profile
Last active September 29, 2015 15:31
Bash profile
#
# Sections:
# 1. Environment Configuration
# 2. Make Terminal Better (remapping defaults and adding functionality)
# 3. File and Folder Management
# 4. Searching
# 5. Process Management
# 6. Networking
# 7. System Operations & Information
# 8. Web Development
@crwang
crwang / webUtils.js
Created August 20, 2015 15:04
Rendr Get Domain Name
var WebUtils = function() {};
WebUtils = {
getCurrentHost: function(app) {
if (!app) throw 'App is required to get the requested information';
if (app.req) {
// Server
return app.req.headers.host;
} else {
// Client
@crwang
crwang / hash_nested_iteration.rb
Created August 17, 2015 15:58
hash nested iteration
# In /lib/rails_extensions.rb
class Hash
##
# Iterate over a hash, motified so we have the breadcrumb of keys
# http://stackoverflow.com/questions/9279768/how-do-i-loop-over-a-hash-of-hashes-in-ruby
# Example:
# {"root"=>{:a=>"tom", :b=>{:c => 1, :x => 2}}}.nested_each_pair{|k,v|
# puts k
class ApplicationController < ActionController::Base
include Pundit
# Verify that controller actions are authorized. Optional, but good.
after_filter :verify_authorized, except: :index
after_filter :verify_policy_scoped, only: :index
rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized
private
@crwang
crwang / pundit_matcher.rb
Last active June 1, 2016 18:12
Rspec Pundit Matcher
#spec/support/matchers/pundit_matcher.rb
#borrowed from http://thunderboltlabs.com/blog/2013/03/27/testing-pundit-policies-with-rspec/
#using permit_authorization instead of permit so we don't clash with shoulda's permit
RSpec::Matchers.define :allow_action do |action|
match do |policy|
policy.public_send("#{action}?")
end
failure_message do |policy|
@crwang
crwang / .gitconfig
Last active September 29, 2015 15:14 — forked from pksunkara/config
[user]
name = Chris Wang
email = code@chris.wang
[core]
editor = vim
whitespace = fix,-indent-with-non-tab,trailing-space,cr-at-eol
excludesfile = ~/.gitignore
[sendemail]
smtpencryption = tls
smtpserver = smtp.gmail.com
@crwang
crwang / pretty_print_rails_api_json
Created January 21, 2015 18:55
Rails pretty print hash in json
pretty_json = JSON.pretty_generate(JSON.parse(my_hash.to_json))
render json: pretty_json