Skip to content

Instantly share code, notes, and snippets.

View ianterrell's full-sized avatar

Ian Terrell ianterrell

  • University of Virginia
  • Charlottesville, VA
View GitHub Profile
@ianterrell
ianterrell / gist:111115
Created May 13, 2009 16:38
Implementation for Authorization plugin
# This lets you define user roles as methods,
# i.e. checking the role of "admin" on a user
# will delegate to user.admin?
#
# This is helpful because in ActiveRecord, boolean
# attributes automatically have their query method
# defined; a boolean field "admin" will define
# the "admin?" method for you.
#
# If your logic is more complex, you can write your
class Book extends ActiveRecordModel {
static $belongs_to = array(
array('publisher'),
array('author', 'readonly' => true, 'select' => 'name', 'conditions' => "name != 'jax'"),
array('another', 'class_name' => 'SomeModel')
);
static $has_many = array(
array('revisions'),
array('editors', 'through' => 'revisions')
class Book < ActiveRecord::Base
belongs_to :publisher
belongs_to :author, :select => [:id, :name], :conditions => "name != 'jax'"
belongs_to :another, :class_name => "SomeModel"
has_many :revisions
has_many :editors, :through => :revisions
has_one :something
[13:20][ian@ian-terrells-macbook-pro:~/src/test/i18ntest]$ ./script/console
Loading development environment (Rails 2.3.2)
>> o = Order.create
=> #<Order id: 1, created_at: "2009-05-25 17:21:00", updated_at: "2009-05-25 17:21:00">
>> t = OtherThing.create
=> #<OtherThing id: 1, created_at: "2009-05-25 17:21:05", updated_at: "2009-05-25 17:21:05">
>> o.payments.create
=> #<Payment id: 1, payable_id: 1, payable_type: "Order", created_at: "2009-05-25 17:21:11", updated_at: "2009-05-25 17:21:11">
>> o.payments.create
=> #<Payment id: 2, payable_id: 1, payable_type: "Order", created_at: "2009-05-25 17:21:13", updated_at: "2009-05-25 17:21:13">
class Fairish
def initialize(probability, unfair_low, unfair_high, min_rolls)
@probability, @unfair_low, @unfair_high, @min_rolls = probability, unfair_low, unfair_high, min_rolls
@rolls, @hits = 0, 0
end
def fire!
hit = if @rolls >= @min_rolls && observed_probability > @unfair_high
false
elsif @rolls >= @min_rolls && observed_probability < @unfair_low
[14:04][ian@ian-terrells-macbook-pro:~]$ ruby --version
ruby 1.8.6 (2008-08-11 patchlevel 287) [universal-darwin9.0]
[14:04][ian@ian-terrells-macbook-pro:~]$ irb
>> class X
>> end
=> nil
>> class Y < X
>> end
=> nil
>> X.subclasses
Loading development environment (Rails 2.3.2)
>> class Z < User; end
=> nil
>> User.subclasses
NoMethodError: protected method `subclasses' called for #<Class:0x22ce974>
from (irb):2
>> User.send(:subclasses)
=> [Z(id: integer, email: string, crypted_password: string, salt: string, created_at: datetime, updated_at: datetime, remember_token: string, remember_token_expires_at: datetime, activation_code: string, activated_at: datetime, state: string, deleted_at: datetime, editor: boolean, has_promise: boolean, reset_code: string)]
>>
@ianterrell
ianterrell / Installing Postgres Gems on OS X
Created December 16, 2010 00:11
Installing Postgres Gems on OS X
PATH=$PATH:/Library/PostgreSQL/9.0/bin/ env ARCHFLAGS="-arch x86_64" gem install pg
@ianterrell
ianterrell / redis-rb safety patch.rb
Created January 6, 2011 20:09
Monkey patch for safety using flushall and flushdb
require 'redis'
class Redis
alias :__flushall :flushall
alias :__flushdb :flushdb
def flushall(confirm=false)
if confirm
__flushall
else
@ianterrell
ianterrell / Devise BCrypt speed up!
Created January 21, 2011 00:46
Generating users dynamically in your test suite? Speed it up with a simple tweak.
# In your devise.rb configuration file, set the bcrypt stretches to 1 in your test environment:
config.stretches = Rails.env.test? ? 1 : 10