Skip to content

Instantly share code, notes, and snippets.

View acuppy's full-sized avatar

Adam Cuppy acuppy

View GitHub Profile
## rake file lib/tasks/backup.rake
desc "Backup Everything Specified in config/backup.yml"
task :backup => [ "backup:db", "backup:push"]
namespace :backup do
RAILS_APPDIR = RAILS_ROOT.sub("/config/..","")
def interesting_tables
@acuppy
acuppy / string.map_concat.coffee
Last active December 20, 2015 00:09
String#mapConcat: splits a string by character; iterates through each character; runs a callback on each character; and concat the callback return into a new string
String.prototype.mapConcat = (args...) ->
separator = args.slice(0,1) if $.type(args[0]) == 'string' or $.type(args[0]) == 'regexp'
separator or= ''
chars = @.split(separator)
rtn = []
for char, i in chars
rtn.push(args[0].call(@, char, i) or char)
rtn.join('')
@acuppy
acuppy / string.array_replace.coffee
Last active December 20, 2015 01:09
String#arrayReplace: similar to String#replace, but allows you to pass multiple array sets for replacement.
String.prototype.arrayReplace = (args...) ->
rtn = @
for set in args
rtn = rtn.replace(new RegExp(set[0], 'gi'), set[1])
rtn
@acuppy
acuppy / url_patterns.rb
Created July 21, 2013 04:43
Remote URL (ex: http://example.com) and Local URL (ex: C:\\path\to\file or /path/to/file.txt)
REMOTE_URL_PATTERN = /\A(?:(?:http|https):\/\/[a-z0-9]+(?:[\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(?:\:[0-9]{1,5})?).*\z/ix
LOCAL_URL_PATTERN = /\A(?:[a-zA-Z]{1,2}\:\\\\?).*\z/
@acuppy
acuppy / paginate_array.rb
Created July 21, 2013 04:57
Array#paginate: adds pagination functionality using the WillPaginate gem to a Ruby Array
require 'will_paginate'
class Array
def paginate(all = nil, options = {})
options[:page] = (options[:page].to_i == 0) ? 1 : options[:page].to_i
options[:per_page] = (options[:per_page].to_i == 0) ? 30 : options[:per_page].to_i
pagination_array = WillPaginate::Collection.new(options[:page], options[:per_page], self.size)
start_index = pagination_array.offset
end_index = start_index + (options[:per_page] - 1)
array_to_concat = self[start_index..end_index]
@acuppy
acuppy / aspell_spell_checker.rb
Last active December 20, 2015 01:18
Simple abstraction layer to interact with the UNIX aspell library via Ruby
module Spelling
require 'net/https'
require 'uri'
require 'rexml/document'
ASPELL_WORD_DATA_REGEX = Regexp.new(/\&\s\w+\s\d+\s\d+(.*)$/)
ASPELL_PATH = "aspell"
#
# @param String: spell_check_text - represents the source string
@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
@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 / 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
class AddAdminToUsers < ActiveRecord::Migration
def change
add_column :users, :admin, :boolean, default: false
# convert existing users to admin
User.reset_column_information
User.find_each do |u|
u.update admin: true
end
end