Skip to content

Instantly share code, notes, and snippets.

View chrisbloom7's full-sized avatar

Chris Bloom chrisbloom7

View GitHub Profile
@chrisbloom7
chrisbloom7 / rails_test_descriptions.md
Last active August 29, 2015 13:57
Short description of the various tests (testunit, minitest) typically found in a Rails app
  • Unit tests are very narrowly focused on testing a single model
  • Functional tests are very narrowly focused on testing a single controller and the interactions between the models it employs
  • Integration tests are broad story-level tests that verify the interactions between the various actions supported by the application, across all controllers
@chrisbloom7
chrisbloom7 / acts_as_indexed.rb
Created August 5, 2014 16:53
Setup acts_as_indexed for Rails testing (Tested on Rails 2.3 with TestUnit)
# config/initializers/acts_as_indexed.rb
ActsAsIndexed.configure do |config|
# Disable acts_as_indexed auto indexing in test by default
config.disable_auto_indexing = Rails.env.test?
end
@chrisbloom7
chrisbloom7 / active_record.rb
Created August 11, 2014 17:19
Rails ActiveRecord finder that accepts an array of IDs and preserves the order in the results
# config/initializers/active_record.rb
module ActiveRecord
class Base
# Find by array of IDs, preserving order of IDs
# See http://stackoverflow.com/a/25248122/83743
def self.find_with_relevance(array_of_ids)
array_of_ids = Array(array_of_ids) unless array_of_ids.is_a?(Array)
self.find(array_of_ids).index_by(&:id).slice(*array_of_ids).values
end
@chrisbloom7
chrisbloom7 / 20140101000000_add_hstore_field_to_trip_tickets.rb
Last active August 29, 2015 14:06
Using a Postgres HStore field in a Rails ActionView form
# db/migrations/20140101000000_add_hstore_field_to_trip_tickets.rb
class AddHstoreFieldToTripTickets < ActiveRecord::Migration
def change
add_column :trip_tickets, :customer_identifiers, :hstore
end
end
@chrisbloom7
chrisbloom7 / Gemfile
Last active August 29, 2015 14:15
TestUnit setup/teardown order in Ruby 2.1 with shoulda-context, factory_girl_rails and database_cleaner
source 'https://rubygems.org'
ruby '2.1.4'
gem 'rails', '~> 3.2.0'
gem 'mysql2'
group :development, :test do
gem 'byebug'
.ilb {
display: -moz-inline-box;
display: inline-block;
}
@chrisbloom7
chrisbloom7 / bookmarklet_to_calculate_the_sum_of_selected_numbers_in_a_table.js
Created February 3, 2010 20:28
Bookmarklet to calculate the sum of selected numbers in a table
javascript:void(prompt('Result',function(){var a=window.getSelection().toString();if(a.indexOf("\n")>=0){a=a.split("\n");var b=0;for(var i=0;i<a.length;i++){b+=parseFloat(a[i])}}else b=parseFloat(a);return(isNaN(b))?0:b}()));
macosx:~ chrisb$ irb -rosx/cocoa
/opt/local/lib/ruby/1.8/osx/foundation.rb:8:in `require': no such file to load -- rubycocoa (LoadError)
from /opt/local/lib/ruby/1.8/osx/foundation.rb:8
from /opt/local/lib/ruby/1.8/osx/cocoa.rb:8:in `require'
from /opt/local/lib/ruby/1.8/osx/cocoa.rb:8
from /opt/local/lib/ruby/1.8/irb/init.rb:254:in `require'
from /opt/local/lib/ruby/1.8/irb/init.rb:254:in `load_modules'
from /opt/local/lib/ruby/1.8/irb/init.rb:252:in `each'
from /opt/local/lib/ruby/1.8/irb/init.rb:252:in `load_modules'
from /opt/local/lib/ruby/1.8/irb/init.rb:21:in `setup'
.mouse{
position: absolute;
background-image: url('../images/cursor.png');
width: 15px;
height: 22px;
z-index: 100;
}
@chrisbloom7
chrisbloom7 / textmate_command_for_php_beautifier.sh
Created July 29, 2010 17:33
Textmate command for PHP_Beautifier
# Runs PHP_Beautifier on the current document.
# See http://beautifyphp.sourceforge.net/ for more info.
# Add this as a new command in TextMate
# I used the following additional command settings:
# Save: Nothing
# Input: Document ("Selected Text or", note PHP_Beautifier
# only works on complete PHP documents)
# Output: Replace Document
if [[ "$TM_SOFT_TABS" == "YES" ]]; then