diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index dcc8277..273e7d9 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 + # Default scope options + cattr_accessor :default_scoped_methods, :instance_writer => false + @@default_scoped_methods = {} + 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 + # method_scoping argument is the same as in named_scope. + # + # class Person << ActiveRecord::Base + # default_scope :order => 'last_name, first_name' + # end + def default_scope(options) + @@default_scoped_methods = { :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"] ||= [@@default_scoped_methods.dup] end def current_scoped_methods #:nodoc: diff --git a/activerecord/test/cases/method_scoping_test.rb b/activerecord/test/cases/method_scoping_test.rb index ff10bfa..ed5d0bd 100644 --- a/activerecord/test/cases/method_scoping_test.rb +++ b/activerecord/test/cases/method_scoping_test.rb @@ -522,6 +522,38 @@ class HasAndBelongsToManyScopingTest< ActiveRecord::TestCase end +class DefaultScopingTest < ActiveRecord::TestCase + fixtures :developers + + def test_default_scope + expected = Developer.find(:all, :order => 'salary DESC').collect { |dev| dev.salary } + received = DeveloperOrderedBySalary.find(:all).collect { |dev| dev.salary } + assert_equal expected, received + end + + def test_method_scope + expected = Developer.find(:all, :order => 'name DESC').collect { |dev| dev.salary } + received = DeveloperOrderedBySalary.all_ordered_by_name.collect { |dev| dev.salary } + assert_equal expected, received + end + + def test_nested_scope + expected = Developer.find(:all, :order => 'name DESC').collect { |dev| dev.salary } + received = DeveloperOrderedBySalary.with_scope(:find => { :order => 'name DESC'}) do + DeveloperOrderedBySalary.find(:all).collect { |dev| dev.salary } + end + assert_equal expected, received + end + + def test_nested_exclusive_scope + expected = Developer.find(:all, :limit => 100).collect { |dev| dev.salary } + received = DeveloperOrderedBySalary.with_exclusive_scope(:find => { :limit => 100 }) do + DeveloperOrderedBySalary.find(:all).collect { |dev| dev.salary } + end + assert_equal expected, received + end +end + =begin # We disabled the scoping for has_one and belongs_to as we can't think of a proper use case diff --git a/activerecord/test/models/developer.rb b/activerecord/test/models/developer.rb index c08476f..1844014 100644 --- a/activerecord/test/models/developer.rb +++ b/activerecord/test/models/developer.rb @@ -77,3 +77,15 @@ class DeveloperWithBeforeDestroyRaise < ActiveRecord::Base raise if projects.empty? end end + +class DeveloperOrderedBySalary < ActiveRecord::Base + self.table_name = 'developers' + default_scope :order => "salary DESC" + + def self.all_ordered_by_name + with_scope(:find => { :order => "name DESC" }) do + find(:all) + end + end + +end