Skip to content

Commit

Permalink
Merge pull request #1027 from vrodokanakis/support_rails_6
Browse files Browse the repository at this point in the history
Support for Rails 6
  • Loading branch information
scarroll32 authored Jun 11, 2019
2 parents 544bcdd + c77b903 commit 2aed3ea
Show file tree
Hide file tree
Showing 12 changed files with 126 additions and 14 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ rvm:
- 2.5

env:
- RAILS=6-0-stable DB=sqlite3
- RAILS=6-0-stable DB=mysql
- RAILS=6-0-stable DB=postgres

- RAILS=5-2-stable DB=sqlite3
- RAILS=5-2-stable DB=mysql
- RAILS=5-2-stable DB=postgres
Expand Down
8 changes: 4 additions & 4 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ gemspec

gem 'rake'

rails = ENV['RAILS'] || '5-2-stable'
rails = ENV['RAILS'] || '6-0-stable'

gem 'pry'

Expand All @@ -16,21 +16,21 @@ when /\// # A path
gem 'activerecord', path: "#{rails}/activerecord", require: false
gem 'actionpack', path: "#{rails}/actionpack"
when /^v/ # A tagged version
git 'git://github.com/rails/rails.git', :tag => rails do
git 'https://github.com/rails/rails.git', :tag => rails do
gem 'activesupport'
gem 'activemodel'
gem 'activerecord', require: false
gem 'actionpack'
end
else
git 'git://github.com/rails/rails.git', :branch => rails do
git 'https://github.com/rails/rails.git', :branch => rails do
gem 'activesupport'
gem 'activemodel'
gem 'activerecord', require: false
gem 'actionpack'
end
end
gem 'mysql2', '~> 0.4.4'
gem 'mysql2', '~> 0.5.2'

group :test do
# TestUnit was removed from Ruby 2.2 but still needed for testing Rails 3.x.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ you're reading the documentation for the master branch with the latest features.

## Getting started

Ransack is compatible with Rails 5.0, 5.1 and 5.2 on Ruby 2.2 and later.
Ransack is compatible with Rails 6.0, 5.0, 5.1 and 5.2 on Ruby 2.2 and later.
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.
Expand Down
26 changes: 22 additions & 4 deletions lib/ransack/adapters/active_record/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,15 @@ def join_sources
base, joins =
if ::ActiveRecord::VERSION::STRING > Constants::RAILS_5_2_0
alias_tracker = ::ActiveRecord::Associations::AliasTracker.create(self.klass.connection, @object.table.name, [])
constraints = if ::ActiveRecord::VERSION::STRING >= Constants::RAILS_6_0
@join_dependency.join_constraints(@object.joins_values, alias_tracker)
else
@join_dependency.join_constraints(@object.joins_values, @join_type, alias_tracker)
end

[
Arel::SelectManager.new(@object.table),
@join_dependency.join_constraints(@object.joins_values, @join_type, alias_tracker)
constraints
]
else
[
Expand Down Expand Up @@ -275,7 +281,11 @@ def build_joins(relation)
end
else
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)
join_dependency = if ::ActiveRecord::VERSION::STRING >= 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
Expand Down Expand Up @@ -303,7 +313,15 @@ def find_association(name, parent = @base, klass = nil)
end

def build_association(name, parent = @base, klass = nil)
if ::ActiveRecord::VERSION::STRING < Constants::RAILS_5_2_0
if ::ActiveRecord::VERSION::STRING >= Constants::RAILS_6_0
jd = Polyamorous::JoinDependency.new(
parent.base_klass,
parent.table,
Polyamorous::Join.new(name, @join_type, klass),
@join_type
)
found_association = jd.instance_variable_get(:@join_root).children.last
elsif ::ActiveRecord::VERSION::STRING < Constants::RAILS_5_2_0
jd = Polyamorous::JoinDependency.new(
parent.base_klass,
Polyamorous::Join.new(name, @join_type, klass),
Expand All @@ -323,7 +341,7 @@ def build_association(name, parent = @base, klass = nil)
jd = Polyamorous::JoinDependency.new(
parent.base_klass,
parent.table,
Polyamorous::Join.new(name, @join_type, klass),
Polyamorous::Join.new(name, @join_type, klass)
)
found_association = jd.instance_variable_get(:@join_root).children.last
end
Expand Down
1 change: 1 addition & 0 deletions lib/ransack/constants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ module Constants
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.rc1'.freeze

RANSACK_SLASH_SEARCHES = 'ransack/searches'.freeze
RANSACK_SLASH_SEARCHES_SLASH_SEARCH = 'ransack/searches/search'.freeze
Expand Down
4 changes: 2 additions & 2 deletions polyamorous/lib/polyamorous.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ 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"
ar_version = "5.2.1" if ::ActiveRecord::VERSION::STRING >= "5.2.1"
ar_version = ::ActiveRecord::VERSION::STRING[0,5] if ar_version >= "5.2" && ::ActiveRecord::VERSION::STRING < "6.0"
ar_version = "5.2.1" if ::ActiveRecord::VERSION::STRING >= "5.2.1" && ::ActiveRecord::VERSION::STRING < "6.0"

%w(join_association join_dependency).each do |file|
require "polyamorous/activerecord_#{ar_version}_ruby_2/#{file}"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# active_record_6.0_ruby_2/join_association
require 'polyamorous/activerecord_5.2.1_ruby_2/join_association'
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# active_record_6.0_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

def join_constraints(joins_to_add, alias_tracker)
@alias_tracker = alias_tracker

construct_tables!(join_root)
joins = make_join_constraints(join_root, join_type)

joins.concat joins_to_add.flat_map { |oj|
construct_tables!(oj.join_root)
if join_root.match?(oj.join_root) && join_root.table.name == oj.join_root.table.name
walk join_root, oj.join_root, oj.join_type
else
make_join_constraints(oj.join_root, oj.join_type)
end
}
end

private
def make_constraints(parent, child, join_type = Arel::Nodes::OuterJoin)
foreign_table = parent.table
foreign_klass = parent.base_klass
join_type = child.join_type || join_type if join_type == Arel::Nodes::InnerJoin
joins = child.join_constraints(foreign_table, foreign_klass, join_type, alias_tracker)
joins.concat child.children.flat_map { |c| make_constraints(child, c, join_type) }
end

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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# active_record_6.0_ruby_2/reflection.rb
require 'polyamorous/activerecord_5.2.0_ruby_2/reflection'
2 changes: 1 addition & 1 deletion ransack.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Gem::Specification.new do |s|
s.add_development_dependency 'rspec', '~> 3'
s.add_development_dependency 'machinist', '~> 1.0.6'
s.add_development_dependency 'faker', '~> 0.9.5'
s.add_development_dependency 'sqlite3', '~> 1.3.3'
s.add_development_dependency 'sqlite3', ENV['RAILS'] == '6-0-stable' ? '~> 1.4.1' : '~> 1.3.3'
s.add_development_dependency 'pg', '~> 0.21'
s.add_development_dependency 'mysql2', '0.3.20'
s.add_development_dependency 'pry', '0.10'
Expand Down
6 changes: 5 additions & 1 deletion spec/helpers/polyamorous_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ def new_join_association(reflection, children, klass)
Polyamorous::JoinAssociation.new reflection, children, klass
end

if ActiveRecord::VERSION::STRING > "5.2.0"
if ActiveRecord::VERSION::STRING >= "6.0.0.rc1"
def new_join_dependency(klass, associations = {})
Polyamorous::JoinDependency.new klass, klass.arel_table, associations, Polyamorous::InnerJoin
end
elsif ActiveRecord::VERSION::STRING > "5.2.0"
def new_join_dependency(klass, associations = {})
Polyamorous::JoinDependency.new klass, klass.arel_table, associations
end
Expand Down
2 changes: 1 addition & 1 deletion spec/ransack/join_dependency_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ module Polyamorous
end

context '#left_outer_join in Rails 5 overrides join type specified',
if: ActiveRecord::VERSION::MAJOR >= 5 && ActiveRecord::VERSION::MINOR < 2 do
if: ActiveRecord::VERSION::MAJOR >= 5 && ActiveRecord::VERSION::MAJOR < 6 && ActiveRecord::VERSION::MINOR < 2 do

let(:join_type_class) do
new_join_dependency(
Expand Down

0 comments on commit 2aed3ea

Please sign in to comment.