Skip to content

Instantly share code, notes, and snippets.

View JamesChevalier's full-sized avatar

James Chevalier JamesChevalier

View GitHub Profile
@JamesChevalier
JamesChevalier / gist:4204744
Created December 4, 2012 14:53
Truncate string to certain number of characters, preserving words
def truncate_words(text, length = 100, truncate_string = '...')
return if text.nil?
trim = length – truncate_string.chars.length
text.length > length ? text[/\A.{#{trim}}\w*\;?/m][/.*[\w\;]/m] + truncate_string : text
end
@JamesChevalier
JamesChevalier / shakespeare_insult_kit.rb
Created December 7, 2012 15:42
Shakespeare Insult Kit
#!/usr/bin/env ruby -wKU
# Paste this code into a TextExpander snippet to have the Shakespeare Insult Kit a keystroke away
# Just set the Content of the TextExpander snippet to 'Shell Script', and give it an abbreviation
# Shakespeare Insult Kit: http://www.pangloss.com/seidel/shake_rule.html
print "Thou " + ["artless","bawdy","beslubbering","bootless","churlish","cockered","clouted","craven","currish","dankish","dissembling","droning","errant","fawning","fobbing","froward","frothy","gleeking","goatish","gorbellied","impertinent","infectious","jarring","loggerheaded","lumpish","mammering","mangled","mewling","paunchy","pribbling","puking","puny","qualling","rank","reeky","roguish","ruttish","saucy","spleeny","spongy","surly","tottering","unmuzzled","vain","venomed","villainous","warped","wayward","weedy","yeasty","cullionly","fusty","caluminous","wimpled","burly-boned","misbegotten","odiferous","poisonous","fishified","Wart-necked"].sample + " " + ["base-court","bat-fowling","beef-witted","beetle-headed","b
@JamesChevalier
JamesChevalier / gist:4242670
Created December 9, 2012 00:15
Force SSL in Apache
<VirtualHost *:80>
ServerAdmin admin@example.com
ServerName example.com
ServerAlias example.com
RewriteEngine on
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^(.*)$ https://%{SERVER_NAME}$1 [L,R]
</VirtualHost>
@JamesChevalier
JamesChevalier / SublimeLinter.sublime-settings
Created December 29, 2012 03:02
Set SublimeLinter to use RVM's ruby
{
"sublimelinter_executable_map":
{
"ruby": "rvm-auto-ruby"
}
}
@JamesChevalier
JamesChevalier / .powrc
Created January 21, 2013 14:42
powrc file
if [ -f "$rvm_path/scripts/rvm" ] && [ -f ".rvmrc" ]; then
source "$rvm_path/scripts/rvm"
source ".rvmrc"
fi
@JamesChevalier
JamesChevalier / routes.rb
Created February 3, 2013 19:23
This is an example routes file for a Rails app using Devise along with a custom Users Controller
Railsappname::Application.routes.draw do
root :to => "home#index"
devise_for :users, :skip => [:sessions, :registrations]
devise_scope :user do
# make some pretty URLs
get "login" => "devise/sessions#new", :as => :new_user_session
post 'login' => 'devise/sessions#create', :as => :user_session
delete "logout" => "devise/sessions#destroy", :as => :destroy_user_session
@JamesChevalier
JamesChevalier / Default (OSX).sublime-keymap
Created February 6, 2013 17:49
Add Control-Shift-G shortcut to Sublime Text 2 (with Git package installed) to add & commit the current file. Chose 'New Plugin...' from the Tools menu, and paste the contents of GitAddCurrentFileAndGitCommit.py to replace what's there. Then, add the keyboard shortcut to trigger the command.
[
{ "keys": ["ctrl+shift+g"], "command": "git_add_current_file_and_git_commit" }
]
class DeferredGarbageCollection
DEFERRED_GC_THRESHOLD = (ENV['DEFER_GC'] || 15.0).to_f
@@last_gc_run = Time.now
def self.start
GC.disable if DEFERRED_GC_THRESHOLD > 0
@JamesChevalier
JamesChevalier / ruby_update_process.md
Last active December 12, 2015 07:18
My method of updating Ruby on the server
  • rvm get stable
  • rvm install ruby-1.9.3-p484
  • rvm use --default ruby-1.9.3-p484
  • rvm gemset create GemsetName
  • gem update --system --no-ri --no-rdoc
  • gem install passenger --no-ri --no-rdoc

If Apache:

  • passenger-install-apache2-module
@JamesChevalier
JamesChevalier / photo.rb
Created February 8, 2013 21:07
This is how you set up your model (photo.rb) and factory (photos.rb) to fake paperclip attachments without blowing up your uploads folder with test files, and avoiding "Errno::EMFILE: Too many open files" errors.
class Photo < ActiveRecord::Base
attr_accessible :caption, :image, :image_file_name, :image_content_type, :image_file_size
end