Skip to content

Instantly share code, notes, and snippets.

@tomafro
tomafro / scoped_ids.rb
Created August 12, 2008 09:32
Get scoped object ids without overhead of instantiating full objects
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|
# 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
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
remove_constant(:JavaScriptHelper)
Kernel.expects(:require).with('action_view/helpers/javascript_helper')
JavaScriptHelper
# Is this crazy?
class AccountRole < ActiveRecord::Base
belongs_to :account
belongs_to :target, :polymorphic => true
validates_presence_of :role
end
class Project < ActiveRecord::Base
class Account < ActiveRecord::Base
end
# 9 out of 10 microbenchmarks agree: implicit return smokes explicit return
require 'benchmark'
def explicit
return 1
end
def implicit
1
end
#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)
Before:
<ul class="actions">
<li><%= link_to content_tag(:span, 'Edit group'), edit_group_path(@group), :class => 'btn' if current_user.group_admin?(@group) %></li>
<li><%= link_to content_tag(:span, 'Start a new discussion'), new_group_discussion_path(@group), :class => 'btn' if @group.public? || @group.members.include?(current_user) %></li>
<li><%= link_to content_tag(:span, 'Start a shared document'), new_group_shared_document_path(@group), :class => 'btn' %></li>
<li><%= link_to content_tag(:span, 'Invite users to join this group'), new_group_invitation_path(@group), :class => 'btn' if current_user.andand.group_admin?(@group) %></li>
</ul>
After:
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