1
0
Fork 0
mirror of https://github.com/activerecord-hackery/ransack.git synced 2022-11-09 13:47:45 -05:00

Drop Rails 5.2.0 support as well

Instead, require a minimum of Rails 5.2.1.
This commit is contained in:
David Rodríguez 2019-10-16 11:31:04 +02:00
parent b135704639
commit 4538c5d905
No known key found for this signature in database
GPG key ID: 1008A258BB37309C
16 changed files with 21 additions and 225 deletions

View file

@ -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

View file

@ -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

View file

@ -97,8 +97,7 @@ module Ransack
# 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 @@ module Ransack
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 @@ module Ransack
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 @@ module Ransack
@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 @@ module Ransack
@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 @@ module Ransack
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

View file

@ -45,8 +45,6 @@ module Ransack
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

View file

@ -12,14 +12,11 @@ if defined?(::ActiveRecord)
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)

View file

@ -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

View file

@ -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

View file

@ -1,2 +0,0 @@
# active_record_5.2.1_ruby_2/reflection.rb
require 'polyamorous/activerecord_5.2.0_ruby_2/reflection'

View file

@ -1,5 +1,3 @@
# active_record_5.2.1_ruby_2/join_association.rb
module Polyamorous
module JoinAssociationExtensions
include SwappingReflectionClass

View file

@ -1,5 +1,3 @@
# active_record_5.2.1_ruby_2/join_dependency.rb
module Polyamorous
module JoinDependencyExtensions
# Replaces ActiveRecord::Associations::JoinDependency#build

View file

@ -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'

View file

@ -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'

View file

@ -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")

View file

@ -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

View file

@ -7,18 +7,9 @@ module PolyamorousHelper
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