Skip to content

Commit

Permalink
Merge pull request #6579 from amatsuda/null_relation_modulize
Browse files Browse the repository at this point in the history
modulize AR::NullRelation
  • Loading branch information
josevalim committed Jun 1, 2012
2 parents c1a0c77 + c8882c1 commit 47896d2
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
17 changes: 8 additions & 9 deletions activerecord/lib/active_record/null_relation.rb
Expand Up @@ -2,24 +2,24 @@

module ActiveRecord
# = Active Record Null Relation
class NullRelation < Relation
module NullRelation
def exec_queries
@records = []
end

def pluck(column_name)
def pluck(_column_name)
[]
end

def delete_all(conditions = nil)
def delete_all(_conditions = nil)
0
end

def update_all(updates, conditions = nil, options = {})
def update_all(_updates, _conditions = nil, _options = {})
0
end

def delete(id_or_array)
def delete(_id_or_array)
0
end

Expand Down Expand Up @@ -51,13 +51,12 @@ def count
0
end

def calculate(operation, column_name, options = {})
def calculate(_operation, _column_name, _options = {})
nil
end

def exists?(id = false)
def exists?(_id = false)
false
end

end
end
end
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/relation/query_methods.rb
Expand Up @@ -279,7 +279,7 @@ def lock!(locks = true)
# end
#
def none
NullRelation.new(@klass, @table)
scoped.extending(NullRelation)
end

def readonly(value = true)
Expand Down
7 changes: 6 additions & 1 deletion activerecord/test/cases/relations_test.rb
Expand Up @@ -226,7 +226,6 @@ def test_none
assert_no_queries do
assert_equal [], Developer.none
assert_equal [], Developer.scoped.none
assert Developer.none.is_a?(ActiveRecord::NullRelation)
end
end

Expand All @@ -236,6 +235,12 @@ def test_none_chainable
end
end

def test_none_chainable_to_existing_scope_extension_method
assert_no_queries do
assert_equal 1, Topic.anonymous_extension.none.one
end
end

def test_none_chained_to_methods_firing_queries_straight_to_db
assert_no_queries do
assert_equal [], Developer.none.pluck(:id) # => uses select_all
Expand Down

0 comments on commit 47896d2

Please sign in to comment.