Skip to content

Instantly share code, notes, and snippets.

View chrisbloom7's full-sized avatar
💭
I may be slow to respond.

Chris Bloom chrisbloom7

💭
I may be slow to respond.
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'
@chrisbloom7
chrisbloom7 / There is something to be learned from a rainstorm.
Created August 20, 2008 11:25
One of the quotes featured in the movie Ghost Dog
There is something to be learned from a rainstorm.
When meeting with a sudden shower, you try
not to get wet and run quickly along the road.
By doing such things as passing under the eaves
of houses, you still get wet. When you are
resolved from the beginning, you will not be
perplexed, though you will still get the same
soaking. This understanding extends to all things.
Ghost Dog, Chapter 22, T01:32:25 (quoted from: Hagakure: The Book of the Samauri)
task :write => :environment do
dir = RAILS_ROOT + '/db/backup'
FileUtils.mkdir_p(dir)
FileUtils.chdir(dir)
interesting_tables.each do |tbl|
sql ||= ActiveRecord::Base.connection()
if (records = sql.execute("SELECT * FROM " + tbl) rescue false)
puts "Writing #{tbl}..."
.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'