Skip to content

Instantly share code, notes, and snippets.

@matthuhiggins
Created May 14, 2009 17:28
Show Gist options
  • Save matthuhiggins/111783 to your computer and use it in GitHub Desktop.
Save matthuhiggins/111783 to your computer and use it in GitHub Desktop.
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