Skip to content

Commit

Permalink
with_exclusive_scope does not work properly if ARel is passed. It doe…
Browse files Browse the repository at this point in the history
…s work nicely if hash is passed. Blow up if user is attempting it pass ARel to with_exclusive_scope.

[#3838 state:resolved]

Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information
Neeraj Singh authored and josevalim committed Jun 28, 2010
1 parent f8011e6 commit 40e87ac
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
13 changes: 13 additions & 0 deletions activerecord/lib/active_record/base.rb
Expand Up @@ -1156,7 +1156,20 @@ def with_scope(method_scoping = {}, action = :merge, &block)
end

# Works like with_scope, but discards any nested properties.
# TODO : this method should be deprecated in favor of standard query API
def with_exclusive_scope(method_scoping = {}, &block)
if method_scoping.values.any? { |e| e.is_a?(ActiveRecord::Relation) }
msg =<<-MSG
ARel can not be used with_exclusive_scope. You can either specify hash style conditions to with_exclusive_scope like this:
User.with_exclusive_scope {:find => :conditions => {:active => true} } do
end
Or you can use unscoped method instead of with_exclusive_scope like this:
User.unscoped.where(:active => true) do
end
MSG
raise ArgumentError.new(msg)
end
with_scope(method_scoping, :overwrite, &block)
end

Expand Down
6 changes: 6 additions & 0 deletions activerecord/test/cases/method_scoping_test.rb
Expand Up @@ -283,6 +283,12 @@ def test_replace_options
end
end

def test_with_exclusive_scope_with_relation
assert_raise(ArgumentError) do
Developer.all_johns
end
end

def test_append_conditions
Developer.send(:with_scope, :find => { :conditions => "name = 'David'" }) do
Developer.send(:with_scope, :find => { :conditions => 'salary = 80000' }) do
Expand Down
8 changes: 7 additions & 1 deletion activerecord/test/models/developer.rb
Expand Up @@ -55,6 +55,12 @@ def find_least_recent
def log=(message)
audit_logs.build :message => message
end

def self.all_johns
self.with_exclusive_scope :find => where(:name => 'John') do
self.all
end
end
end

class AuditLog < ActiveRecord::Base
Expand Down Expand Up @@ -103,4 +109,4 @@ class DeveloperCalledJamis < ActiveRecord::Base
class PoorDeveloperCalledJamis < ActiveRecord::Base
self.table_name = 'developers'
default_scope :conditions => { :name => 'Jamis', :salary => 50000 }
end
end

0 comments on commit 40e87ac

Please sign in to comment.