From d44575506ccc7cb00e618256707ed53732a07bca Mon Sep 17 00:00:00 2001 From: Piers Chambers Date: Sat, 3 Feb 2018 08:28:04 -0500 Subject: [PATCH] Adapt to changes in Rails 5.2. --- lib/ransack/adapters/active_record/context.rb | 62 ++++++++++++------- .../adapters/active_record/ransack/context.rb | 4 +- lib/ransack/constants.rb | 1 + lib/ransack/helpers/form_builder.rb | 12 +++- 4 files changed, 54 insertions(+), 25 deletions(-) diff --git a/lib/ransack/adapters/active_record/context.rb b/lib/ransack/adapters/active_record/context.rb index 235b73809..2228701a3 100644 --- a/lib/ransack/adapters/active_record/context.rb +++ b/lib/ransack/adapters/active_record/context.rb @@ -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) @@ -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 @@ -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) @@ -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 @@ -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 @@ -308,7 +315,7 @@ 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) @@ -316,21 +323,34 @@ def find_association(name, parent = @base, klass = nil) 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 @@ -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( diff --git a/lib/ransack/adapters/active_record/ransack/context.rb b/lib/ransack/adapters/active_record/ransack/context.rb index 68f35f205..922e3485c 100644 --- a/lib/ransack/adapters/active_record/ransack/context.rb +++ b/lib/ransack/adapters/active_record/ransack/context.rb @@ -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 diff --git a/lib/ransack/constants.rb b/lib/ransack/constants.rb index a24148176..7563be722 100644 --- a/lib/ransack/constants.rb +++ b/lib/ransack/constants.rb @@ -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 diff --git a/lib/ransack/helpers/form_builder.rb b/lib/ransack/helpers/form_builder.rb index 81d7be7f0..c42b89f60 100644 --- a/lib/ransack/helpers/form_builder.rb +++ b/lib/ransack/helpers/form_builder.rb @@ -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