Skip to content

Instantly share code, notes, and snippets.

View stevenharman's full-sized avatar

Steven Harman stevenharman

View GitHub Profile
@stevenharman
stevenharman / factories.rb
Created November 30, 2011 18:42
What am I missing about Ruby blocks that the following happens in factory_girl?
FactoryGirl.define do
factory :user do
sequence(:username) { |n| "bob-#{n}" }
email { |u| "#{u.username}@example.com" }
password 'password123'
# the following works:
factory :bob do username 'bob' end
# but this does not:
@stevenharman
stevenharman / thing_spec.rb
Created December 2, 2011 22:21
Using Capybara 1.1.2, "session#has_text?" doesn't exist?
require 'spec_helper'
# spec/acceptance/thing_spec.rb
feature "Viewing a thing" do
let(:thing) { Factory.create(:thing, name: "I'm a thing!") }
scenario "show a summary of the thing" do
visit thing_path(thing)
page.should have_text "I'm a thing!"
end
@stevenharman
stevenharman / albums-records-users.rb
Created December 7, 2011 00:04
Find an item by id, and eagerly load association if the association exists. Possible?
# This is a record, as in a vinyl.
class Record < ActiveRecord::Base
belongs_to :user
belongs_to :album
end
class Album < ActiveRecord::Base
has_many :records
belongs_to :artist
end
@stevenharman
stevenharman / music_library.rb
Created December 11, 2011 01:41
When wrapping AR behind an intentional interface, go explicit or generic/idiomatic?
# This is a record, as in a vinyl.
class Record < ActiveRecord::Base
belongs_to :user
belongs_to :album
# explicitly stating what is needed
def from_library_for_album(collector, album)
where(user_id: collector).where(album_id: album)
end
@stevenharman
stevenharman / page_name.rb
Created December 12, 2011 21:53
A simple wrapper over Hash to providing a way to override a default convention.
class PageName
@pages = {
"Super Awesome Page" => "bad-ass-page-name-2",
"Secret Page" => :secret_page_as_symbol
}
def self.[](name)
@pages.fetch(name) { |n| n.gsub(/\s/, '') }
end
@stevenharman
stevenharman / db_export.rake
Created January 5, 2012 02:27
Dump a list database tables to CSV via ActiveRecord models.
namespace :db do
desc 'export the shit'
task :export => [:environment] do
require 'csv'
ActiveRecordize('Brewery', 'Beer', 'Style', 'Category', 'Geocode') do |model|
CSV.open("#{model.table_name}.csv", 'wb') do |csv|
csv << model.column_names
model.find_each { |m| csv << m.attributes.values }
@stevenharman
stevenharman / app_pool.rb
Created January 23, 2012 22:43
managing an IIS AppPool from Ruby
require 'win32ole'
# Connect to the WMI WebAdministration namespace.
web_admin = WIN32OLE.connect("winmgmts:root\\WebAdministration")
app_pool = web_admin.get("ApplicationPool.Name='DefaultAppPool'")
app_pool.stop
app_pool.start
app_pool.recycle
@stevenharman
stevenharman / a_sample_usage.rb
Created January 27, 2012 22:36
A wrapper for running app installer/un-installers, etc.
# An example usage might look something like:
class MyApp
extend Verver::SandboxedExecute
def initialize(options)
# ...
end
# ...
@stevenharman
stevenharman / zsh_startup_files.sh
Created January 29, 2012 18:03
zshell startup files, in order. (because I always forget)
/etc/zshenv
~/.zshenv
# if login shell
/etc/zprofile
~/.zprofile
#if interactive shell
/etc/zshrc
~/.zshrc
@stevenharman
stevenharman / win32ole_wrapper.rb
Created January 31, 2012 22:23
A Win32OLE object wrapper that does the right thing. #lolwindows
# NOTE: I pulled this straight out of a Gem I wrote to help manage/automate
# the CI environment at VersionOne - hence the names and why you won't
# find Verver on http://rubygems.org. Sorry.
require 'delegate'
require 'win32ole'
module Verver
module IIS