Skip to content

Commit

Permalink
Merge pull request #76 from GeorgeKaraszi/bugfix/ar-61-contains-overl…
Browse files Browse the repository at this point in the history
…ap-fix

Deprecate old contains & overlap code for AR 6.1+
  • Loading branch information
GeorgeKaraszi authored Jul 27, 2022
2 parents b73cea5 + 942bbb3 commit f97b78a
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 16 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@

- Dropped Rails 5.1 support.
- Dropped Ruby 2.4 support.
- Arel for ActiveRecord 6.1+ `contains` will no longer accept INET (ip address column types), use `inet_contains` instead.
- ActiveRecord `contains` should remain unaffected as it never accepted INET column types.

#### Bug fixes:

- [#73](https://github.com/GeorgeKaraszi/ActiveRecordExtended/issues/73) [#77](https://github.com/GeorgeKaraszi/ActiveRecordExtended/pull/77) Fix union and window query merging
- [#74](https://github.com/GeorgeKaraszi/ActiveRecordExtended/issues/74) Fix visitor conflict in Arel 6.1+ and support their native implementation of `contains` and `overlap` functionality

# 2.1.1 - February 14th 2022

Expand Down
5 changes: 5 additions & 0 deletions lib/active_record_extended/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
require "active_record/relation/merger"
require "active_record/relation/query_methods"

module ActiveRecordExtended
# TODO: Deprecate <= AR 6.0 methods & routines
AR_VERSION_GTE_6_1 = Gem::Requirement.new(">= 6.1").satisfied_by?(ActiveRecord.gem_version)
end

require "active_record_extended/predicate_builder/array_handler_decorator"

require "active_record_extended/active_record/relation_patch"
Expand Down
2 changes: 1 addition & 1 deletion lib/active_record_extended/arel/nodes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

module Arel
module Nodes
if Gem::Requirement.new("< 6.1").satisfied_by?(ActiveRecord.gem_version)
unless ActiveRecordExtended::AR_VERSION_GTE_6_1
["Contains", "Overlaps"].each { |binary_node_name| const_set(binary_node_name, Class.new(::Arel::Nodes::Binary)) }
end

Expand Down
28 changes: 16 additions & 12 deletions lib/active_record_extended/arel/visitors/postgresql_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,25 @@ module PostgreSQLDecorator

# rubocop:disable Naming/MethodName

def visit_Arel_Nodes_Overlaps(object, collector)
infix_value object, collector, " && "
unless ActiveRecordExtended::AR_VERSION_GTE_6_1
def visit_Arel_Nodes_Overlaps(object, collector)
infix_value object, collector, " && "
end
end

def visit_Arel_Nodes_Contains(object, collector)
left_column = object.left.relation.name.classify.constantize.columns.detect do |col|
matchable_column?(col, object)
end
unless ActiveRecordExtended::AR_VERSION_GTE_6_1
def visit_Arel_Nodes_Contains(object, collector)
left_column = object.left.relation.name.classify.constantize.columns.detect do |col|
matchable_column?(col, object)
end

if [:hstore, :jsonb].include?(left_column&.type)
visit_Arel_Nodes_ContainsHStore(object, collector)
elsif left_column.try(:array)
visit_Arel_Nodes_ContainsArray(object, collector)
else
visit_Arel_Nodes_Inet_Contains(object, collector)
if [:hstore, :jsonb].include?(left_column&.type)
visit_Arel_Nodes_ContainsHStore(object, collector)
elsif left_column.try(:array)
visit_Arel_Nodes_ContainsArray(object, collector)
else
visit_Arel_Nodes_Inet_Contains(object, collector)
end
end
end

Expand Down
8 changes: 5 additions & 3 deletions lib/active_record_extended/query_methods/where_chain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

module ActiveRecordExtended
module WhereChain
AR_VERSION_AT_LEAST_6_1 = ActiveRecord.version >= Gem::Version.new("6.1")

# Finds Records that have an array column that contain any a set of values
# User.where.overlap(tags: [1,2])
# # SELECT * FROM users WHERE tags && {1,2}
Expand Down Expand Up @@ -47,6 +45,10 @@ def all(opts, *rest)
# # SELECT tags.* FROM tags INNER JOIN user on user.id = tags.user_id WHERE user.data @> { nickname: 'chainer' }
#
def contains(opts, *rest)
if ActiveRecordExtended::AR_VERSION_GTE_6_1
return substitute_comparisons(opts, rest, Arel::Nodes::Contains, "contains")
end

build_where_chain(opts, rest) do |arel|
case arel
when Arel::Nodes::In, Arel::Nodes::Equality
Expand Down Expand Up @@ -108,7 +110,7 @@ def substitute_comparisons(opts, rest, arel_node_class, method)
end

def build_where_clause_for(scope, opts, rest)
if AR_VERSION_AT_LEAST_6_1
if ActiveRecordExtended::AR_VERSION_GTE_6_1
scope.send(:build_where_clause, opts, rest)
else
scope.send(:where_clause_factory).build(opts, rest)
Expand Down

0 comments on commit f97b78a

Please sign in to comment.