Skip to content

Instantly share code, notes, and snippets.

View codespectator's full-sized avatar

James MacLeod codespectator

View GitHub Profile
# Unit test
test "should create record" do
plan = Fabricate(:free_plan)
account = Fabricate(:account, :agents => [ Fabricate.build(:agent) ])
Fabricate.build(:free_subscription, :plan => plan, :account => account)
assert account.valid?, "Account was invalid\n#{account.errors.to_yaml}"
end
# Fabricators
class Order < ActiveRecord::Base
belongs_to :order_type
has_many :contacts
def delivered
self.contacts.length
end
class CompanyDocument < ActiveRecord::Base
has_many :documents, :as => :document_references, :dependent => :destroy
accepts_nested_attributes_for :documents, :allow_destroy => true, :reject_if => proc { |attributes| attributes['document'].blank? && attributes['_destroy'] == '0' }
end
class Document < ActiveRecord::Base
belongs_to :document_references, :polymorphic => true
# Email model
class EmailAddress < ActiveRecord::Base
belongs_to :emailable, :polymorhpic => true
validates_presence_of :address
validates_format_of :address, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
attr_accessible :address, :location
end
@codespectator
codespectator / gist:1069315
Created July 7, 2011 11:16
Newrelic output for deployment tracking
MacBook-Pro:turnstile-stable(1m|master) $ cap newrelic:notice_deployment -Snewrelic_desc="This is a test"
* executing `newrelic:notice_deployment'
* Getting log of changes for New Relic Deployment details
** Error creating New Relic deployment (undefined local variable or method `source' for #<Capistrano::Configuration::Namespaces::Namespace:0x0000010148e110>)
** /Users/james/.rvm/gems/ruby-1.9.2-p0@turnstile-stable/gems/capistrano-2.6.0/lib/capistrano/configuration/namespaces.rb:188:in `method_missing'
** /Users/james/.rvm/gems/ruby-1.9.2-p0@turnstile-stable/gems/newrelic_rpm-3.1.0/lib/new_relic/recipes.rb:28:in `block (3 levels) in <top (required)>'
** /Users/james/.rvm/gems/ruby-1.9.2-p0@turnstile-stable/gems/capistrano-2.6.0/lib/capistrano/configuration/execution.rb:139:in `instance_eval'
** /Users/james/.rvm/gems/ruby-1.9.2-p0@turnstile-stable/gems/capistrano-2.6.0/lib/capistrano/configuration/execution.rb:139:in `invoke_task_directly'
** /Users/james/.rvm/gems/ruby-1.9.2-p0@turnstile-stable/g
@codespectator
codespectator / gist:1111500
Created July 28, 2011 13:07
Method for calling via switch case
def manage_thing(args={})
args.each do |action, object|
case action
when :create
# create code
when :remove
# removal code
end
end
end
# Example:
def example(class)
class.constantize.all
end
# Calling it:
example('person')
@codespectator
codespectator / gist:1336230
Created November 3, 2011 10:37
Reload instance variable
it "should save latlng in db" do
@venue = Fabricate(:venue)
expect { Venue.find_address(@venue.id) }.to change(@venue, :latlng).to([51.5339146, 0.7840745000000001])
end
## lib/test/test.rb
require 'lib/test/app/models/card'
require 'lib/test/exts/user'
## lib/test/exts/user.rb
module Test
module User
module ClassMethods
<!DOCTYPE html>
<!-- Helpful things to keep in your <head/>
// Brian Blakely, 360i
// http://twitter.com/brianblakely/
-->
<head>
<!-- Disable automatic DNS prefetching.