Skip to content

Commit

Permalink
Returning nil from named scope lambda is equivalent to an empty hash [#…
Browse files Browse the repository at this point in the history
…1773 state:resolved]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
  • Loading branch information
jqr authored and lifo committed Mar 12, 2009
1 parent aa57e66 commit 91b98cf
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
1 change: 1 addition & 0 deletions activerecord/lib/active_record/named_scope.rb
Expand Up @@ -117,6 +117,7 @@ class Scope
delegate :scopes, :with_scope, :to => :proxy_scope

def initialize(proxy_scope, options, &block)
options ||= {}
[options[:extend]].flatten.each { |extension| extend extension } if options[:extend]
extend Module.new(&block) if block_given?
unless Scope === proxy_scope
Expand Down
6 changes: 6 additions & 0 deletions activerecord/test/cases/named_scope_test.rb
Expand Up @@ -99,6 +99,12 @@ def test_procedural_scopes
assert_equal topics_written_before_the_second, Topic.written_before(topics(:second).written_on)
end

def test_procedural_scopes_returning_nil
all_topics = Topic.find(:all)

assert_equal all_topics, Topic.written_before(nil)
end

def test_scopes_with_joins
address = author_addresses(:david_address)
posts_with_authors_at_address = Post.find(
Expand Down
4 changes: 3 additions & 1 deletion activerecord/test/models/topic.rb
@@ -1,7 +1,9 @@
class Topic < ActiveRecord::Base
named_scope :base
named_scope :written_before, lambda { |time|
{ :conditions => ['written_on < ?', time] }
if time
{ :conditions => ['written_on < ?', time] }
end
}
named_scope :approved, :conditions => {:approved => true}
named_scope :rejected, :conditions => {:approved => false}
Expand Down

0 comments on commit 91b98cf

Please sign in to comment.