aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
authorPratik Naik <pratiknaik@gmail.com>2008-11-16 23:36:41 +0530
committerPratik Naik <pratiknaik@gmail.com>2008-11-16 23:36:41 +0530
commit2530d0eea8eaecd2c61f99225f050ff47973e9a0 (patch)
treee32d4920a21417e79584c6c7a563a2cb948b4532 /activerecord/lib/active_record
parentd9f460a39b73fd2cf0f17f523cc4810d0bf44cac (diff)
downloadrails-2530d0eea8eaecd2c61f99225f050ff47973e9a0.tar.gz
rails-2530d0eea8eaecd2c61f99225f050ff47973e9a0.tar.bz2
rails-2530d0eea8eaecd2c61f99225f050ff47973e9a0.zip
Added default_scope to Base [#1381 state:committed] (Paweł Kondzior)
Diffstat (limited to 'activerecord/lib/active_record')
-rwxr-xr-xactiverecord/lib/active_record/base.rb16
1 files changed, 15 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index dcc8277849..d8b7a5a931 100755
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -495,6 +495,10 @@ module ActiveRecord #:nodoc:
superclass_delegating_accessor :store_full_sti_class
self.store_full_sti_class = false
+ # Stores the default scope for the class
+ class_inheritable_accessor :default_scoping, :instance_writer => false
+ self.default_scoping = []
+
class << self # Class methods
# Find operates with four different retrieval approaches:
#
@@ -2016,6 +2020,16 @@ module ActiveRecord #:nodoc:
@@subclasses[self] + extra = @@subclasses[self].inject([]) {|list, subclass| list + subclass.subclasses }
end
+ # Sets the default options for the model. The format of the
+ # <tt>method_scoping</tt> argument is the same as in with_scope.
+ #
+ # class Person < ActiveRecord::Base
+ # default_scope :find => { :order => 'last_name, first_name' }
+ # end
+ def default_scope(options = {})
+ self.default_scoping << { :find => options, :create => options.is_a?(Hash) ? options[:conditions] : {} }
+ end
+
# Test whether the given method and optional key are scoped.
def scoped?(method, key = nil) #:nodoc:
if current_scoped_methods && (scope = current_scoped_methods[method])
@@ -2031,7 +2045,7 @@ module ActiveRecord #:nodoc:
end
def scoped_methods #:nodoc:
- Thread.current[:"#{self}_scoped_methods"] ||= []
+ Thread.current[:"#{self}_scoped_methods"] ||= self.default_scoping
end
def current_scoped_methods #:nodoc: