Skip to content

Instantly share code, notes, and snippets.

@ldenman
Forked from amw/ar_patch.rb
Created May 13, 2011 20:29
Show Gist options
  • Save ldenman/971254 to your computer and use it in GitHub Desktop.
Save ldenman/971254 to your computer and use it in GitHub Desktop.
A patch that allows lambda arguments to default_scope in rails ~> 3.0.0
module ActiveRecord
class Base
class << self
def default_scope(options = {})
reset_scoped_methods
default_scoping = self.default_scoping.dup
previous = default_scoping.pop
if previous.respond_to?(:call) or options.respond_to?(:call)
new_default_scope = lambda do
sane_options = options.respond_to?(:call) ? options.call : options
sane_previous = previous.respond_to?(:call) ? previous.call : previous
construct_finder_arel sane_options, sane_previous
end
else
new_default_scope = construct_finder_arel options, previous
end
self.default_scoping = default_scoping << new_default_scope
end
protected
def current_scoped_methods
method = scoped_methods.last
if method.kind_of? Proc
unscoped(&method)
else
method
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment