Skip to content

Instantly share code, notes, and snippets.

View joshuaclayton's full-sized avatar

Josh Clayton joshuaclayton

View GitHub Profile
# not all callbacks should be put in an observer, just things not directly associated with the model itself:
class UserObserver < ActiveRecord::Observer
def after_create(user)
ClearanceMailer::deliver_user_credentials(user)
end
end
# callbacks are still cool in AR models if it deals with the class itself
class User < ActiveRecord::Base
class UserTest < ActiveSupport::TestCase
should_belong_to :customer
context "Clearance tests" do
setup do
User.any_instance.stubs(:generate_random_password)
end
include Clearance::Test::Unit::UserTest
end
namespace :test do
task :double do
Rake::Task["rake:test:units"].invoke
Rake::Task["rake:test:units"].invoke
end
# etc
end
body { min-width: 950px; }
.cucumber {
background: #f4f4f4;
color: #666;
padding: 0 2% 1em;
width: 58%;
}
.cucumber .feature h2 {
font-size: 2.5em;
module PercentBase
def self.included(base)
base.extend ClassMethods
end
module ClassMethods
def has_percent(attribute_name = :pretty_percent, column = :percent)
raise(ArgumentError, "Cannot name attribute and column the same") if attribute_name.to_s == column.to_s
class_eval <<-END
# autocompletion for ruby_test
# # works with tu/tf aliases
# # see also ~/bin/ruby_test.rb
_ruby_tests() {
if [[ -n $words[2] ]]; then
compadd `ruby_test -l ${words[2]}`
fi
}
compdef _ruby_tests ruby_test
alias tu="ruby_test unit"
# This allows generation of "codes"... namely for part-numbers and unique identifiers for records
# It's especially effective with FactoryGirl's sequences.
#
# Examples:
# Factory.define :whatever do |whatever|
# whatever.sequence(:unique_code) {|n| generate_code(n, :length => 4, :numeric => true) }
# # ...
# end
#
# Factory.define :another do |another|
--type-add=ruby=.haml,.rake,.rsel,.builder
--type-add=html=.html.erb,.html.haml
--type-add=js=.js.erb
--type-add=css=.sass
--type-set=cucumber=.feature
--ignore-dir=vendor
--ignore-dir=log
--ignore-dir=tmp
--ignore-dir=doc
--ignore-dir=coverage
# Problem 1: 234168
(1..1000).select {|i| i % 3 == 0 || i % 5 == 0}.inject(:+)
# Problem 2: 4613732
def fib(x)
return 0 if x == 0
cache = Array.new(x)
cache[0], cache[1] = 0, 1
2.upto(x) do |i|
cache[i] = cache[i - 1] + cache[i - 2]
def self.private_attributes(*args)
@@private_attributes = [args].flatten
class_eval do
def method_missing_with_private_accessor(call, *args)
method_missing_without_private_accessor(call, *args) unless @@private_attributes.map(&:to_sym).include?(call.to_sym)
end
alias_method_chain :method_missing, :private_accessor
end