Add intersects? to Relation

Ruby 3.1 added `intersects?` which is equivalent to `(a & b).any?`. Rubocop added a corresponding cop, `Style/ArrayIntersect`, which transforms the old style to use `intersect?`. Unfortunately as `intersects?` is not delegated on `CollectionProxy`, this leads to false positives that need to be disabled for no good reason other than the fact the method isn't delegated.

This PR add delegation of `intersects?` to `Relation` which fixes this.
This commit is contained in:
John Kelly 2023-03-22 16:21:27 +00:00 committed by Jean Boussier
parent eac3208b80
commit c837ae624b
3 changed files with 10 additions and 2 deletions

@ -1,3 +1,11 @@
* Add support for `Array#intersect?` to `ActiveRecord::Relation`.
`Array#intersect?` is only available on Ruby 3.1 or later.
This allows the Rubocop `Style/ArrayIntersect` cop to work with `ActiveRecord::Relation` objects.
*John Harry Kelly*
* The deferrable foreign key can be passed to `t.references`.
*Hiroyuki Ishii*

@ -97,7 +97,7 @@ def #{method}(...)
# may vary depending on the klass of a relation, so we create a subclass of Relation
# for each different klass, and the delegations are compiled into that subclass only.
delegate :to_xml, :encode_with, :length, :each, :join,
delegate :to_xml, :encode_with, :length, :each, :join, :intersects?,
:[], :&, :|, :+, :-, :sample, :reverse, :rotate, :compact, :in_groups, :in_groups_of,
:to_sentence, :to_fs, :to_formatted_s, :as_json,
:shuffle, :split, :slice, :index, :rindex, to: :records

@ -9,7 +9,7 @@
module ActiveRecord
module DelegationTests
ARRAY_DELEGATES = [
:+, :-, :|, :&, :[], :shuffle,
:+, :-, :|, :&, :[], :shuffle, :intersects?,
:all?, :collect, :compact, :detect, :each, :each_cons, :each_with_index,
:exclude?, :find_all, :flat_map, :group_by, :include?, :length,
:map, :none?, :one?, :partition, :reject, :reverse, :rotate,