提交 70b377f4 编写于 作者: E Earl J St Sauver 提交者: Earl St Sauver

select! renamed to avoid name collision Array#select!

Fixes #14752

Select mimics the block interface of arrays, but does not mock the
block interface for select!. This change moves the api to be a
private method, _select!.
上级 43f52503
......@@ -116,7 +116,7 @@ def build_scope
scope.where_values = Array(values[:where]) + Array(preload_values[:where])
scope.references_values = Array(values[:references]) + Array(preload_values[:references])
scope.select! preload_values[:select] || values[:select] || table[Arel.star]
scope._select! preload_values[:select] || values[:select] || table[Arel.star]
scope.includes! preload_values[:includes] || values[:includes]
if preload_values.key? :order
......
......@@ -40,7 +40,7 @@ def inherited(child_class)
BLACKLISTED_ARRAY_METHODS = [
:compact!, :flatten!, :reject!, :reverse!, :rotate!, :map!,
:shuffle!, :slice!, :sort!, :sort_by!, :delete_if,
:keep_if, :pop, :shift, :delete_at, :compact
:keep_if, :pop, :shift, :delete_at, :compact, :select!
].to_set # :nodoc:
delegate :to_xml, :to_yaml, :length, :collect, :map, :each, :all?, :include?, :to_ary, to: :to_a
......
......@@ -30,6 +30,8 @@ def other
else
other.joins!(*v)
end
elsif k == :select
other._select!(v)
else
other.send("#{k}!", v)
end
......@@ -62,7 +64,13 @@ def merge
# expensive), most of the time the value is going to be `nil` or `.blank?`, the only catch is that
# `false.blank?` returns `true`, so there needs to be an extra check so that explicit `false` values
# don't fall through the cracks.
relation.send("#{name}!", *value) unless value.nil? || (value.blank? && false != value)
unless value.nil? || (value.blank? && false != value)
if name == :select
relation._select!(*value)
else
relation.send("#{name}!", *value)
end
end
end
merge_multi_values
......
......@@ -235,11 +235,11 @@ def select(*fields)
to_a.select { |*block_args| yield(*block_args) }
else
raise ArgumentError, 'Call this with at least one field' if fields.empty?
spawn.select!(*fields)
spawn._select!(*fields)
end
end
def select!(*fields) # :nodoc:
def _select!(*fields) # :nodoc:
fields.flatten!
fields.map! do |field|
klass.attribute_alias?(field) ? klass.attribute_alias(field) : field
......
......@@ -24,13 +24,18 @@ def relation
@relation ||= Relation.new FakeKlass.new('posts'), Post.arel_table
end
(Relation::MULTI_VALUE_METHODS - [:references, :extending, :order, :unscope]).each do |method|
(Relation::MULTI_VALUE_METHODS - [:references, :extending, :order, :unscope, :select]).each do |method|
test "##{method}!" do
assert relation.public_send("#{method}!", :foo).equal?(relation)
assert_equal [:foo], relation.public_send("#{method}_values")
end
end
test "#_select!" do
assert relation.public_send("_select!", :foo).equal?(relation)
assert_equal [:foo], relation.public_send("select_values")
end
test '#order!' do
assert relation.order!('name ASC').equal?(relation)
assert_equal ['name ASC'], relation.order_values
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册