Skip to content

Instantly share code, notes, and snippets.

@benaskins
Forked from xaviershay/gist:47274
Created January 15, 2009 05:52
Show Gist options
  • Save benaskins/47278 to your computer and use it in GitHub Desktop.
Save benaskins/47278 to your computer and use it in GitHub Desktop.
# BONUS PARTY GAME - Choose 1 or make your own! Is this DRY gone mad?
# A
named_scope :leads, :conditions => ["dancers.lead = ?", true], :joins => :dancer
named_scope :follows, :conditions => ["dancers.lead = ?", false], :joins => :dancer
# B
[
[:leads, true],
[:follows, false]
].each do |role, data|
named_scope name, :conditions => ["dancers.lead = ?", data], :joins => :dancer
end
# C
lambda {|role_conditions|
named_scope :leads, role_conditions[true]
named_scope :follows, role_conditions[false]
}[lambda {|x| {
:conditions => ["dancers.lead = ?", x],
:joins => :dancer
}}]
# D
with(L{|x| {:conditions => ["dancers.lead = ?", x], :joins => :dancer}}) do |f|
named_scope :leads, f[true]
named_scope :follows, f[false]
end
# E
named_scope :with_role, lambda { |role| :conditions => ["dancers.lead = ?", role == :lead ? true : false], :joins => :dancer }
# eg.
Partner.with_role(:lead)
Partner.with_role(:follow)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment