1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Remove all cases of manuallly wrapping Arel::Nodes::Quoted

This is no longer required now that we are injecting a type caster
object into the Arel table, with the exception of uniqueness
validations. Since it calls `ConnectionAdapter#type_cast`, the value has
already been cast for the database. We don't want Arel to attempt to
cast it further, so we need to continue wrapping it in a quoted node.
This can potentially go away when this validator is refactored to make
better use of `where` or the predicate builder.
This commit is contained in:
Sean Griffin 2014-12-29 11:21:56 -07:00
parent 7931c96338
commit f916aa247b
7 changed files with 15 additions and 34 deletions

View file

@ -193,7 +193,6 @@ module ActiveRecord
def type_condition(table = arel_table) def type_condition(table = arel_table)
sti_column = table[inheritance_column] sti_column = table[inheritance_column]
sti_names = ([self] + descendants).map(&:sti_name) sti_names = ([self] + descendants).map(&:sti_name)
sti_names.map! { |v| Arel::Nodes::Quoted.new(v) } # FIXME: Remove this when type casting in Arel is removed (5.1)
sti_column.in(sti_names) sti_column.in(sti_names)
end end

View file

@ -52,12 +52,7 @@ module ActiveRecord
end end
else else
enum_for :find_each, options do enum_for :find_each, options do
# FIXME: Remove this when type casting is removed from Arel options[:start] ? where(table[primary_key].gteq(options[:start])).size : size
# (Rails 5.1). We can pass start directly instead.
if options[:start]
quoted_start = Arel::Nodes::Quoted.new(options[:start])
end
options[:start] ? where(table[primary_key].gteq(quoted_start)).size : size
end end
end end
end end
@ -107,15 +102,9 @@ module ActiveRecord
start = options[:start] start = options[:start]
batch_size = options[:batch_size] || 1000 batch_size = options[:batch_size] || 1000
if start
# FIXME: Remove this when type casting is removed from Arel
# (Rails 5.1). We can pass start directly instead.
quoted_start = Arel::Nodes::Quoted.new(start)
end
unless block_given? unless block_given?
return to_enum(:find_in_batches, options) do return to_enum(:find_in_batches, options) do
total = start ? where(table[primary_key].gteq(quoted_start)).size : size total = start ? where(table[primary_key].gteq(start)).size : size
(total - 1).div(batch_size) + 1 (total - 1).div(batch_size) + 1
end end
end end
@ -125,7 +114,7 @@ module ActiveRecord
end end
relation = relation.reorder(batch_order).limit(batch_size) relation = relation.reorder(batch_order).limit(batch_size)
records = start ? relation.where(table[primary_key].gteq(quoted_start)).to_a : relation.to_a records = start ? relation.where(table[primary_key].gteq(start)).to_a : relation.to_a
while records.any? while records.any?
records_size = records.size records_size = records.size
@ -136,11 +125,7 @@ module ActiveRecord
break if records_size < batch_size break if records_size < batch_size
# FIXME: Remove this when type casting is removed from Arel records = relation.where(table[primary_key].gt(primary_key_offset)).to_a
# (Rails 5.1). We can pass the offset directly instead.
quoted_offset = Arel::Nodes::Quoted.new(primary_key_offset)
records = relation.where(table[primary_key].gt(quoted_offset)).to_a
end end
end end

View file

@ -64,7 +64,6 @@ module ActiveRecord
value = value.to_s[0, column.limit] value = value.to_s[0, column.limit]
end end
# FIXME: Remove this when type casting is removed from Arel (Rails 5.1)
value = Arel::Nodes::Quoted.new(value) value = Arel::Nodes::Quoted.new(value)
comparison = if !options[:case_sensitive] && value && column.text? comparison = if !options[:case_sensitive] && value && column.text?

View file

@ -25,8 +25,8 @@ class RelationMergingTest < ActiveRecord::TestCase
end end
def test_relation_merging_with_arel_equalities_keeps_last_equality def test_relation_merging_with_arel_equalities_keeps_last_equality
devs = Developer.where(Developer.arel_table[:salary].eq(Arel::Nodes::Quoted.new(80000))).merge( devs = Developer.where(Developer.arel_table[:salary].eq(80000)).merge(
Developer.where(Developer.arel_table[:salary].eq(Arel::Nodes::Quoted.new(9000))) Developer.where(Developer.arel_table[:salary].eq(9000))
) )
assert_equal [developers(:poor_jamis)], devs.to_a assert_equal [developers(:poor_jamis)], devs.to_a
end end

View file

@ -64,21 +64,21 @@ module ActiveRecord
def test_has_values def test_has_values
relation = Relation.new(Post, Post.arel_table, Post.predicate_builder) relation = Relation.new(Post, Post.arel_table, Post.predicate_builder)
relation.where! relation.table[:id].eq(Arel::Nodes::Quoted.new(10)) relation.where! relation.table[:id].eq(10)
assert_equal({:id => 10}, relation.where_values_hash) assert_equal({:id => 10}, relation.where_values_hash)
end end
def test_values_wrong_table def test_values_wrong_table
relation = Relation.new(Post, Post.arel_table, Post.predicate_builder) relation = Relation.new(Post, Post.arel_table, Post.predicate_builder)
relation.where! Comment.arel_table[:id].eq(Arel::Nodes::Quoted.new(10)) relation.where! Comment.arel_table[:id].eq(10)
assert_equal({}, relation.where_values_hash) assert_equal({}, relation.where_values_hash)
end end
def test_tree_is_not_traversed def test_tree_is_not_traversed
relation = Relation.new(Post, Post.arel_table, Post.predicate_builder) relation = Relation.new(Post, Post.arel_table, Post.predicate_builder)
# FIXME: Remove the Arel::Nodes::Quoted in Rails 5.1 # FIXME: Remove the Arel::Nodes::Quoted in Rails 5.1
left = relation.table[:id].eq(Arel::Nodes::Quoted.new(10)) left = relation.table[:id].eq(10)
right = relation.table[:id].eq(Arel::Nodes::Quoted.new(10)) right = relation.table[:id].eq(10)
combine = left.and right combine = left.and right
relation.where! combine relation.where! combine
assert_equal({}, relation.where_values_hash) assert_equal({}, relation.where_values_hash)
@ -104,7 +104,7 @@ module ActiveRecord
def test_create_with_value_with_wheres def test_create_with_value_with_wheres
relation = Relation.new(Post, Post.arel_table, Post.predicate_builder) relation = Relation.new(Post, Post.arel_table, Post.predicate_builder)
# FIXME: Remove the Arel::Nodes::Quoted in Rails 5.1 # FIXME: Remove the Arel::Nodes::Quoted in Rails 5.1
relation.where! relation.table[:id].eq(Arel::Nodes::Quoted.new(10)) relation.where! relation.table[:id].eq(10)
relation.create_with_value = {:hello => 'world'} relation.create_with_value = {:hello => 'world'}
assert_equal({:hello => 'world', :id => 10}, relation.scope_for_create) assert_equal({:hello => 'world', :id => 10}, relation.scope_for_create)
end end
@ -115,7 +115,7 @@ module ActiveRecord
assert_equal({}, relation.scope_for_create) assert_equal({}, relation.scope_for_create)
# FIXME: Remove the Arel::Nodes::Quoted in Rails 5.1 # FIXME: Remove the Arel::Nodes::Quoted in Rails 5.1
relation.where! relation.table[:id].eq(Arel::Nodes::Quoted.new(10)) relation.where! relation.table[:id].eq(10)
assert_equal({}, relation.scope_for_create) assert_equal({}, relation.scope_for_create)
relation.create_with_value = {:hello => 'world'} relation.create_with_value = {:hello => 'world'}

View file

@ -145,13 +145,11 @@ class DefaultScopingTest < ActiveRecord::TestCase
assert_equal expected_5, received_5 assert_equal expected_5, received_5
expected_6 = Developer.order('salary DESC').collect(&:name) expected_6 = Developer.order('salary DESC').collect(&:name)
# FIXME: Remove the Arel::Nodes::Quoted in Rails 5.1 received_6 = DeveloperOrderedBySalary.where(Developer.arel_table['name'].eq('David')).unscope(where: :name).collect(&:name)
received_6 = DeveloperOrderedBySalary.where(Developer.arel_table['name'].eq(Arel::Nodes::Quoted.new('David'))).unscope(where: :name).collect(&:name)
assert_equal expected_6, received_6 assert_equal expected_6, received_6
expected_7 = Developer.order('salary DESC').collect(&:name) expected_7 = Developer.order('salary DESC').collect(&:name)
# FIXME: Remove the Arel::Nodes::Quoted in Rails 5.1 received_7 = DeveloperOrderedBySalary.where(Developer.arel_table[:name].eq('David')).unscope(where: :name).collect(&:name)
received_7 = DeveloperOrderedBySalary.where(Developer.arel_table[:name].eq(Arel::Nodes::Quoted.new('David'))).unscope(where: :name).collect(&:name)
assert_equal expected_7, received_7 assert_equal expected_7, received_7
end end

View file

@ -34,7 +34,7 @@ class Author < ActiveRecord::Base
-> { where(title: 'Welcome to the weblog').where('comments_count = ?', 1) }, -> { where(title: 'Welcome to the weblog').where('comments_count = ?', 1) },
class_name: 'Post' class_name: 'Post'
has_many :welcome_posts_with_comments, has_many :welcome_posts_with_comments,
-> { where(title: 'Welcome to the weblog').where(Post.arel_table[:comments_count].gt(Arel::Nodes::Quoted.new(0))) }, -> { where(title: 'Welcome to the weblog').where(Post.arel_table[:comments_count].gt(0)) },
class_name: 'Post' class_name: 'Post'
has_many :comments_desc, -> { order('comments.id DESC') }, :through => :posts, :source => :comments has_many :comments_desc, -> { order('comments.id DESC') }, :through => :posts, :source => :comments