Skip to content
This repository
Browse code

Returning nil from named scope lambda is equivalent to an empty hash [#…

…1773 state:resolved]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
  • Loading branch information...
commit 91b98cf0a5417ce4042a0b3cd1930d5a221b737f 1 parent aa57e66
Elijah Miller authored March 12, 2009 lifo committed March 12, 2009
1  activerecord/lib/active_record/named_scope.rb
@@ -117,6 +117,7 @@ class Scope
117 117
       delegate :scopes, :with_scope, :to => :proxy_scope
118 118
 
119 119
       def initialize(proxy_scope, options, &block)
  120
+        options ||= {}
120 121
         [options[:extend]].flatten.each { |extension| extend extension } if options[:extend]
121 122
         extend Module.new(&block) if block_given?
122 123
         unless Scope === proxy_scope
6  activerecord/test/cases/named_scope_test.rb
@@ -99,6 +99,12 @@ def test_procedural_scopes
99 99
     assert_equal topics_written_before_the_second, Topic.written_before(topics(:second).written_on)
100 100
   end
101 101
 
  102
+  def test_procedural_scopes_returning_nil
  103
+    all_topics = Topic.find(:all)
  104
+
  105
+    assert_equal all_topics, Topic.written_before(nil)
  106
+  end
  107
+
102 108
   def test_scopes_with_joins
103 109
     address = author_addresses(:david_address)
104 110
     posts_with_authors_at_address = Post.find(
4  activerecord/test/models/topic.rb
... ...
@@ -1,7 +1,9 @@
1 1
 class Topic < ActiveRecord::Base
2 2
   named_scope :base
3 3
   named_scope :written_before, lambda { |time|
4  
-    { :conditions => ['written_on < ?', time] }
  4
+    if time
  5
+      { :conditions => ['written_on < ?', time] }
  6
+    end
5 7
   }
6 8
   named_scope :approved, :conditions => {:approved => true}
7 9
   named_scope :rejected, :conditions => {:approved => false}

0 notes on commit 91b98cf

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