Skip to content
This repository has been archived by the owner on Mar 19, 2021. It is now read-only.

Commit

Permalink
Merge pull request activerecord-hackery#868 from varyonic/rails-5.2
Browse files Browse the repository at this point in the history
Add support for Rails 5.2
  • Loading branch information
scarroll32 authored Feb 5, 2018
2 parents 7513987 + e533651 commit 88661fb
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 25 deletions.
18 changes: 18 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ rvm:
- 2.0

env:
- RAILS=5-2-stable DB=sqlite3
- RAILS=5-2-stable DB=mysql
- RAILS=5-2-stable DB=postgres

- RAILS=5-0-stable DB=sqlite3
- RAILS=5-0-stable DB=mysql
- RAILS=5-0-stable DB=postgres
Expand All @@ -35,13 +39,27 @@ env:

matrix:
exclude:
- rvm: 2.1.10
env: RAILS=5-2-stable DB=sqlite3
- rvm: 2.1.10
env: RAILS=5-2-stable DB=mysql
- rvm: 2.1.10
env: RAILS=5-2-stable DB=postgres

- rvm: 2.1.10
env: RAILS=5-0-stable DB=sqlite3
- rvm: 2.1.10
env: RAILS=5-0-stable DB=mysql
- rvm: 2.1.10
env: RAILS=5-0-stable DB=postgres

- rvm: 2.0
env: RAILS=5-2-stable DB=sqlite3
- rvm: 2.0
env: RAILS=5-2-stable DB=mysql
- rvm: 2.0
env: RAILS=5-2-stable DB=postgres

- rvm: 2.0
env: RAILS=5-0-stable DB=sqlite3
- rvm: 2.0
Expand Down
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ else
if rails == '3-0-stable'
gem 'mysql2', '< 0.3'
end
if rails == '5-2-stable'
gem 'mysql2', '~> 0.4.4'
end
end

if ENV['DB'] =~ /mongoid4/
Expand Down
62 changes: 41 additions & 21 deletions lib/ransack/adapters/active_record/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ class Context < ::Ransack::Context

def initialize(object, options = {})
super
@arel_visitor = @engine.connection.visitor
if ::ActiveRecord::VERSION::STRING < Constants::RAILS_5_2
@arel_visitor = @engine.connection.visitor
end
end

def relation_for(object)
Expand All @@ -25,7 +27,7 @@ def type_for(attr)
return nil unless attr && attr.valid?
name = attr.arel_attribute.name.to_s
table = attr.arel_attribute.relation.table_name
schema_cache = @engine.connection.schema_cache
schema_cache = ::ActiveRecord::Base.connection.schema_cache
unless schema_cache.send(database_table_exists?, table)
raise "No table named #{table} exists."
end
Expand Down Expand Up @@ -135,7 +137,7 @@ def join_sources
end

def alias_tracker
@join_dependency.alias_tracker
@join_dependency.send(:alias_tracker)
end

def lock_association(association)
Expand All @@ -145,11 +147,11 @@ def lock_association(association)
if ::ActiveRecord::VERSION::STRING >= Constants::RAILS_4_1
def remove_association(association)
return if @lock_associations.include?(association)
@join_dependency.join_root.children.delete_if { |stashed|
@join_dependency.instance_variable_get(:@join_root).children.delete_if { |stashed|
stashed.eql?(association)
}
@object.joins_values.delete_if { |jd|
jd.join_root.children.map(&:object_id) == [association.object_id]
jd.instance_variable_get(:@join_root).children.map(&:object_id) == [association.object_id]
}
end
else
Expand Down Expand Up @@ -280,12 +282,17 @@ def build_joins(relation)
relation.table.from(relation.table), string_joins
end

join_dependency = JoinDependency.new(
relation.klass, association_joins, join_list
)

join_nodes.each do |join|
join_dependency.alias_tracker.aliases[join.left.name.downcase] = 1
if ::ActiveRecord::VERSION::STRING < Constants::RAILS_5_2
join_dependency = 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
else
alias_tracker = ::ActiveRecord::Associations::AliasTracker.create(::ActiveRecord::Base.connection, relation.table.name, join_list)
join_dependency = JoinDependency.new(relation.klass, relation.table, association_joins, alias_tracker)
join_nodes.each do |join|
join_dependency.alias_tracker.aliases[join.left.name.downcase] = 1
end
end

if ::ActiveRecord::VERSION::STRING >= Constants::RAILS_4_1
Expand All @@ -308,29 +315,42 @@ def build_or_find_association(name, parent = @base, klass = nil)
if ::ActiveRecord::VERSION::STRING >= Constants::RAILS_4_1

def find_association(name, parent = @base, klass = nil)
@join_dependency.join_root.children.detect do |assoc|
@join_dependency.instance_variable_get(:@join_root).children.detect do |assoc|
assoc.reflection.name == name &&
(@associations_pot.empty? || @associations_pot[assoc] == parent) &&
(!klass || assoc.reflection.klass == klass)
end
end

def build_association(name, parent = @base, klass = nil)
jd = JoinDependency.new(
parent.base_klass,
Polyamorous::Join.new(name, @join_type, klass),
[]
)
found_association = jd.join_root.children.last
if ::ActiveRecord::VERSION::STRING < Constants::RAILS_5_2
jd = JoinDependency.new(
parent.base_klass,
Polyamorous::Join.new(name, @join_type, klass),
[]
)
found_association = jd.join_root.children.last
else
alias_tracker = ::ActiveRecord::Associations::AliasTracker.create(::ActiveRecord::Base.connection, parent.table.name, [])
jd = JoinDependency.new(
parent.base_klass,
parent.base_klass.arel_table,
Polyamorous::Join.new(name, @join_type, klass),
alias_tracker
)
found_association = jd.instance_variable_get(:@join_root).children.last
end


@associations_pot[found_association] = parent

# TODO maybe we dont need to push associations here, we could loop
# through the @associations_pot instead
@join_dependency.join_root.children.push found_association
@join_dependency.instance_variable_get(:@join_root).children.push found_association

# Builds the arel nodes properly for this association
@join_dependency.send(
:construct_tables!, jd.join_root, found_association
:construct_tables!, jd.instance_variable_get(:@join_root), found_association
)

# Leverage the stashed association functionality in AR
Expand All @@ -340,7 +360,7 @@ def build_association(name, parent = @base, klass = nil)
end

def extract_joins(association)
parent = @join_dependency.join_root
parent = @join_dependency.instance_variable_get(:@join_root)
reflection = association.reflection
join_constraints = if ::ActiveRecord::VERSION::STRING < Constants::RAILS_5_1
association.join_constraints(
Expand Down
4 changes: 3 additions & 1 deletion lib/ransack/adapters/active_record/ransack/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ def initialize(object, options = {})
@associations_pot = {}
@lock_associations = []

if ::ActiveRecord::VERSION::STRING >= Constants::RAILS_4_1
if ::ActiveRecord::VERSION::STRING >= Constants::RAILS_5_2
@base = @join_dependency.instance_variable_get(:@join_root)
elsif ::ActiveRecord::VERSION::STRING >= Constants::RAILS_4_1
@base = @join_dependency.join_root
@engine = @base.base_klass.arel_engine
else
Expand Down
1 change: 1 addition & 0 deletions lib/ransack/constants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ module Constants

RAILS_4_1 = '4.1'.freeze
RAILS_5_1 = '5.1'.freeze
RAILS_5_2 = '5.2'.freeze

RANSACK_SLASH_SEARCHES = 'ransack/searches'.freeze
RANSACK_SLASH_SEARCHES_SLASH_SEARCH = 'ransack/searches/search'.freeze
Expand Down
12 changes: 9 additions & 3 deletions lib/ransack/helpers/form_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@ module ActionView::Helpers::Tags
# https://github.com/rails/rails/commit/c1a118a
class Base
private
def value(object)
object.send @method_name if object # use send instead of public_send
end
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
@object.send @method_name if @object
end
end
end
end

Expand Down

0 comments on commit 88661fb

Please sign in to comment.