Skip to content

Commit

Permalink
Make sure default_scope#create checks for options[:conditions] [#2181
Browse files Browse the repository at this point in the history
…state:resolved] [James Le Cuirot]
  • Loading branch information
lifo committed May 18, 2009
1 parent 5e190ef commit c5e109b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/base.rb
Expand Up @@ -2176,7 +2176,7 @@ def subclasses #:nodoc:
# default_scope :order => 'last_name, first_name'
# end
def default_scope(options = {})
self.default_scoping << { :find => options, :create => (options.is_a?(Hash) && options.has_key?(:conditions)) ? options[:conditions] : {} }
self.default_scoping << { :find => options, :create => options[:conditions].is_a?(Hash) ? options[:conditions] : {} }
end

# Test whether the given method and optional key are scoped.
Expand Down
10 changes: 10 additions & 0 deletions activerecord/test/cases/method_scoping_test.rb
Expand Up @@ -591,6 +591,16 @@ def test_default_scope
assert_equal expected, received
end

def test_default_scope_with_conditions_string
assert_equal Developer.find_all_by_name('David').map(&:id).sort, DeveloperCalledDavid.all.map(&:id).sort
assert_equal nil, DeveloperCalledDavid.create!.name
end

def test_default_scope_with_conditions_hash
assert_equal Developer.find_all_by_name('Jamis').map(&:id).sort, DeveloperCalledJamis.all.map(&:id).sort
assert_equal 'Jamis', DeveloperCalledJamis.create!.name
end

def test_default_scoping_with_threads
scope = [{ :create => {}, :find => { :order => 'salary DESC' } }]

Expand Down
10 changes: 10 additions & 0 deletions activerecord/test/models/developer.rb
Expand Up @@ -89,3 +89,13 @@ def self.all_ordered_by_name
end
end
end

class DeveloperCalledDavid < ActiveRecord::Base
self.table_name = 'developers'
default_scope :conditions => "name = 'David'"
end

class DeveloperCalledJamis < ActiveRecord::Base
self.table_name = 'developers'
default_scope :conditions => { :name => 'Jamis' }
end

0 comments on commit c5e109b

Please sign in to comment.