From 4917cd8cfa5b8c2f7ef6f08a62aa3bce93c5c94f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Bonis=C5=82awski?= Date: Wed, 13 Dec 2017 20:55:15 +0100 Subject: [PATCH] add support for rails 5.2 --- lib/ransack/adapters/active_record/context.rb | 33 +++++++++++-------- .../adapters/active_record/ransack/context.rb | 15 +++++++-- lib/ransack/constants.rb | 1 + lib/ransack/helpers/form_builder.rb | 2 +- ransack.gemspec | 2 +- 5 files changed, 35 insertions(+), 18 deletions(-) diff --git a/lib/ransack/adapters/active_record/context.rb b/lib/ransack/adapters/active_record/context.rb index 07477175d..995aa835a 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 @@ -279,12 +281,14 @@ def build_joins(relation) relation.table.from(relation.table), string_joins end + alias_tracker = ::ActiveRecord::Associations::AliasTracker.create(::ActiveRecord::Base.connection, relation.table.name, join_list) + join_dependency = JoinDependency.new( - relation.klass, association_joins, join_list + relation.klass, relation.table, association_joins, alias_tracker ) join_nodes.each do |join| - join_dependency.alias_tracker.aliases[join.left.name.downcase] = 1 + join_dependency.send(:alias_tracker).aliases[join.left.name.downcase] = 1 end if ::ActiveRecord::VERSION::STRING >= Constants::RAILS_4_1 @@ -308,7 +312,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 +320,24 @@ def find_association(name, parent = @base, klass = nil) end def build_association(name, parent = @base, klass = nil) + 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.join_root.children.last + found_association = jd.instance_variable_get(:@join_root).children.last @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 +347,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 708efeaa2..f465c6faf 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 @@ -38,9 +40,16 @@ def initialize(object, options = {}) @engine = @base.arel_engine end - @default_table = Arel::Table.new( - @base.table_name, as: @base.aliased_table_name, type_caster: self + if ::ActiveRecord::VERSION::STRING >= Constants::RAILS_5_2 + @default_table = Arel::Table.new( + @base.table_name, as: @base.base_klass.table_name, type_caster: self ) + else + @default_table = Arel::Table.new( + @base.table_name, as: @base.aliased_table_name, type_caster: self + ) + end + @bind_pairs = Hash.new do |hash, key| parent, attr_name = get_parent_and_attribute_name(key) if parent && attr_name 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..30379d31e 100644 --- a/lib/ransack/helpers/form_builder.rb +++ b/lib/ransack/helpers/form_builder.rb @@ -6,7 +6,7 @@ module ActionView::Helpers::Tags # https://github.com/rails/rails/commit/c1a118a class Base private - def value(object) + def value object.send @method_name if object # use send instead of public_send end end diff --git a/ransack.gemspec b/ransack.gemspec index 2961f67b4..c364d9cac 100644 --- a/ransack.gemspec +++ b/ransack.gemspec @@ -17,7 +17,7 @@ Gem::Specification.new do |s| s.rubyforge_project = "ransack" s.add_dependency 'actionpack', '>= 3.0' - s.add_dependency 'activerecord', '>= 3.0' + s.add_dependency 'activerecord', '>= 5.2.0.beta2' s.add_dependency 'activesupport', '>= 3.0' s.add_dependency 'i18n' s.add_dependency 'polyamorous', '~> 1.3'