Skip to content

Instantly share code, notes, and snippets.

View acuppy's full-sized avatar

Adam Cuppy acuppy

View GitHub Profile
" shortcuts
inoremap jk <Esc>
:hi CursorLine cterm=NONE ctermbg=darkred ctermfg=white guibg=darkred guifg=white
:hi CursorColumn cterm=NONE ctermbg=darkred ctermfg=white guibg=darkred guifg=white
:nnoremap <Leader>c :set cursorline! cursorcolumn!<CR>
autocmd BufWritePre * :%s/\s\+$//e
" close buffer
nmap <leader>d :bp<bar>sp<bar>bn<bar>bd<CR>
@acuppy
acuppy / arguments.rb
Created December 11, 2014 05:26
Arguments wrapper
require "ostruct"
class Arguments
def initialize(arguments)
@whiny = arguments.delete(:whiny) || false
@arguments = arguments
@delegee = OpenStruct.new(@arguments)
end
def method_missing(method, *args)
@acuppy
acuppy / 20141027-active-record-left-outer-joins.md
Last active December 18, 2018 03:53
Using SQL JOINS to Retrieve ActiveRecord Objects Without Associated Records

Scenario: you're pulling all User records that do not have any associated Activities. In Rails you may find yourself doing something like this...

User.all.select { |user| user.activities.blank? }

Pretty simple to implement, but a performance hog: Rails will load a new ActiveRecord Object for each User, then call for all activities related to the user; simply to determine, which do and don't have any Activites: bad idea.

We can achieve the same result with a fraction of the overhead, using LEFT OUTER JOIN.

@acuppy
acuppy / purge.sh
Last active August 29, 2015 14:05 — forked from seanculver/purge.sh
#!/bin/sh
# Credits to:
# - http://vstone.eu/reducing-vagrant-box-size/
# - https://github.com/mitchellh/vagrant/issues/343
sudo su
aptitude -y purge ri
aptitude -y purge installation-report landscape-common wireless-tools wpasupplicant ubuntu-serverguide
@acuppy
acuppy / install-ruby-redis-imagemagick-elasticsearch-ubuntu.sh
Last active August 29, 2015 14:05
Install Ruby, Imagemagick, Redis-server and Elasticsearch onto the development environment
#!/usr/bin/env bash
cd ~
sudo apt-get update
sudo apt-get install curl wget
# install ruby
curl -sSL https://get.rvm.io | bash -s stable --ruby="2.0.0-p353"
# install imagemagick
cd ~
sudo apt-get update
sudo apt-get install openjdk-7-jre-headless -y
### Check http://www.elasticsearch.org/download/ for latest version of ElasticSearch and replace wget link below
# NEW WAY / EASY WAY
wget https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-1.1.1.deb
sudo dpkg -i elasticsearch-1.1.1.deb
@acuppy
acuppy / hash_compact.rb
Created February 6, 2014 17:07
Recursively delete hash key/value pairs where the value is nil
class Hash
def compact(opts={})
inject({}) do |new_hash, (k,v)|
if !v.nil?
new_hash[k] = opts[:recurse] && v.class == Hash ? v.compact(opts) : v
end
new_hash
end
end
end
@acuppy
acuppy / install.md
Last active December 26, 2015 02:29
Setup Ubuntu 12.04 box with Postgres 9.3 and Rails

Setup Rails

  1. cd /vagrant
  2. sudo gem install bundler --no-ri --no-rdoc
  3. \curl -L https://get.rvm.io | bash -s stable --ruby
  4. bundle

Postgres 9.3 onto Ubuntu 12.04

  1. sudo apt-get update
@acuppy
acuppy / range_scopes.rb
Last active December 20, 2015 02:09
ActiveRecord Range Scopes => ( between, created_between, updated_between, since, created_since, updated_since, before, created_before, updated_before )
module RangeScopes
extend ActiveSupport::Concern
module ClassMethods
# Order.between(1..5) => pulls all orders who's ids are between 1 and 4
# Order.between(:customer_id, 1..4) => pulls all orders who's orders.customer_id is between 1 and 4
def between(*args)
column = args.first.is_a?(Symbol) ? args.shift : :id
range = args.first
@acuppy
acuppy / object_true_false.rb
Last active December 20, 2015 01:18
Object#true? and Object#false? boolean assertions
class Object
def true?
self.respond_to?( :to_i ) ? !self.to_i.zero? : true
end
def false?
!true?
end
end