Skip to content

Instantly share code, notes, and snippets.

@jcalvert
Created October 22, 2011 22:54
Show Gist options
  • Save jcalvert/1306596 to your computer and use it in GitHub Desktop.
Save jcalvert/1306596 to your computer and use it in GitHub Desktop.
Boolean class method companions for named_scopes
module ActiveRecord
module NamedScope
module ClassMethods
alias_method :original_scope, :scope
def scope(name, scope_options = {})
original_scope(name, scope_options)
boolean_scope = lambda do
send(name).any?
end
singleton_class.send(:redefine_method, "#{name}?".to_sym, &boolean_scope)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment