This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Animal < ActiveRecord::Base | |
named_scope :with_wings, :conditions => {:wings => true} | |
named_scope :with_four_legs, :conditions => {:legs => 4} | |
# Returns object ids falling within current scope without | |
# instantiating actual model objects | |
def self.ids | |
column = columns_hash[primary_key] | |
ids = connection.select_all(construct_finder_sql(:select => primary_key)).collect do |value| |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# I hear that aliasing lambda is bad, but this is | |
# much more readable... | |
alias :this_block :lambda | |
this_block{ @this.destroy }.should change(Thing, :count).by(-1) | |
# (tomafro) I've done this for ages, only using doing rather than this_block: | |
alias :doing, :lambda |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ColumnReader | |
def column_reader(column_name, options = {}) | |
name = options.delete(:as) || column_name.to_s.pluralize | |
behaviour_module = column_reader_module(column_name) | |
column = columns_hash[column_name.to_s] | |
behaviour_module.module_eval %{ | |
def #{name}(options = {}) | |
connection.select_all(construct_finder_sql(options.merge(:select => '#{column_name}'))).collect do |value| | |
v = value.values.first |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
remove_constant(:JavaScriptHelper) | |
Kernel.expects(:require).with('action_view/helpers/javascript_helper') | |
JavaScriptHelper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Is this crazy? | |
class AccountRole < ActiveRecord::Base | |
belongs_to :account | |
belongs_to :target, :polymorphic => true | |
validates_presence_of :role | |
end | |
class Project < ActiveRecord::Base |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Account < ActiveRecord::Base | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 9 out of 10 microbenchmarks agree: implicit return smokes explicit return | |
require 'benchmark' | |
def explicit | |
return 1 | |
end | |
def implicit | |
1 | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#compdef gem | |
# My first zsh completion function, for the gem command. It lets you type 'gem <tab>' to complete gem commands | |
# (including installed ones) and for some commands (currently just open and update) allows you to complete gem | |
# names as well. The implementation isn't ideal, so I'd appreciate advice on how I can improve it, particularly | |
# the 'caching' of the gem_commands and installed_gems. | |
_gem_commands () { | |
if [[ -z $gem_commands ]] ; then | |
gem_commands=$(gem help commands | grep '^ [a-z]' | cut -d " " -f 5) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From: | |
Then /^the "([^\"]*)" checkbox should be checked$/ do |label| | |
field_labeled(label).should be_checked | |
end | |
Then /^the "([^\"]*)" checkbox should not be checked$/ do |label| | |
field_labeled(label).should_not be_checked | |
end |
OlderNewer