提交 f916aa24 编写于 作者: S Sean Griffin

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.
上级 7931c963
......@@ -193,7 +193,6 @@ def find_sti_class(type_name)
def type_condition(table = arel_table)
sti_column = table[inheritance_column]
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)
end
......
......@@ -52,12 +52,7 @@ def find_each(options = {})
end
else
enum_for :find_each, options do
# FIXME: Remove this when type casting is removed from Arel
# (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
options[:start] ? where(table[primary_key].gteq(options[:start])).size : size
end
end
end
......@@ -107,15 +102,9 @@ def find_in_batches(options = {})
start = options[:start]
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?
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
end
end
......@@ -125,7 +114,7 @@ def find_in_batches(options = {})
end
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?
records_size = records.size
......@@ -136,11 +125,7 @@ def find_in_batches(options = {})
break if records_size < batch_size
# FIXME: Remove this when type casting is removed from Arel
# (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
records = relation.where(table[primary_key].gt(primary_key_offset)).to_a
end
end
......
......@@ -64,7 +64,6 @@ def build_relation(klass, table, attribute, value) #:nodoc:
value = value.to_s[0, column.limit]
end
# FIXME: Remove this when type casting is removed from Arel (Rails 5.1)
value = Arel::Nodes::Quoted.new(value)
comparison = if !options[:case_sensitive] && value && column.text?
......
......@@ -25,8 +25,8 @@ def test_relation_to_sql
end
def test_relation_merging_with_arel_equalities_keeps_last_equality
devs = Developer.where(Developer.arel_table[:salary].eq(Arel::Nodes::Quoted.new(80000))).merge(
Developer.where(Developer.arel_table[:salary].eq(Arel::Nodes::Quoted.new(9000)))
devs = Developer.where(Developer.arel_table[:salary].eq(80000)).merge(
Developer.where(Developer.arel_table[:salary].eq(9000))
)
assert_equal [developers(:poor_jamis)], devs.to_a
end
......
......@@ -64,21 +64,21 @@ def test_empty_where_values_hash
def test_has_values
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)
end
def test_values_wrong_table
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)
end
def test_tree_is_not_traversed
relation = Relation.new(Post, Post.arel_table, Post.predicate_builder)
# FIXME: Remove the Arel::Nodes::Quoted in Rails 5.1
left = relation.table[:id].eq(Arel::Nodes::Quoted.new(10))
right = relation.table[:id].eq(Arel::Nodes::Quoted.new(10))
left = relation.table[:id].eq(10)
right = relation.table[:id].eq(10)
combine = left.and right
relation.where! combine
assert_equal({}, relation.where_values_hash)
......@@ -104,7 +104,7 @@ def test_create_with_value
def test_create_with_value_with_wheres
relation = Relation.new(Post, Post.arel_table, Post.predicate_builder)
# 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'}
assert_equal({:hello => 'world', :id => 10}, relation.scope_for_create)
end
......@@ -115,7 +115,7 @@ def test_scope_for_create_is_cached
assert_equal({}, relation.scope_for_create)
# 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)
relation.create_with_value = {:hello => 'world'}
......
......@@ -145,13 +145,11 @@ def test_unscope_with_where_attributes
assert_equal expected_5, received_5
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(Arel::Nodes::Quoted.new('David'))).unscope(where: :name).collect(&:name)
received_6 = DeveloperOrderedBySalary.where(Developer.arel_table['name'].eq('David')).unscope(where: :name).collect(&:name)
assert_equal expected_6, received_6
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(Arel::Nodes::Quoted.new('David'))).unscope(where: :name).collect(&:name)
received_7 = DeveloperOrderedBySalary.where(Developer.arel_table[:name].eq('David')).unscope(where: :name).collect(&:name)
assert_equal expected_7, received_7
end
......
......@@ -34,7 +34,7 @@ def ratings
-> { where(title: 'Welcome to the weblog').where('comments_count = ?', 1) },
class_name: 'Post'
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'
has_many :comments_desc, -> { order('comments.id DESC') }, :through => :posts, :source => :comments
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册