From 99a67085f06154049bd840580d0b86ecfdff5d28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Rodr=C3=ADguez?= Date: Mon, 14 Oct 2019 15:24:50 +0200 Subject: [PATCH 1/7] Drop Rails 5.0 and Rails 5.1 support Since they have reached their End of Life. --- .travis.yml | 4 - CHANGELOG.md | 3 + README.md | 3 +- lib/ransack/adapters/active_record/context.rb | 12 +- lib/ransack/constants.rb | 1 - .../join_association.rb | 2 - .../join_dependency.rb | 2 - .../join_association.rb | 31 ----- .../join_dependency.rb | 112 ------------------ polyamorous/polyamorous.gemspec | 2 +- ransack.gemspec | 6 +- spec/ransack/search_spec.rb | 9 +- 12 files changed, 11 insertions(+), 176 deletions(-) delete mode 100644 polyamorous/lib/polyamorous/activerecord_5.0_ruby_2/join_association.rb delete mode 100644 polyamorous/lib/polyamorous/activerecord_5.0_ruby_2/join_dependency.rb delete mode 100644 polyamorous/lib/polyamorous/activerecord_5.1_ruby_2/join_association.rb delete mode 100644 polyamorous/lib/polyamorous/activerecord_5.1_ruby_2/join_dependency.rb diff --git a/.travis.yml b/.travis.yml index d37adcd15..60749cbe4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -27,10 +27,6 @@ env: - RAILS=v5.2.0 DB=mysql - RAILS=v5.2.0 DB=postgres - - RAILS=5-1-stable DB=sqlite3 - - RAILS=5-1-stable DB=mysql - - RAILS=5-1-stable DB=postgres - matrix: allow_failures: - env: RAILS=5-2-stable DB=sqlite3 diff --git a/CHANGELOG.md b/CHANGELOG.md index 95c0af8c9..51328836c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## Unreleased +* Drop support for Active Record < 5.2. + PR [#1073](https://github.com/activerecord-hackery/ransack/pull/1073) + ## 2.3.0 - 2019-08-18 * Arabic translations PR [979](https://github.com/activerecord-hackery/ransack/pull/979) diff --git a/README.md b/README.md index 61b91c0d6..a850a5d6b 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,8 @@ you're reading the documentation for the master branch with the latest features. ## Getting started -Ransack is compatible with Rails 6.0, 5.0, 5.1 and 5.2 on Ruby 2.2 and later. +Ransack is compatible with Rails 6.0, and 5.2 on Ruby 2.2 and later. +If you are using Rails 5.0 or Rails 5.1 use the 2.3 line of Ransack. If you are using Rails <5.0 use the 1.8 line of Ransack. If you are using Ruby 1.8 or an earlier JRuby and run into compatibility issues, you can use an earlier version of Ransack, say, up to 1.3.0. diff --git a/lib/ransack/adapters/active_record/context.rb b/lib/ransack/adapters/active_record/context.rb index 791459ceb..410c7ef17 100644 --- a/lib/ransack/adapters/active_record/context.rb +++ b/lib/ransack/adapters/active_record/context.rb @@ -367,17 +367,7 @@ def build_association(name, parent = @base, klass = nil) def extract_joins(association) parent = @join_dependency.instance_variable_get(:@join_root) reflection = association.reflection - join_constraints = if ::ActiveRecord::VERSION::STRING < Constants::RAILS_5_1 - association.join_constraints( - parent.table, - parent.base_klass, - association, - Arel::Nodes::OuterJoin, - association.tables, - reflection.scope_chain, - reflection.chain - ) - elsif ::ActiveRecord::VERSION::STRING <= Constants::RAILS_5_2_0 + join_constraints = if ::ActiveRecord::VERSION::STRING <= Constants::RAILS_5_2_0 association.join_constraints( parent.table, parent.base_klass, diff --git a/lib/ransack/constants.rb b/lib/ransack/constants.rb index 6fbafabd4..9d15e930f 100644 --- a/lib/ransack/constants.rb +++ b/lib/ransack/constants.rb @@ -45,7 +45,6 @@ module Constants NOT_EQ_ALL = 'not_eq_all'.freeze CONT = 'cont'.freeze - RAILS_5_1 = '5.1'.freeze RAILS_5_2 = '5.2'.freeze RAILS_5_2_0 = '5.2.0'.freeze RAILS_6_0 = '6.0.0'.freeze diff --git a/polyamorous/lib/polyamorous/activerecord_5.0_ruby_2/join_association.rb b/polyamorous/lib/polyamorous/activerecord_5.0_ruby_2/join_association.rb deleted file mode 100644 index 794f2da92..000000000 --- a/polyamorous/lib/polyamorous/activerecord_5.0_ruby_2/join_association.rb +++ /dev/null @@ -1,2 +0,0 @@ -# active_record_5.0_ruby_2/join_association.rb -require 'polyamorous/activerecord_5.1_ruby_2/join_association' diff --git a/polyamorous/lib/polyamorous/activerecord_5.0_ruby_2/join_dependency.rb b/polyamorous/lib/polyamorous/activerecord_5.0_ruby_2/join_dependency.rb deleted file mode 100644 index 03423b268..000000000 --- a/polyamorous/lib/polyamorous/activerecord_5.0_ruby_2/join_dependency.rb +++ /dev/null @@ -1,2 +0,0 @@ -# active_record_5.0_ruby_2/join_dependency.rb -require 'polyamorous/activerecord_5.1_ruby_2/join_dependency' diff --git a/polyamorous/lib/polyamorous/activerecord_5.1_ruby_2/join_association.rb b/polyamorous/lib/polyamorous/activerecord_5.1_ruby_2/join_association.rb deleted file mode 100644 index 7e9c24e94..000000000 --- a/polyamorous/lib/polyamorous/activerecord_5.1_ruby_2/join_association.rb +++ /dev/null @@ -1,31 +0,0 @@ -# active_record_5.1_ruby_2/join_association.rb - -module Polyamorous - module JoinAssociationExtensions - include SwappingReflectionClass - def self.prepended(base) - base.class_eval { attr_reader :join_type } - end - - def initialize(reflection, children, polymorphic_class = nil, join_type = Arel::Nodes::InnerJoin) - @join_type = join_type - if polymorphic_class && ::ActiveRecord::Base > polymorphic_class - swapping_reflection_klass(reflection, polymorphic_class) do |reflection| - super(reflection, children) - self.reflection.options[:polymorphic] = true - end - else - super(reflection, children) - end - end - - def build_constraint(klass, table, key, foreign_table, foreign_key) - if reflection.polymorphic? - super(klass, table, key, foreign_table, foreign_key) - .and(foreign_table[reflection.foreign_type].eq(reflection.klass.name)) - else - super(klass, table, key, foreign_table, foreign_key) - end - end - end -end diff --git a/polyamorous/lib/polyamorous/activerecord_5.1_ruby_2/join_dependency.rb b/polyamorous/lib/polyamorous/activerecord_5.1_ruby_2/join_dependency.rb deleted file mode 100644 index b1b63af3a..000000000 --- a/polyamorous/lib/polyamorous/activerecord_5.1_ruby_2/join_dependency.rb +++ /dev/null @@ -1,112 +0,0 @@ -# active_record_5.1_ruby_2/join_dependency.rb - -module Polyamorous - module JoinDependencyExtensions - # Replaces ActiveRecord::Associations::JoinDependency#build - # - def build(associations, base_klass) - associations.map do |name, right| - if name.is_a? Join - reflection = find_reflection base_klass, name.name - reflection.check_validity! - reflection.check_eager_loadable! - - klass = if reflection.polymorphic? - name.klass || base_klass - else - reflection.klass - end - JoinAssociation.new(reflection, build(right, klass), name.klass, name.type) - else - reflection = find_reflection base_klass, name - reflection.check_validity! - reflection.check_eager_loadable! - - if reflection.polymorphic? - raise ActiveRecord::EagerLoadPolymorphicError.new(reflection) - end - JoinAssociation.new reflection, build(right, reflection.klass) - end - end - end - - # Replaces ActiveRecord::Associations::JoinDependency#join_constraints - # - # This internal method was changed in Rails 5.0 by commit - # https://github.com/rails/rails/commit/e038975 which added - # left_outer_joins (see #make_polyamorous_left_outer_joins below) and added - # passing an additional argument, `join_type`, to #join_constraints. - # - def join_constraints(outer_joins, join_type) - joins = join_root.children.flat_map { |child| - if join_type == Arel::Nodes::OuterJoin - make_polyamorous_left_outer_joins join_root, child - else - make_polyamorous_inner_joins join_root, child - end - } - - joins.concat outer_joins.flat_map { |oj| - if join_root.match? oj.join_root - walk(join_root, oj.join_root) - else - oj.join_root.children.flat_map { |child| - make_outer_joins(oj.join_root, child) - } - end - } - end - - # Replaces ActiveRecord::Associations::JoinDependency#make_left_outer_joins, - # a new method that was added in Rails 5.0 with the following commit: - # https://github.com/rails/rails/commit/e038975 - # - def make_polyamorous_left_outer_joins(parent, child) - tables = child.tables - join_type = Arel::Nodes::OuterJoin - info = make_constraints parent, child, tables, join_type - - [info] + child.children.flat_map { |c| - make_polyamorous_left_outer_joins(child, c) - } - end - - # Replaces ActiveRecord::Associations::JoinDependency#make_inner_joins - # - def make_polyamorous_inner_joins(parent, child) - tables = child.tables - join_type = child.join_type || Arel::Nodes::InnerJoin - info = make_constraints parent, child, tables, join_type - - [info] + child.children.flat_map { |c| - make_polyamorous_inner_joins(child, c) - } - end - - private :make_polyamorous_inner_joins, :make_polyamorous_left_outer_joins - - module ClassMethods - # Prepended before ActiveRecord::Associations::JoinDependency#walk_tree - # - def walk_tree(associations, hash) - case associations - when TreeNode - associations.add_to_tree(hash) - when Hash - associations.each do |k, v| - cache = - if TreeNode === k - k.add_to_tree(hash) - else - hash[k] ||= {} - end - walk_tree(v, cache) - end - else - super(associations, hash) - end - end - end - - end -end diff --git a/polyamorous/polyamorous.gemspec b/polyamorous/polyamorous.gemspec index 8a76124c1..1e4e707c6 100644 --- a/polyamorous/polyamorous.gemspec +++ b/polyamorous/polyamorous.gemspec @@ -18,7 +18,7 @@ Gem::Specification.new do |s| associations. } - s.add_dependency 'activerecord', '>= 5.0' + s.add_dependency 'activerecord', '>= 5.2' s.add_development_dependency 'rspec', '~> 3' s.add_development_dependency 'machinist', '~> 1.0.6' s.add_development_dependency 'faker', '~> 1.6.5' diff --git a/ransack.gemspec b/ransack.gemspec index 269cf3f0c..3bc1b2453 100644 --- a/ransack.gemspec +++ b/ransack.gemspec @@ -14,9 +14,9 @@ Gem::Specification.new do |s| s.required_ruby_version = '>= 1.9' s.license = 'MIT' - s.add_dependency 'actionpack', '>= 5.0' - s.add_dependency 'activerecord', '>= 5.0' - s.add_dependency 'activesupport', '>= 5.0' + s.add_dependency 'actionpack', '>= 5.2' + s.add_dependency 'activerecord', '>= 5.2' + s.add_dependency 'activesupport', '>= 5.2' s.add_dependency 'i18n' s.add_dependency 'polyamorous', Ransack::VERSION.to_s diff --git a/spec/ransack/search_spec.rb b/spec/ransack/search_spec.rb index c19a6f158..e294a8f8f 100644 --- a/spec/ransack/search_spec.rb +++ b/spec/ransack/search_spec.rb @@ -228,7 +228,6 @@ module Ransack end it 'use appropriate table alias' do - skip "Make this spec pass for Rails <5.2" if ::ActiveRecord::VERSION::STRING < '5.2.0' s = Search.new(Person, { name_eq: "person_name_query", articles_title_eq: "person_article_title_query", @@ -251,13 +250,7 @@ module Ransack .to include "articles_people.title = 'parents_article_title_query'" end - # FIXME: Make this spec pass for Rails 4.1 / 4.2 / 5.0 and not just 4.0 by - # commenting out lines 221 and 242 to run the test. Addresses issue #374. - # https://github.com/activerecord-hackery/ransack/issues/374 - # - it 'evaluates conditions for multiple `belongs_to` associations to the - same table contextually' do - skip "Make this spec pass for Rails <5.2" if ::ActiveRecord::VERSION::STRING < '5.2.0' + it 'evaluates conditions for multiple `belongs_to` associations to the same table contextually' do s = Search.new( Recommendation, person_name_eq: 'Ernie', From ba77c8c7e5d02989b9fef35d7c2aea9905f24731 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Rodr=C3=ADguez?= Date: Mon, 14 Oct 2019 15:27:41 +0200 Subject: [PATCH 2/7] Remove polyamorous dev dependencies They should be the same as ransack's. --- polyamorous/polyamorous.gemspec | 4 ---- 1 file changed, 4 deletions(-) diff --git a/polyamorous/polyamorous.gemspec b/polyamorous/polyamorous.gemspec index 1e4e707c6..73dd6b1d4 100644 --- a/polyamorous/polyamorous.gemspec +++ b/polyamorous/polyamorous.gemspec @@ -19,10 +19,6 @@ Gem::Specification.new do |s| } s.add_dependency 'activerecord', '>= 5.2' - s.add_development_dependency 'rspec', '~> 3' - s.add_development_dependency 'machinist', '~> 1.0.6' - s.add_development_dependency 'faker', '~> 1.6.5' - s.add_development_dependency 'sqlite3', '~> 1.3.3' s.files = `git ls-files`.split("\n") s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") From 70136de391ca366d1888df1777228f0ec7877d30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Rodr=C3=ADguez?= Date: Mon, 14 Oct 2019 15:28:17 +0200 Subject: [PATCH 3/7] Remove unnecessary commented out code --- polyamorous/polyamorous.gemspec | 4 ---- 1 file changed, 4 deletions(-) diff --git a/polyamorous/polyamorous.gemspec b/polyamorous/polyamorous.gemspec index 73dd6b1d4..ead81360d 100644 --- a/polyamorous/polyamorous.gemspec +++ b/polyamorous/polyamorous.gemspec @@ -24,8 +24,4 @@ Gem::Specification.new do |s| s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } s.require_paths = ["lib"] - - # specify any dependencies here; for example: - # s.add_development_dependency "rspec" - # s.add_runtime_dependency "rest-client" end From b135704639b8a2999a4e103cb53127d9459411e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Rodr=C3=ADguez?= Date: Wed, 16 Oct 2019 11:22:39 +0200 Subject: [PATCH 4/7] Remove more leftovers --- lib/ransack/adapters/active_record/context.rb | 22 +------------------ .../adapters/active_record/ransack/context.rb | 7 +----- lib/ransack/helpers/form_builder.rb | 16 +++++--------- polyamorous/lib/polyamorous.rb | 8 +++---- 4 files changed, 10 insertions(+), 43 deletions(-) diff --git a/lib/ransack/adapters/active_record/context.rb b/lib/ransack/adapters/active_record/context.rb index 410c7ef17..ea0401a44 100644 --- a/lib/ransack/adapters/active_record/context.rb +++ b/lib/ransack/adapters/active_record/context.rb @@ -6,13 +6,6 @@ module Adapters module ActiveRecord class Context < ::Ransack::Context - def initialize(object, options = {}) - super - if ::ActiveRecord::VERSION::STRING < Constants::RAILS_5_2 - @arel_visitor = @engine.connection.visitor - end - end - def relation_for(object) object.all end @@ -123,7 +116,6 @@ def join_sources @join_dependency.join_constraints(@object.joins_values, @join_type) ] end - joins = joins.collect(&:joins).flatten if ::ActiveRecord::VERSION::STRING < Constants::RAILS_5_2 joins.each do |aliased_join| base.from(aliased_join) end @@ -268,12 +260,7 @@ def build_joins(relation) join_list = join_nodes + convert_join_strings_to_ast(relation.table, string_joins) - if ::ActiveRecord::VERSION::STRING < Constants::RAILS_5_2_0 - join_dependency = Polyamorous::JoinDependency.new(relation.klass, association_joins, join_list) - join_nodes.each do |join| - join_dependency.send(:alias_tracker).aliases[join.left.name.downcase] = 1 - end - elsif ::ActiveRecord::VERSION::STRING == Constants::RAILS_5_2_0 + if ::ActiveRecord::VERSION::STRING == Constants::RAILS_5_2_0 alias_tracker = ::ActiveRecord::Associations::AliasTracker.create(self.klass.connection, relation.table.name, join_list) join_dependency = Polyamorous::JoinDependency.new(relation.klass, relation.table, association_joins, alias_tracker) join_nodes.each do |join| @@ -321,13 +308,6 @@ def build_association(name, parent = @base, klass = nil) @join_type ) found_association = jd.instance_variable_get(:@join_root).children.last - elsif ::Gem::Version.new(::ActiveRecord::VERSION::STRING) < ::Gem::Version.new(Constants::RAILS_5_2_0) - jd = Polyamorous::JoinDependency.new( - parent.base_klass, - Polyamorous::Join.new(name, @join_type, klass), - [] - ) - found_association = jd.join_root.children.last elsif ::ActiveRecord::VERSION::STRING == Constants::RAILS_5_2_0 alias_tracker = ::ActiveRecord::Associations::AliasTracker.create(self.klass.connection, parent.table.name, []) jd = Polyamorous::JoinDependency.new( diff --git a/lib/ransack/adapters/active_record/ransack/context.rb b/lib/ransack/adapters/active_record/ransack/context.rb index 353da953b..73ad0f16e 100644 --- a/lib/ransack/adapters/active_record/ransack/context.rb +++ b/lib/ransack/adapters/active_record/ransack/context.rb @@ -30,12 +30,7 @@ def initialize(object, options = {}) @associations_pot = {} @lock_associations = [] - if ::ActiveRecord::VERSION::STRING >= Constants::RAILS_5_2 - @base = @join_dependency.instance_variable_get(:@join_root) - else - @base = @join_dependency.join_root - @engine = @base.base_klass.arel_engine - end + @base = @join_dependency.instance_variable_get(:@join_root) end def bind_pair_for(key) diff --git a/lib/ransack/helpers/form_builder.rb b/lib/ransack/helpers/form_builder.rb index 1726bbbdc..2ee3c25f7 100644 --- a/lib/ransack/helpers/form_builder.rb +++ b/lib/ransack/helpers/form_builder.rb @@ -7,17 +7,11 @@ module ActionView::Helpers::Tags class Base private if defined? ::ActiveRecord - if ::ActiveRecord::VERSION::STRING < '5.2' - def value(object) - object.send @method_name if object # use send instead of public_send - end - else # rails/rails#29791 - def value - if @allow_method_names_outside_object - object.send @method_name if object && object.respond_to?(@method_name, true) - else - object.send @method_name if object - end + def value + if @allow_method_names_outside_object + object.send @method_name if object && object.respond_to?(@method_name, true) + else + object.send @method_name if object end end end diff --git a/polyamorous/lib/polyamorous.rb b/polyamorous/lib/polyamorous.rb index 05bca1037..c9598a311 100644 --- a/polyamorous/lib/polyamorous.rb +++ b/polyamorous/lib/polyamorous.rb @@ -12,16 +12,14 @@ module Polyamorous require 'polyamorous/swapping_reflection_class' ar_version = ::ActiveRecord::VERSION::STRING[0,3] - ar_version = ::ActiveRecord::VERSION::STRING[0,5] if ar_version >= "5.2" && ::ActiveRecord.version < ::Gem::Version.new("6.0") + ar_version = ::ActiveRecord::VERSION::STRING[0,5] if ::ActiveRecord.version < ::Gem::Version.new("6.0") ar_version = "5.2.1" if ::ActiveRecord::VERSION::STRING >= "5.2.1" && ::ActiveRecord.version < ::Gem::Version.new("6.0") %w(join_association join_dependency).each do |file| require "polyamorous/activerecord_#{ar_version}_ruby_2/#{file}" end - if ar_version >= "5.2.0" - require "polyamorous/activerecord_#{ar_version}_ruby_2/reflection.rb" - ::ActiveRecord::Reflection::AbstractReflection.send(:prepend, Polyamorous::ReflectionExtensions) - end + require "polyamorous/activerecord_#{ar_version}_ruby_2/reflection.rb" + ::ActiveRecord::Reflection::AbstractReflection.send(:prepend, Polyamorous::ReflectionExtensions) Polyamorous::JoinDependency.send(:prepend, Polyamorous::JoinDependencyExtensions) Polyamorous::JoinDependency.singleton_class.send(:prepend, Polyamorous::JoinDependencyExtensions::ClassMethods) From 4538c5d9058efafb6e63f8aafe508406415ad0a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Rodr=C3=ADguez?= Date: Wed, 16 Oct 2019 11:31:04 +0200 Subject: [PATCH 5/7] Drop Rails 5.2.0 support as well Instead, require a minimum of Rails 5.2.1. --- .travis.yml | 4 - CHANGELOG.md | 2 +- lib/ransack/adapters/active_record/context.rb | 59 ++------- lib/ransack/constants.rb | 2 - polyamorous/lib/polyamorous.rb | 7 +- .../join_association.rb | 31 ----- .../join_dependency.rb | 112 ------------------ .../activerecord_5.2.1_ruby_2/reflection.rb | 2 - .../join_association.rb | 2 - .../join_dependency.rb | 2 - .../reflection.rb | 0 .../join_association.rb | 2 +- .../activerecord_6.0_ruby_2/reflection.rb | 2 +- polyamorous/polyamorous.gemspec | 2 +- ransack.gemspec | 6 +- spec/helpers/polyamorous_helper.rb | 11 +- 16 files changed, 21 insertions(+), 225 deletions(-) delete mode 100644 polyamorous/lib/polyamorous/activerecord_5.2.0_ruby_2/join_association.rb delete mode 100644 polyamorous/lib/polyamorous/activerecord_5.2.0_ruby_2/join_dependency.rb delete mode 100644 polyamorous/lib/polyamorous/activerecord_5.2.1_ruby_2/reflection.rb rename polyamorous/lib/polyamorous/{activerecord_5.2.1_ruby_2 => activerecord_5.2_ruby_2}/join_association.rb (92%) rename polyamorous/lib/polyamorous/{activerecord_5.2.1_ruby_2 => activerecord_5.2_ruby_2}/join_dependency.rb (98%) rename polyamorous/lib/polyamorous/{activerecord_5.2.0_ruby_2 => activerecord_5.2_ruby_2}/reflection.rb (100%) diff --git a/.travis.yml b/.travis.yml index 60749cbe4..49eaf3e0c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -23,10 +23,6 @@ env: - RAILS=v5.2.1 DB=mysql - RAILS=v5.2.1 DB=postgres - - RAILS=v5.2.0 DB=sqlite3 - - RAILS=v5.2.0 DB=mysql - - RAILS=v5.2.0 DB=postgres - matrix: allow_failures: - env: RAILS=5-2-stable DB=sqlite3 diff --git a/CHANGELOG.md b/CHANGELOG.md index 51328836c..d88575e78 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ## Unreleased -* Drop support for Active Record < 5.2. +* Drop support for Active Record 5.0, 5.1, and 5.2.0. PR [#1073](https://github.com/activerecord-hackery/ransack/pull/1073) ## 2.3.0 - 2019-08-18 diff --git a/lib/ransack/adapters/active_record/context.rb b/lib/ransack/adapters/active_record/context.rb index ea0401a44..9ea20ab90 100644 --- a/lib/ransack/adapters/active_record/context.rb +++ b/lib/ransack/adapters/active_record/context.rb @@ -97,8 +97,7 @@ def klassify(obj) # JoinDependency to track table aliases. # def join_sources - base, joins = - if ::ActiveRecord::VERSION::STRING > Constants::RAILS_5_2_0 + base, joins = begin alias_tracker = ::ActiveRecord::Associations::AliasTracker.create(self.klass.connection, @object.table.name, []) constraints = if ::Gem::Version.new(::ActiveRecord::VERSION::STRING) >= ::Gem::Version.new(Constants::RAILS_6_0) @join_dependency.join_constraints(@object.joins_values, alias_tracker) @@ -110,11 +109,6 @@ def join_sources Arel::SelectManager.new(@object.table), constraints ] - else - [ - Arel::SelectManager.new(@object.table), - @join_dependency.join_constraints(@object.joins_values, @join_type) - ] end joins.each do |aliased_join| base.from(aliased_join) @@ -260,23 +254,15 @@ def build_joins(relation) join_list = join_nodes + convert_join_strings_to_ast(relation.table, string_joins) - if ::ActiveRecord::VERSION::STRING == Constants::RAILS_5_2_0 - alias_tracker = ::ActiveRecord::Associations::AliasTracker.create(self.klass.connection, relation.table.name, join_list) - join_dependency = Polyamorous::JoinDependency.new(relation.klass, relation.table, association_joins, alias_tracker) - join_nodes.each do |join| - join_dependency.send(:alias_tracker).aliases[join.left.name.downcase] = 1 - end + alias_tracker = ::ActiveRecord::Associations::AliasTracker.create(self.klass.connection, relation.table.name, join_list) + join_dependency = if ::Gem::Version.new(::ActiveRecord::VERSION::STRING) >= ::Gem::Version.new(Constants::RAILS_6_0) + Polyamorous::JoinDependency.new(relation.klass, relation.table, association_joins, Arel::Nodes::OuterJoin) else - alias_tracker = ::ActiveRecord::Associations::AliasTracker.create(self.klass.connection, relation.table.name, join_list) - join_dependency = if ::Gem::Version.new(::ActiveRecord::VERSION::STRING) >= ::Gem::Version.new(Constants::RAILS_6_0) - Polyamorous::JoinDependency.new(relation.klass, relation.table, association_joins, Arel::Nodes::OuterJoin) - else - Polyamorous::JoinDependency.new(relation.klass, relation.table, association_joins) - end - join_dependency.instance_variable_set(:@alias_tracker, alias_tracker) - join_nodes.each do |join| - join_dependency.send(:alias_tracker).aliases[join.left.name.downcase] = 1 - end + Polyamorous::JoinDependency.new(relation.klass, relation.table, association_joins) + end + join_dependency.instance_variable_set(:@alias_tracker, alias_tracker) + join_nodes.each do |join| + join_dependency.send(:alias_tracker).aliases[join.left.name.downcase] = 1 end join_dependency end @@ -308,15 +294,6 @@ def build_association(name, parent = @base, klass = nil) @join_type ) found_association = jd.instance_variable_get(:@join_root).children.last - elsif ::ActiveRecord::VERSION::STRING == Constants::RAILS_5_2_0 - alias_tracker = ::ActiveRecord::Associations::AliasTracker.create(self.klass.connection, parent.table.name, []) - jd = Polyamorous::JoinDependency.new( - parent.base_klass, - parent.table, - Polyamorous::Join.new(name, @join_type, klass), - alias_tracker - ) - found_association = jd.instance_variable_get(:@join_root).children.last else jd = Polyamorous::JoinDependency.new( parent.base_klass, @@ -333,11 +310,7 @@ def build_association(name, parent = @base, klass = nil) @join_dependency.instance_variable_get(:@join_root).children.push found_association # Builds the arel nodes properly for this association - if ::ActiveRecord::VERSION::STRING > Constants::RAILS_5_2_0 - @join_dependency.send(:construct_tables!, jd.instance_variable_get(:@join_root)) - else - @join_dependency.send(:construct_tables!, jd.instance_variable_get(:@join_root), found_association) - end + @join_dependency.send(:construct_tables!, jd.instance_variable_get(:@join_root)) # Leverage the stashed association functionality in AR @object = @object.joins(jd) @@ -347,22 +320,12 @@ def build_association(name, parent = @base, klass = nil) def extract_joins(association) parent = @join_dependency.instance_variable_get(:@join_root) reflection = association.reflection - join_constraints = if ::ActiveRecord::VERSION::STRING <= Constants::RAILS_5_2_0 - association.join_constraints( - parent.table, - parent.base_klass, - Arel::Nodes::OuterJoin, - association.tables, - reflection.chain - ) - else - association.join_constraints( + join_constraints = association.join_constraints( parent.table, parent.base_klass, Arel::Nodes::OuterJoin, @join_dependency.instance_variable_get(:@alias_tracker) ) - end join_constraints.to_a.flatten end end diff --git a/lib/ransack/constants.rb b/lib/ransack/constants.rb index 9d15e930f..48897d9df 100644 --- a/lib/ransack/constants.rb +++ b/lib/ransack/constants.rb @@ -45,8 +45,6 @@ module Constants NOT_EQ_ALL = 'not_eq_all'.freeze CONT = 'cont'.freeze - RAILS_5_2 = '5.2'.freeze - RAILS_5_2_0 = '5.2.0'.freeze RAILS_6_0 = '6.0.0'.freeze RANSACK_SLASH_SEARCHES = 'ransack/searches'.freeze diff --git a/polyamorous/lib/polyamorous.rb b/polyamorous/lib/polyamorous.rb index c9598a311..26f3322c2 100644 --- a/polyamorous/lib/polyamorous.rb +++ b/polyamorous/lib/polyamorous.rb @@ -12,14 +12,11 @@ module Polyamorous require 'polyamorous/swapping_reflection_class' ar_version = ::ActiveRecord::VERSION::STRING[0,3] - ar_version = ::ActiveRecord::VERSION::STRING[0,5] if ::ActiveRecord.version < ::Gem::Version.new("6.0") - ar_version = "5.2.1" if ::ActiveRecord::VERSION::STRING >= "5.2.1" && ::ActiveRecord.version < ::Gem::Version.new("6.0") - %w(join_association join_dependency).each do |file| + %w(join_association join_dependency reflection).each do |file| require "polyamorous/activerecord_#{ar_version}_ruby_2/#{file}" end - require "polyamorous/activerecord_#{ar_version}_ruby_2/reflection.rb" - ::ActiveRecord::Reflection::AbstractReflection.send(:prepend, Polyamorous::ReflectionExtensions) + ActiveRecord::Reflection::AbstractReflection.send(:prepend, Polyamorous::ReflectionExtensions) Polyamorous::JoinDependency.send(:prepend, Polyamorous::JoinDependencyExtensions) Polyamorous::JoinDependency.singleton_class.send(:prepend, Polyamorous::JoinDependencyExtensions::ClassMethods) diff --git a/polyamorous/lib/polyamorous/activerecord_5.2.0_ruby_2/join_association.rb b/polyamorous/lib/polyamorous/activerecord_5.2.0_ruby_2/join_association.rb deleted file mode 100644 index 20cf2a4d9..000000000 --- a/polyamorous/lib/polyamorous/activerecord_5.2.0_ruby_2/join_association.rb +++ /dev/null @@ -1,31 +0,0 @@ -# active_record_5.2_ruby_2/join_association.rb - -module Polyamorous - module JoinAssociationExtensions - include SwappingReflectionClass - def self.prepended(base) - base.class_eval { attr_reader :join_type } - end - - def initialize(reflection, children, alias_tracker, polymorphic_class = nil, join_type = Arel::Nodes::InnerJoin) - @join_type = join_type - if polymorphic_class && ::ActiveRecord::Base > polymorphic_class - swapping_reflection_klass(reflection, polymorphic_class) do |reflection| - super(reflection, children, alias_tracker) - self.reflection.options[:polymorphic] = true - end - else - super(reflection, children, alias_tracker) - end - end - - def build_constraint(klass, table, key, foreign_table, foreign_key) - if reflection.polymorphic? - super(klass, table, key, foreign_table, foreign_key) - .and(foreign_table[reflection.foreign_type].eq(reflection.klass.name)) - else - super(klass, table, key, foreign_table, foreign_key) - end - end - end -end diff --git a/polyamorous/lib/polyamorous/activerecord_5.2.0_ruby_2/join_dependency.rb b/polyamorous/lib/polyamorous/activerecord_5.2.0_ruby_2/join_dependency.rb deleted file mode 100644 index 1e76107c2..000000000 --- a/polyamorous/lib/polyamorous/activerecord_5.2.0_ruby_2/join_dependency.rb +++ /dev/null @@ -1,112 +0,0 @@ -# active_record_5.2_ruby_2/join_dependency.rb - -module Polyamorous - module JoinDependencyExtensions - # Replaces ActiveRecord::Associations::JoinDependency#build - # - def build(associations, base_klass) - associations.map do |name, right| - if name.is_a? Join - reflection = find_reflection base_klass, name.name - reflection.check_validity! - reflection.check_eager_loadable! - - klass = if reflection.polymorphic? - name.klass || base_klass - else - reflection.klass - end - JoinAssociation.new(reflection, build(right, klass), alias_tracker, name.klass, name.type) - else - reflection = find_reflection base_klass, name - reflection.check_validity! - reflection.check_eager_loadable! - - if reflection.polymorphic? - raise ActiveRecord::EagerLoadPolymorphicError.new(reflection) - end - JoinAssociation.new(reflection, build(right, reflection.klass), alias_tracker) - end - end - end - - # Replaces ActiveRecord::Associations::JoinDependency#join_constraints - # - # This internal method was changed in Rails 5.0 by commit - # https://github.com/rails/rails/commit/e038975 which added - # left_outer_joins (see #make_polyamorous_left_outer_joins below) and added - # passing an additional argument, `join_type`, to #join_constraints. - # - def join_constraints(outer_joins, join_type) - joins = join_root.children.flat_map { |child| - if join_type == Arel::Nodes::OuterJoin - make_polyamorous_left_outer_joins join_root, child - else - make_polyamorous_inner_joins join_root, child - end - } - - joins.concat outer_joins.flat_map { |oj| - if join_root.match?(oj.join_root) && join_root.table.name == oj.join_root.table.name - walk(join_root, oj.join_root) - else - oj.join_root.children.flat_map { |child| - make_outer_joins(oj.join_root, child) - } - end - } - end - - # Replaces ActiveRecord::Associations::JoinDependency#make_left_outer_joins, - # a new method that was added in Rails 5.0 with the following commit: - # https://github.com/rails/rails/commit/e038975 - # - def make_polyamorous_left_outer_joins(parent, child) - tables = child.tables - join_type = Arel::Nodes::OuterJoin - info = make_constraints parent, child, tables, join_type - - info + child.children.flat_map { |c| - make_polyamorous_left_outer_joins(child, c) - } - end - - # Replaces ActiveRecord::Associations::JoinDependency#make_inner_joins - # - def make_polyamorous_inner_joins(parent, child) - tables = child.tables - join_type = child.join_type || Arel::Nodes::InnerJoin - info = make_constraints parent, child, tables, join_type - - info + child.children.flat_map { |c| - make_polyamorous_inner_joins(child, c) - } - end - - private :make_polyamorous_inner_joins, :make_polyamorous_left_outer_joins - - module ClassMethods - # Prepended before ActiveRecord::Associations::JoinDependency#walk_tree - # - def walk_tree(associations, hash) - case associations - when TreeNode - associations.add_to_tree(hash) - when Hash - associations.each do |k, v| - cache = - if TreeNode === k - k.add_to_tree(hash) - else - hash[k] ||= {} - end - walk_tree(v, cache) - end - else - super(associations, hash) - end - end - end - - end -end diff --git a/polyamorous/lib/polyamorous/activerecord_5.2.1_ruby_2/reflection.rb b/polyamorous/lib/polyamorous/activerecord_5.2.1_ruby_2/reflection.rb deleted file mode 100644 index e66bf9090..000000000 --- a/polyamorous/lib/polyamorous/activerecord_5.2.1_ruby_2/reflection.rb +++ /dev/null @@ -1,2 +0,0 @@ -# active_record_5.2.1_ruby_2/reflection.rb -require 'polyamorous/activerecord_5.2.0_ruby_2/reflection' diff --git a/polyamorous/lib/polyamorous/activerecord_5.2.1_ruby_2/join_association.rb b/polyamorous/lib/polyamorous/activerecord_5.2_ruby_2/join_association.rb similarity index 92% rename from polyamorous/lib/polyamorous/activerecord_5.2.1_ruby_2/join_association.rb rename to polyamorous/lib/polyamorous/activerecord_5.2_ruby_2/join_association.rb index 8b3976033..fd836fa14 100644 --- a/polyamorous/lib/polyamorous/activerecord_5.2.1_ruby_2/join_association.rb +++ b/polyamorous/lib/polyamorous/activerecord_5.2_ruby_2/join_association.rb @@ -1,5 +1,3 @@ -# active_record_5.2.1_ruby_2/join_association.rb - module Polyamorous module JoinAssociationExtensions include SwappingReflectionClass diff --git a/polyamorous/lib/polyamorous/activerecord_5.2.1_ruby_2/join_dependency.rb b/polyamorous/lib/polyamorous/activerecord_5.2_ruby_2/join_dependency.rb similarity index 98% rename from polyamorous/lib/polyamorous/activerecord_5.2.1_ruby_2/join_dependency.rb rename to polyamorous/lib/polyamorous/activerecord_5.2_ruby_2/join_dependency.rb index ca7de5650..58b9f6744 100644 --- a/polyamorous/lib/polyamorous/activerecord_5.2.1_ruby_2/join_dependency.rb +++ b/polyamorous/lib/polyamorous/activerecord_5.2_ruby_2/join_dependency.rb @@ -1,5 +1,3 @@ -# active_record_5.2.1_ruby_2/join_dependency.rb - module Polyamorous module JoinDependencyExtensions # Replaces ActiveRecord::Associations::JoinDependency#build diff --git a/polyamorous/lib/polyamorous/activerecord_5.2.0_ruby_2/reflection.rb b/polyamorous/lib/polyamorous/activerecord_5.2_ruby_2/reflection.rb similarity index 100% rename from polyamorous/lib/polyamorous/activerecord_5.2.0_ruby_2/reflection.rb rename to polyamorous/lib/polyamorous/activerecord_5.2_ruby_2/reflection.rb diff --git a/polyamorous/lib/polyamorous/activerecord_6.0_ruby_2/join_association.rb b/polyamorous/lib/polyamorous/activerecord_6.0_ruby_2/join_association.rb index ea2b61e47..25c1bccf4 100644 --- a/polyamorous/lib/polyamorous/activerecord_6.0_ruby_2/join_association.rb +++ b/polyamorous/lib/polyamorous/activerecord_6.0_ruby_2/join_association.rb @@ -1,2 +1,2 @@ # active_record_6.0_ruby_2/join_association -require 'polyamorous/activerecord_5.2.1_ruby_2/join_association' +require 'polyamorous/activerecord_5.2_ruby_2/join_association' diff --git a/polyamorous/lib/polyamorous/activerecord_6.0_ruby_2/reflection.rb b/polyamorous/lib/polyamorous/activerecord_6.0_ruby_2/reflection.rb index 0417c1089..bb92287e9 100644 --- a/polyamorous/lib/polyamorous/activerecord_6.0_ruby_2/reflection.rb +++ b/polyamorous/lib/polyamorous/activerecord_6.0_ruby_2/reflection.rb @@ -1,2 +1,2 @@ # active_record_6.0_ruby_2/reflection.rb -require 'polyamorous/activerecord_5.2.0_ruby_2/reflection' +require 'polyamorous/activerecord_5.2_ruby_2/reflection' diff --git a/polyamorous/polyamorous.gemspec b/polyamorous/polyamorous.gemspec index ead81360d..57e07e0ba 100644 --- a/polyamorous/polyamorous.gemspec +++ b/polyamorous/polyamorous.gemspec @@ -18,7 +18,7 @@ Gem::Specification.new do |s| associations. } - s.add_dependency 'activerecord', '>= 5.2' + s.add_dependency 'activerecord', '>= 5.2.1' s.files = `git ls-files`.split("\n") s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") diff --git a/ransack.gemspec b/ransack.gemspec index 3bc1b2453..a9c26c31d 100644 --- a/ransack.gemspec +++ b/ransack.gemspec @@ -14,9 +14,9 @@ Gem::Specification.new do |s| s.required_ruby_version = '>= 1.9' s.license = 'MIT' - s.add_dependency 'actionpack', '>= 5.2' - s.add_dependency 'activerecord', '>= 5.2' - s.add_dependency 'activesupport', '>= 5.2' + s.add_dependency 'actionpack', '>= 5.2.1' + s.add_dependency 'activerecord', '>= 5.2.1' + s.add_dependency 'activesupport', '>= 5.2.1' s.add_dependency 'i18n' s.add_dependency 'polyamorous', Ransack::VERSION.to_s diff --git a/spec/helpers/polyamorous_helper.rb b/spec/helpers/polyamorous_helper.rb index 74a144117..b35da729d 100644 --- a/spec/helpers/polyamorous_helper.rb +++ b/spec/helpers/polyamorous_helper.rb @@ -7,18 +7,9 @@ def new_join_association(reflection, children, klass) def new_join_dependency(klass, associations = {}) Polyamorous::JoinDependency.new klass, klass.arel_table, associations, Polyamorous::InnerJoin end - elsif ActiveRecord.version > ::Gem::Version.new("5.2.0") - def new_join_dependency(klass, associations = {}) - Polyamorous::JoinDependency.new klass, klass.arel_table, associations - end - elsif ActiveRecord.version == ::Gem::Version.new("5.2.0") - def new_join_dependency(klass, associations = {}) - alias_tracker = ::ActiveRecord::Associations::AliasTracker.create(klass.connection, klass.table_name, []) - Polyamorous::JoinDependency.new klass, klass.arel_table, associations, alias_tracker - end else def new_join_dependency(klass, associations = {}) - Polyamorous::JoinDependency.new klass, associations, [] + Polyamorous::JoinDependency.new klass, klass.arel_table, associations end end From 754ffc7145a5990325a2392dc5dc6723d7a1777a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Rodr=C3=ADguez?= Date: Wed, 16 Oct 2019 21:05:29 +0200 Subject: [PATCH 6/7] Test against the latest rails 5.2 release --- .travis.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 49eaf3e0c..0882bd25e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -19,9 +19,9 @@ env: - RAILS=5-2-stable DB=mysql - RAILS=5-2-stable DB=postgres - - RAILS=v5.2.1 DB=sqlite3 - - RAILS=v5.2.1 DB=mysql - - RAILS=v5.2.1 DB=postgres + - RAILS=v5.2.3 DB=sqlite3 + - RAILS=v5.2.3 DB=mysql + - RAILS=v5.2.3 DB=postgres matrix: allow_failures: From 49dde38f2b4fd355b1dda12a73673fdb7755d7a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Rodr=C3=ADguez?= Date: Mon, 21 Oct 2019 18:21:53 +0200 Subject: [PATCH 7/7] 5-2-stable failures shouldn't be allowed --- .travis.yml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 0882bd25e..39c4fc668 100644 --- a/.travis.yml +++ b/.travis.yml @@ -25,12 +25,10 @@ env: matrix: allow_failures: - - env: RAILS=5-2-stable DB=sqlite3 - - env: RAILS=5-2-stable DB=mysql - - env: RAILS=5-2-stable DB=postgres - env: RAILS=6-0-stable DB=sqlite3 - env: RAILS=6-0-stable DB=mysql - env: RAILS=6-0-stable DB=postgres + before_script: - if [ "$DB" = "mysql" ]; then