Skip to content

Instantly share code, notes, and snippets.

View davejachimiak's full-sized avatar

Dave Jachimiak davejachimiak

View GitHub Profile
@davejachimiak
davejachimiak / inclusion.rb
Created June 9, 2012 16:50
:inclusion validator
class Resource < ActiveRecord::Base
module Types
ACTIVE = 'active'
CANCELLED = 'cancelled'
CEASED = 'ceased'
DITCHED = 'ditched'
MEDIA = 'media'
ONE_TIME = 'one time'
ARCHIVED = 'archived'
end
@davejachimiak
davejachimiak / create_cohabitants_notifications.rb
Created June 27, 2012 02:44
Rails 3 join table migration
class CreateCohabitantsNotifications < ActiveRecord::Migration
def change
create_table :cohabitants_notifications, :id => false do |t|
t.string :cohabitant_id, :null => false
t.string :notification_id, :null => false
end
end
end
@davejachimiak
davejachimiak / create_cohabitants_notifications.rb
Created June 27, 2012 02:45
Rails 4 join table migration
class CreateCohabitantsNotifications < ActiveRecord::Migration
def change
create_join_table :cohabitants, :notifications
end
end
@notification = Notification.find(11)
@notification.cohabitant_ids
# => [2, 3, 6]
@notification.cohabitants.each { |c| puts c.contact_name }
# => Cool Lady
# => Cool Guy
# => Super Awesome Dude
@davejachimiak
davejachimiak / cohabitant_notifications.rb
Created June 27, 2012 14:14
Cohabitant notifications
@cohabitant = Cohabitant.find(2)
@cohabitant.notification_ids
# => [11, 13, 14]
@cohabitant.notifications.each do |n|
puts n.created_at.strftime("%A, %B %e, %Y") + " by #{n.user.name}"
end
# => Tuesday, June 26, 2012 by New Guy
@davejachimiak
davejachimiak / cohabitant.rb
Created June 27, 2012 14:36
Cohabitant and Notification models
class Cohabitant < ActiveRecord::Base
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates_presence_of :department, :location, :contact_name, :contact_email
validates :contact_email, :format => { :with => VALID_EMAIL_REGEX }
has_and_belongs_to_many :notifications
end
@davejachimiak
davejachimiak / mappings.vim
Created February 12, 2013 13:48
Mappings for opening related js.coffee file in rails.
" ...
function! OpenRelatedCoffeeFile(action)
let action = a:action
if match(expand('%'), 'app/assets') != -1
exec action . " " . expand("%:s?app/assets?spec?:s?.js.coffee?_spec.js.coffee?")
else
exec action . " " . expand("%:s?^spec?app/assets?:s?_spec??")
endif
endfunction
old_instance_methods = Object.instance_methods
require 'minitest/spec'
$infected_assertions = Object.instance_methods - old_instance_methods
module Kernel
def expect object
Expect.new object
end
@davejachimiak
davejachimiak / github_generated_merge_commit__sha_from_pull.sh
Last active December 20, 2015 14:59
Get merge commit created by Github when you open a pull request from a branch (`my_branch`)
REMOTE_SHA=`git rev-parse origin/my_branch`
PULL_NUMBER=`git ls-remote origin | grep $REMOTE_SHA | grep pull | perl -n -e '/pull\/(.*)\/head/ && print $1'`
git ls-remote origin | grep refs\/pull\/$PULL_NUMBER\/merge | awk '{ print $1 };'
module LoggingObserver
def self.included base
base.extend ClassMethods
end
module ClassMethods
def new *args
@instance = super
override_methods
@instance