Skip to content
This repository
Browse code

Added default_scope to Base [#1381 state:committed] (Paweł Kondzior)

  • Loading branch information...
commit ff594b2bc94ff2a942fe6ca05672387722dee686 1 parent c5448c7
David Heinemeier Hansson authored November 16, 2008
16  activerecord/CHANGELOG
... ...
@@ -1,3 +1,19 @@
  1
+*2.3.0/3.0*
  2
+
  3
+* Added default_scope to Base #1381 [Paweł Kondzior]. Example:
  4
+
  5
+    class Person < ActiveRecord::Base
  6
+      default_scope :order => 'last_name, first_name'
  7
+    end
  8
+    
  9
+    class Company < ActiveRecord::Base
  10
+      has_many :people
  11
+    end
  12
+    
  13
+    Person.all             # => Person.find(:all, :order => 'last_name, first_name')
  14
+    Company.find(1).people # => Person.find(:all, :order => 'last_name, first_name', :conditions => { :company_id => 1 })
  15
+
  16
+
1 17
 *2.2.1 [RC2] (November 14th, 2008)*
2 18
 
3 19
 * Ensure indices don't flip order in schema.rb #1266 [Jordi Bunster]
10  activerecord/lib/active_record/base.rb
@@ -2016,6 +2016,16 @@ def subclasses #:nodoc:
2016 2016
           @@subclasses[self] + extra = @@subclasses[self].inject([]) {|list, subclass| list + subclass.subclasses }
2017 2017
         end
2018 2018
 
  2019
+        # Sets the default options for the model. The format of the
  2020
+        # <tt>method_scoping</tt> argument is the same as in with_scope.
  2021
+        #
  2022
+        #   class Person << ActiveRecord::Base
  2023
+        #     default_scope :find => { :order => 'last_name, first_name' }
  2024
+        #   end
  2025
+        def default_scope(options = {})
  2026
+          self.scoped_methods << { :find => options, :create => options.is_a?(Hash) ?  options[:conditions] : {} }
  2027
+        end
  2028
+
2019 2029
         # Test whether the given method and optional key are scoped.
2020 2030
         def scoped?(method, key = nil) #:nodoc:
2021 2031
           if current_scoped_methods && (scope = current_scoped_methods[method])
38  activerecord/test/cases/method_scoping_test.rb
@@ -522,6 +522,44 @@ def test_nested_scope
522 522
 end
523 523
 
524 524
 
  525
+class DefaultScopingTest < ActiveRecord::TestCase
  526
+  fixtures :developers
  527
+
  528
+  def test_default_scope
  529
+    expected = Developer.find(:all, :order => 'salary DESC').collect { |dev| dev.salary }
  530
+    received = DeveloperOrderedBySalary.find(:all).collect { |dev| dev.salary }
  531
+    assert_equal expected, received
  532
+  end
  533
+
  534
+  def test_method_scope
  535
+    expected = Developer.find(:all, :order => 'name DESC').collect { |dev| dev.salary }
  536
+    received = DeveloperOrderedBySalary.all_ordered_by_name.collect { |dev| dev.salary }
  537
+    assert_equal expected, received
  538
+  end
  539
+
  540
+  def test_nested_scope
  541
+    expected = Developer.find(:all, :order => 'name DESC').collect { |dev| dev.salary }
  542
+    received = DeveloperOrderedBySalary.with_scope(:find => { :order => 'name DESC'}) do
  543
+      DeveloperOrderedBySalary.find(:all).collect { |dev| dev.salary }
  544
+    end
  545
+    assert_equal expected, received
  546
+  end
  547
+
  548
+  def test_nested_exclusive_scope
  549
+    expected = Developer.find(:all, :limit => 100).collect { |dev| dev.salary }
  550
+    received = DeveloperOrderedBySalary.with_exclusive_scope(:find => { :limit => 100 }) do
  551
+      DeveloperOrderedBySalary.find(:all).collect { |dev| dev.salary }
  552
+    end
  553
+    assert_equal expected, received
  554
+  end
  555
+  
  556
+  def test_overwriting_default_scope
  557
+    expected = Developer.find(:all, :order => 'salary').collect { |dev| dev.salary }
  558
+    received = DeveloperOrderedBySalary.find(:all, :order => 'salary').collect { |dev| dev.salary }
  559
+    assert_equal expected, received
  560
+  end
  561
+end
  562
+
525 563
 =begin
526 564
 # We disabled the scoping for has_one and belongs_to as we can't think of a proper use case
527 565
 
12  activerecord/test/models/developer.rb
@@ -77,3 +77,15 @@ def raise_if_projects_empty!
77 77
     raise if projects.empty?
78 78
   end
79 79
 end
  80
+
  81
+class DeveloperOrderedBySalary < ActiveRecord::Base
  82
+    self.table_name = 'developers'
  83
+    default_scope :order => "salary DESC" 
  84
+    
  85
+    def self.all_ordered_by_name
  86
+      with_scope(:find => { :order => "name DESC" }) do
  87
+        find(:all)
  88
+      end
  89
+    end
  90
+
  91
+end

0 notes on commit ff594b2

Please sign in to comment.
Something went wrong with that request. Please try again.