diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index dd7ea21..4bb0254 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -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 }} diff --git a/CHANGELOG.md b/CHANGELOG.md index 442985c..009a4cb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/Gemfile b/Gemfile index fc3c017..7d2ef92 100644 --- a/Gemfile +++ b/Gemfile @@ -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 diff --git a/ISSUE_TEMPLATE.md b/ISSUE_TEMPLATE.md index 1597705..f0fd5a1 100644 --- a/ISSUE_TEMPLATE.md +++ b/ISSUE_TEMPLATE.md @@ -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 diff --git a/baby_squeel.gemspec b/baby_squeel.gemspec index ab42740..fa764aa 100644 --- a/baby_squeel.gemspec +++ b/baby_squeel.gemspec @@ -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' diff --git a/lib/baby_squeel.rb b/lib/baby_squeel.rb index 045d84b..d382e93 100644 --- a/lib/baby_squeel.rb +++ b/lib/baby_squeel.rb @@ -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' diff --git a/lib/baby_squeel/active_record/query_methods.rb b/lib/baby_squeel/active_record/query_methods.rb index 7e2dcc2..0a3f87e 100644 --- a/lib/baby_squeel/active_record/query_methods.rb +++ b/lib/baby_squeel/active_record/query_methods.rb @@ -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 diff --git a/lib/baby_squeel/active_record/version_helper.rb b/lib/baby_squeel/active_record/version_helper.rb index 2be8b64..96b9f2a 100644 --- a/lib/baby_squeel/active_record/version_helper.rb +++ b/lib/baby_squeel/active_record/version_helper.rb @@ -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 diff --git a/lib/baby_squeel/association.rb b/lib/baby_squeel/association.rb index 52c8b36..79405d5 100644 --- a/lib/baby_squeel/association.rb +++ b/lib/baby_squeel/association.rb @@ -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 diff --git a/lib/baby_squeel/join_dependency.rb b/lib/baby_squeel/join_dependency.rb index bdde798..00a303c 100644 --- a/lib/baby_squeel/join_dependency.rb +++ b/lib/baby_squeel/join_dependency.rb @@ -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) @@ -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 diff --git a/spec/integration/__snapshots__/joining_spec.yaml b/spec/integration/__snapshots__/joining_spec.yaml index 628feb9..67219a4 100644 --- a/spec/integration/__snapshots__/joining_spec.yaml +++ b/spec/integration/__snapshots__/joining_spec.yaml @@ -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) @@ -94,20 +91,11 @@ : 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" @@ -115,3 +103,6 @@ "#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" diff --git a/spec/integration/joining_spec.rb b/spec/integration/joining_spec.rb index 02f7ecf..f1c323a 100644 --- a/spec/integration/joining_spec.rb +++ b/spec/integration/joining_spec.rb @@ -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