aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJon Leighton <j@jonathanleighton.com>2011-08-13 16:50:03 +0100
committerJon Leighton <j@jonathanleighton.com>2011-08-13 16:54:05 +0100
commit9ecc4433bbda255cbcb4a2be442c8ee985692d06 (patch)
tree6d0330a44090e6a1b265738a8f45f9f7c2b95bb7
parent24f902b1bcfa5dca4bfc7f2b978a4b0dece73894 (diff)
downloadrails-9ecc4433bbda255cbcb4a2be442c8ee985692d06.tar.gz
rails-9ecc4433bbda255cbcb4a2be442c8ee985692d06.tar.bz2
rails-9ecc4433bbda255cbcb4a2be442c8ee985692d06.zip
Perf: don't mess around with thread local vars unless we actually need to
-rw-r--r--activerecord/lib/active_record/base.rb41
1 files changed, 23 insertions, 18 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index 5a57e1bc70..0c5248c576 100644
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -1206,14 +1206,6 @@ MSG
Thread.current["#{self}_current_scope"] = scope
end
- def ignore_default_scope? #:nodoc:
- Thread.current["#{self}_ignore_default_scope"]
- end
-
- def ignore_default_scope=(ignore) #:nodoc:
- Thread.current["#{self}_ignore_default_scope"] = ignore
- end
-
# Use this macro in your model to set a default scope for all operations on
# the model.
#
@@ -1264,17 +1256,11 @@ MSG
self.default_scopes = default_scopes + [scope]
end
- # The @ignore_default_scope flag is used to prevent an infinite recursion situation where
- # a default scope references a scope which has a default scope which references a scope...
def build_default_scope #:nodoc:
- return if ignore_default_scope?
-
- begin
- self.ignore_default_scope = true
-
- if method(:default_scope).owner != Base.singleton_class
- default_scope
- elsif default_scopes.any?
+ if method(:default_scope).owner != Base.singleton_class
+ evaluate_default_scope { default_scope }
+ elsif default_scopes.any?
+ evaluate_default_scope do
default_scopes.inject(relation) do |default_scope, scope|
if scope.is_a?(Hash)
default_scope.apply_finder_options(scope)
@@ -1285,6 +1271,25 @@ MSG
end
end
end
+ end
+ end
+
+ def ignore_default_scope? #:nodoc:
+ Thread.current["#{self}_ignore_default_scope"]
+ end
+
+ def ignore_default_scope=(ignore) #:nodoc:
+ Thread.current["#{self}_ignore_default_scope"] = ignore
+ end
+
+ # The ignore_default_scope flag is used to prevent an infinite recursion situation where
+ # a default scope references a scope which has a default scope which references a scope...
+ def evaluate_default_scope
+ return if ignore_default_scope?
+
+ begin
+ self.ignore_default_scope = true
+ yield
ensure
self.ignore_default_scope = false
end