Created
May 14, 2009 17:28
-
-
Save matthuhiggins/111783 to your computer and use it in GitHub Desktop.
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 StrictlyUntyped | |
module ConditionsScope | |
# Makes it easy to add scopes which are only conditions. | |
# | |
# For example, | |
# class Person < ActiveRecord::Base | |
# named_scope :active, :conditions => {:active => true} | |
# end | |
# | |
# Can be replaced with | |
# named_conditions :active, :active => true | |
# | |
# | |
# Also, | |
# Person.scoped(:conditions => {:active => true}) | |
# | |
# Can be replaced with | |
# Person.conditions :active => true | |
# | |
def self.included(base) | |
base.class_eval do | |
extend ClassMethods | |
named_conditions :conditions, lambda { |conditions| conditions } | |
end | |
end | |
module ClassMethods | |
def named_conditions(name, conditions, &block) | |
named_scope(name, case conditions | |
when Hash | |
{:conditions => conditions} | |
when Proc | |
lambda { |*args| {:conditions => conditions.call(*args)} } | |
end, &block) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment