Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Override AR::Relation methods in NullRelation. #5809

Merged
merged 1 commit into from

2 participants

@xuanxu

So a NullRelation (Relation#none) is chainable with database methods, as discussed with @josevalim and @jonleighton here: rails/rails#5590

@xuanxu xuanxu Override AR::Relation methods in NullRelation.
So a NullRelation (Relation#none) is chainable with database methods.
1a20683
@jonleighton jonleighton merged commit ddefdd7 into from
@xuanxu xuanxu deleted the branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 11, 2012
  1. @xuanxu

    Override AR::Relation methods in NullRelation.

    xuanxu authored
    So a NullRelation (Relation#none) is chainable with database methods.
This page is out of date. Refresh to see the latest.
View
53 activerecord/lib/active_record/null_relation.rb
@@ -6,5 +6,58 @@ class NullRelation < Relation
def exec_queries
@records = []
end
+
+ def pluck(column_name)
+ []
+ end
+
+ def delete_all(conditions = nil)
+ 0
+ end
+
+ def update_all(updates, conditions = nil, options = {})
+ 0
+ end
+
+ def delete(id_or_array)
+ 0
+ end
+
+ def size
+ 0
+ end
+
+ def empty?
+ true
+ end
+
+ def any?
+ false
+ end
+
+ def many?
+ false
+ end
+
+ def to_sql
+ @to_sql ||= ""
+ end
+
+ def where_values_hash
+ {}
+ end
+
+ def count
+ 0
+ end
+
+ def calculate(operation, column_name, options = {})
+ nil
+ end
+
+ def exists?(id = false)
+ false
+ end
+
end
end
View
33 activerecord/test/cases/relations_test.rb
@@ -219,6 +219,7 @@ 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
@@ -228,6 +229,38 @@ def test_none_chainable
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
+ assert_equal 0, Developer.none.delete_all
+ assert_equal 0, Developer.none.update_all(:name => 'David')
+ assert_equal 0, Developer.none.delete(1)
+ assert_equal false, Developer.none.exists?(1)
+ end
+ end
+
+ def test_null_relation_content_size_methods
+ assert_no_queries do
+ assert_equal 0, Developer.none.size
+ assert_equal 0, Developer.none.count
+ assert_equal true, Developer.none.empty?
+ assert_equal false, Developer.none.any?
+ assert_equal false, Developer.none.many?
+ end
+ end
+
+ def test_null_relation_calculations_methods
+ assert_no_queries do
+ assert_equal 0, Developer.none.count
+ assert_equal nil, Developer.none.calculate(:average, 'salary')
+ end
+ end
+
+ def test_null_relation_metadata_methods
+ assert_equal "", Developer.none.to_sql
+ assert_equal({}, Developer.none.where_values_hash)
+ end
+
def test_joins_with_nil_argument
assert_nothing_raised { DependentFirm.joins(nil).first }
end
Something went wrong with that request. Please try again.