Skip to content

Instantly share code, notes, and snippets.

@ches
ches / company.rb
Created December 9, 2011 23:01
Rails #3847
class Company < ActiveRecord::Base
has_many :relationships
has_many :users, :through => :relationships, :uniq => true, :dependent => :destroy
with_options :through => :relationships, :source => :user, :dependent => :destroy,
:extend => Associations::RelationshipExtension do |co|
co.has_many :advisors, :conditions => ["relationships.role = ?", ROLES[:advisor]]
co.has_many :incubators, :conditions => ["relationships.role = ?", ROLES[:incubator]]
co.has_many :past_investors, :conditions => ["relationships.role = ?", ROLES[:past_investor]]
co.has_many :referrers, :conditions => ["relationships.role = ?", ROLES[:referrer]]
@ches
ches / db.rake
Created December 5, 2011 21:57
Dev Machinist fixtures
namespace :db do
namespace :fixtures do
desc 'Create some dummy development data from factories'
task :create => :environment do
unless Rails.env.development? || Rails.env.test?
puts "This task generates random data and is not intended for use in production."
exit 1
end
unless admin = User.first(:conditions => { :username => 'admin' })
@ches
ches / eydeploy.rb
Created November 16, 2011 10:41
Extend `bundle --without` groups on Engine Yard deploy.
# This monkeypatches engineyard-serverside to specify `--without` options for
# `bundle install` on Engine Yard deployment. It supercedes the instructions in
# this doc, which are currently outdated:
#
# http://docs.engineyard.com/bundler-tips-for-cloud.html
#
# More helpful is this one, which leads us to where the code can be found to
# override:
#
# http://docs.engineyard.com/customize-your-deployment.html#second
@ches
ches / migration.rb
Created September 22, 2011 11:23
Migrating Mongoid model to natural primary key
# To run after the `key` declaration is added to the above User model
User.all.each do |user|
nu = user.dup
user.unset :username # clear unique index
nu.save
# Could we re-associate without duping? My attempts resulted in
# `dependent: :destroy` getting invoked when the original user is deleted
nu.notes << user.notes.clone
@ches
ches / clipboard
Created September 15, 2011 06:34
Should ENV be trying File.path somewhere?
ruby-1.9.2-p290 :001 > require 'pathname'
=> true
ruby-1.9.2-p290 :002 > p = Pathname.new('/Users/ches')
=> #<Pathname:/Users/ches>
ruby-1.9.2-p290 :003 > ENV['foobar'] = p + 'src'
TypeError: can't convert Pathname into String
from (irb):3:in `[]='
from (irb):3
from /Users/ches/.rvm/rubies/ruby-1.9.2-p290/bin/irb:16:in `<main>'
@ches
ches / javascript_steps.rb
Created September 12, 2011 05:27
Capybara selector/Cucumber step to click any element with given text
# Brandon Keepers' "click on any element with given text" Capybara selector/Cucumber
# step pairing, adapted for Capybara 1.0+.
# http://collectiveidea.com/blog/archives/2010/08/03/clicking-any-element-with-cucumber-and-capybara/
Capybara.add_selector(:element) do
xpath { |locator| "//*[normalize-space(text())=#{XPath::Expression::StringLiteral.new(locator)}]" }
end
When 'I click "$locator"' do |locator|
msg = "No element found with the content of '#{locator}'"
find(:element, locator, :message => msg).click
@ches
ches / subject_prefixes.rb
Created September 7, 2011 07:27
Hack ActionMailer to always prefix email subjects. Gotta be a better way.
ActionMailer::Base.class_eval do
def mail_with_prefix(headers={}, &block)
unless Rails.env.production?
headers[:subject] = "[#{Rails.env.upcase}] " + (headers[:subject] || '')
end
mail_without_prefix(headers, &block)
end
alias_method_chain :mail, :prefix
end
@ches
ches / .pryrc
Created August 29, 2011 06:39
Enable Hirb in Pry, and hack disable/enable to work in-session
begin
require 'hirb'
rescue LoadError
# Missing goodies, bummer
end
if defined? Hirb
# Dirty hack to support in-session Hirb.disable/enable
Hirb::View.instance_eval do
def enable_output_method
@ches
ches / tags_input.rb
Created August 26, 2011 08:09
SimpleForm (1.4.x) input for mongoid_taggable -- place in app/inputs
# Input builder to render tags from an Array field, for use with my fork
# of mongoid_taggable:
#
# https://github.com/ches/mongoid_taggable
#
class TagsInput < SimpleForm::Inputs::StringInput
map_type :tags, :to => :text_field
protected
@ches
ches / assets.rake
Created August 14, 2011 07:29
Reusing Guard tasks via Rake
# There are a couple minor quirks (see https://github.com/guard/guard/issues/118),
# but mostly this is delightfully straightforward:
require 'guard'
namespace :assets do
desc 'Generate CSS from all source LESS files'
task :less do
Guard.setup
Guard::Dsl.evaluate_guardfile(:guardfile => 'Guardfile', :group => ['frontend'])