Skip to content

Commit

Permalink
Merge pull request #127 from rocket-turtle/ISSUE-126
Browse files Browse the repository at this point in the history
Issue 126 - Update ransack to 4.1
  • Loading branch information
rzane authored Nov 4, 2023
2 parents 515fe68 + 0e16abb commit c5ec16c
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 81 deletions.
7 changes: 2 additions & 5 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,9 @@ jobs:
name: Active Record ${{ matrix.activerecord }} on ruby ${{ matrix.ruby }} (compat=${{ matrix.compat }})
strategy:
matrix:
activerecord: ["~> 6.0", "~> 6.1", "~> 7.0"]
activerecord: ["~> 6.1", "~> 7.0"]
compat: ["0", "1"]
ruby: [ "2.6", "2.7", "3.0", "3.1"]
exclude:
- activerecord: "~> 7.0"
ruby: "2.6"
ruby: ["3.0", "3.1"]
env:
AR: ${{ matrix.activerecord }}
COMPAT: ${{ matrix.compat }}
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
## [Unreleased]
- Update ransack to 4.1.
- Drop support for Ruby 2.6 and 2.7.
- Drop support for ActiveRecord 6.0.

## [2.0.0] - 2022-08-28

- AR 6.1: fix - FrozenError: can't modify frozen object: []
- Drop support for ActiveRecord older than 6.0.
Expand Down
6 changes: 2 additions & 4 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@ gemspec
case ENV.fetch('AR', 'latest')
when 'latest'
gem 'activerecord'
gem 'sqlite3', '~> 1.4'
when 'master'
gem 'activerecord', github: 'rails/rails'
gem 'sqlite3', '~> 1.4'
else
gem 'activerecord', ENV['AR']

gem 'sqlite3', '~> 1.4'
end

gem 'sqlite3', '~> 1.4'

case ENV.fetch('RANSACK', 'latest')
when 'latest'
gem 'ransack', require: false
Expand Down
2 changes: 1 addition & 1 deletion ISSUE_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require 'minitest/autorun'

gemfile true do
source 'https://rubygems.org'
gem 'activerecord', '~> 6.0.0' # which Active Record version?
gem 'activerecord', '~> 7.0.0' # which Active Record version?
gem 'sqlite3'
gem 'baby_squeel', github: 'rzane/baby_squeel'
end
Expand Down
4 changes: 2 additions & 2 deletions baby_squeel.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ Gem::Specification.new do |spec|

spec.files = Dir.glob('{lib/**/*,*.{md,txt,gemspec}}')

spec.add_dependency 'activerecord', '>= 6.0', '< 7.1'
spec.add_dependency 'ransack', '~> 2.3'
spec.add_dependency 'activerecord', '>= 6.1', '< 7.1'
spec.add_dependency 'ransack', '~> 4.1'

spec.add_development_dependency 'bundler', '~> 2'
spec.add_development_dependency 'rake', '~> 13.0'
Expand Down
8 changes: 2 additions & 6 deletions lib/baby_squeel.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
require 'active_record'
require 'active_record/relation'
begin
require 'polyamorous'
rescue LoadError
# Trying loading from 'ransack' as of commit c9cc20de9 (post v2.3.2)
require 'polyamorous/polyamorous'
end
# Loading polyamorous from 'ransack' as of commit c9cc20de9 (post v2.3.2)
require 'polyamorous/polyamorous'
require 'baby_squeel/version'
require 'baby_squeel/errors'
require 'baby_squeel/active_record/base'
Expand Down
36 changes: 12 additions & 24 deletions lib/baby_squeel/active_record/query_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,34 +51,22 @@ def when_having(&block)
having DSL.evaluate(self, &block)
end

if BabySqueel::ActiveRecord::VersionHelper.at_least_6_1?
def construct_join_dependency(associations, join_type)
result = super(associations, join_type)
if associations.any? { |assoc| assoc.is_a?(BabySqueel::Join) }
result.extend(BabySqueel::JoinDependency::Injector6_1)
end
result
def construct_join_dependency(associations, join_type)
result = super(associations, join_type)
if associations.any? { |assoc| assoc.is_a?(BabySqueel::Join) }
result.extend(BabySqueel::JoinDependency::Injector6_1)
end
result
end

private

# https://github.com/rails/rails/commit/c0c53ee9d28134757cf1418521cb97c4a135f140
def select_association_list(*args)
if args[0].any? { |join| join.is_a?(BabySqueel::Join) }
args[0].extend(BabySqueel::ActiveRecord::QueryMethods::Injector6_1)
end
super *args
end
else
private
private

# Active Record will call `each` on the `joins`. The
# Injector has a custom `each` method that handles
# BabySqueel::Join nodes.
def build_joins(*args)
args[1] = BabySqueel::JoinDependency::Injector6_0.new(args.second)
super(*args)
# https://github.com/rails/rails/commit/c0c53ee9d28134757cf1418521cb97c4a135f140
def select_association_list(*args)
if args[0].any? { |join| join.is_a?(BabySqueel::Join) }
args[0].extend(BabySqueel::ActiveRecord::QueryMethods::Injector6_1)
end
super *args
end
end
end
Expand Down
11 changes: 7 additions & 4 deletions lib/baby_squeel/active_record/version_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
module BabySqueel
module ActiveRecord
class VersionHelper
def self.at_least_6_1?
::ActiveRecord::VERSION::MAJOR > 6 ||
::ActiveRecord::VERSION::MAJOR == 6 && ::ActiveRecord::VERSION::MINOR >= 1
end
# Example
# BabySqueel::ActiveRecord::VersionHelper.at_least_7_1?
#
# def self.at_least_7_1?
# ::ActiveRecord::VERSION::MAJOR > 7 ||
# ::ActiveRecord::VERSION::MAJOR == 7 && ::ActiveRecord::VERSION::MINOR >= 1
# end
end
end
end
8 changes: 1 addition & 7 deletions lib/baby_squeel/association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,7 @@ def _arel(associations = [])
def build_where_clause(other)
if valid_where_clause?(other)
relation = @parent._scope.all

if BabySqueel::ActiveRecord::VersionHelper.at_least_6_1?
relation.send(:build_where_clause, { _reflection.name => other }, [])
else
factory = relation.send(:where_clause_factory)
factory.build({ _reflection.name => other }, [])
end
relation.send(:build_where_clause, { _reflection.name => other }, [])
else
raise AssociationComparisonError.new(_reflection.name, other)
end
Expand Down
23 changes: 8 additions & 15 deletions lib/baby_squeel/join_dependency.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,13 @@ def initialize(relation)
# a list (in order of chaining) of associations and finding
# the respective JoinAssociation at each level.
def find_alias(associations)
if BabySqueel::ActiveRecord::VersionHelper.at_least_6_1?
# construct_tables! got removed by rails
# https://github.com/rails/rails/commit/590b045ee2c0906ff162e6658a184afb201865d7
#
# construct_tables_for_association! is a method from the polyamorous (ransack) gem
join_root = join_dependency.send(:join_root)
join_root.each_children do |parent, child|
join_dependency.construct_tables_for_association!(parent, child)
end
else
# If we tell join_dependency to construct its tables, Active Record
# handles building the correct aliases and attaching them to its
# JoinDepenencies.
join_dependency.send(:construct_tables!, join_dependency.send(:join_root))
# construct_tables! got removed by rails
# https://github.com/rails/rails/commit/590b045ee2c0906ff162e6658a184afb201865d7
#
# construct_tables_for_association! is a method from the polyamorous (ransack) gem
join_root = join_dependency.send(:join_root)
join_root.each_children do |parent, child|
join_dependency.construct_tables_for_association!(parent, child)
end

join_association = find_join_association(associations)
Expand Down Expand Up @@ -95,7 +88,7 @@ def collect_joins(relation)
joins += relation.joins_values
joins += relation.left_outer_joins_values

buckets = joins.group_by do |join|
_buckets = joins.group_by do |join|
case join
when String
:string_join
Expand Down
15 changes: 3 additions & 12 deletions spec/integration/__snapshots__/joining_spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@
JOIN "authors" "a" ON "authors"."id" = "posts"."author_id"
"#joining when joining explicitly aliases after the on clause 1": SELECT "posts".*
FROM "posts" INNER JOIN "authors" "a" ON "authors"."id" = "posts"."author_id"
"#joining when joining explicitly merges bind values 1": SELECT "posts".* FROM "posts"
INNER JOIN "authors" ON "authors"."ugly" = 't' AND "authors"."id" = "posts"."author_id"
INNER JOIN "comments" ON "comments"."author_id" = "authors"."id"
"#joining when joining explicitly with complex conditions inner joins 1": SELECT "posts".*
FROM "posts" INNER JOIN "authors" ON ("posts"."author_id" = "authors"."id" AND "authors"."id"
!= 5 OR "authors"."name" IS NULL)
Expand Down Expand Up @@ -94,24 +91,18 @@
: SELECT "posts".* FROM "posts" INNER JOIN "authors" ON "authors"."id" = "posts"."author_id"
LEFT OUTER JOIN "comments" ON "comments"."author_id" = "authors"."id" INNER JOIN
"authors" "authors_posts" ON "authors_posts"."id" = "posts"."author_id"
"#joining when joining explicitly merges bind values 1 (Active Record: v6.0)": SELECT
"posts".* FROM "posts" INNER JOIN "authors" ON "authors"."ugly" = 1 AND "authors"."id"
= "posts"."author_id" INNER JOIN "comments" ON "comments"."author_id" = "authors"."id"
"#joining when joining implicitly polymorphism double polymorphic joining 1": SELECT
"pictures".* FROM "pictures" INNER JOIN "authors" ON "authors"."id" = "pictures"."imageable_id"
AND "pictures"."imageable_type" = 'Author' INNER JOIN "posts" ON "posts"."id" =
"pictures"."imageable_id" AND "pictures"."imageable_type" = 'Post' WHERE ("authors"."name"
= 'NameOfTheAuthor' OR "posts"."title" = 'NameOfThePost')
"#joining when joining explicitly merges bind values 1 (Active Record: v6.1)": SELECT
"posts".* FROM "posts" INNER JOIN "authors" ON "authors"."ugly" = 1 AND "authors"."id"
= "posts"."author_id" INNER JOIN "comments" ON "comments"."author_id" = "authors"."id"
"#joining when joining explicitly merges bind values 1 (Active Record: v7.0)": SELECT
"posts".* FROM "posts" INNER JOIN "authors" ON "authors"."ugly" = 1 AND "authors"."id"
= "posts"."author_id" INNER JOIN "comments" ON "comments"."author_id" = "authors"."id"
"#joining when joining implicitly inner joins 1": SELECT "posts".* FROM "posts" INNER
JOIN "authors" ON "authors"."id" = "posts"."author_id"
"#joining when joining implicitly outer joins single 1": SELECT "posts".* FROM "posts"
LEFT OUTER JOIN "authors" ON "authors"."id" = "posts"."author_id"
"#joining when joining implicitly outer joins multi 1": SELECT "posts".* FROM "posts"
LEFT OUTER JOIN "posts" "parents_posts" ON "parents_posts"."id" = "posts"."parent_id"
LEFT OUTER JOIN "authors" ON "authors"."id" = "posts"."author_id"
"#joining when joining explicitly merges bind values 1": SELECT "posts".* FROM "posts"
INNER JOIN "authors" ON "authors"."ugly" = 1 AND "authors"."id" = "posts"."author_id"
INNER JOIN "comments" ON "comments"."author_id" = "authors"."id"
2 changes: 1 addition & 1 deletion spec/integration/joining_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
it 'merges bind values' do
relation = Post.joining { ugly_author_comments }

expect(relation).to match_sql_snapshot(variants: ['6.0', '6.1', '7.0'])
expect(relation).to match_sql_snapshot
end

context 'with complex conditions' do
Expand Down

0 comments on commit c5ec16c

Please sign in to comment.