From e15a86cdd4ca52e420adebc34c5b07a77b4e4fd7 Mon Sep 17 00:00:00 2001 From: Washington Luiz Date: Wed, 9 Apr 2014 14:25:54 -0300 Subject: [PATCH] MOAR AR version checks for 4.1 and 4.0 support --- Gemfile | 6 +- lib/ransack/adapters/active_record/context.rb | 96 ++++++++++++------- lib/ransack/context.rb | 12 ++- 3 files changed, 71 insertions(+), 43 deletions(-) diff --git a/Gemfile b/Gemfile index b759daf..56f4e5a 100644 --- a/Gemfile +++ b/Gemfile @@ -5,11 +5,7 @@ gem 'rake' rails = ENV['RAILS'] || 'master' -gem 'arel', '~> 5.0.0' - -gem 'polyamorous', - github: 'activerecord-hackery/polyamorous', - branch: 'rails-4.1' +gem 'polyamorous', github: 'activerecord-hackery/polyamorous' case rails when /\// # A path diff --git a/lib/ransack/adapters/active_record/context.rb b/lib/ransack/adapters/active_record/context.rb index 5e6cbe4..388af59 100644 --- a/lib/ransack/adapters/active_record/context.rb +++ b/lib/ransack/adapters/active_record/context.rb @@ -127,7 +127,7 @@ module Ransack 'string_join' when Hash, Symbol, Array 'association_join' - when ::ActiveRecord::Associations::JoinDependency + when JoinDependency, JoinDependency::JoinAssociation 'stashed_join' when Arel::Nodes::Join 'join_node' @@ -157,50 +157,76 @@ module Ransack join_dependency.alias_tracker.aliases[join.left.name.downcase] = 1 end - join_dependency + if ::ActiveRecord::VERSION::STRING >= "4.1" + join_dependency + else + join_dependency.graft(*stashed_association_joins) + end end - def build_or_find_association(name, parent = @base, klass = nil) - list = if ActiveRecord::VERSION::STRING >= "4.1" - @join_dependency.join_root.children.detect - else - @join_dependency.join_associations - end + if ::ActiveRecord::VERSION::STRING >= "4.1" - found_association = list.detect do |assoc| - assoc.reflection.name == name && - @associations_pot[assoc] == parent && - (!klass || assoc.reflection.klass == klass) - end + def build_or_find_association(name, parent = @base, klass = nil) + list = if ::ActiveRecord::VERSION::STRING >= "4.1" + @join_dependency.join_root.children.detect + else + @join_dependency.join_associations + end - unless found_association - jd = JoinDependency.new( - parent.base_klass, - Polyamorous::Join.new(name, @join_type, klass), - [] - ) - found_association = jd.join_root.children.last - associations found_association, parent + found_association = list.detect do |assoc| + assoc.reflection.name == name && + @associations_pot[assoc] == parent && + (!klass || assoc.reflection.klass == klass) + end - # 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 - - # Builds the arel nodes properly for this association - @join_dependency.send( - :construct_tables!, jd.join_root, found_association + unless found_association + jd = JoinDependency.new( + parent.base_klass, + Polyamorous::Join.new(name, @join_type, klass), + [] ) + found_association = jd.join_root.children.last + associations found_association, parent - # Leverage the stashed association functionality in AR - @object = @object.joins(jd) + # 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 + + # Builds the arel nodes properly for this association + @join_dependency.send( + :construct_tables!, jd.join_root, found_association + ) + + # Leverage the stashed association functionality in AR + @object = @object.joins(jd) + end + + found_association end - found_association - end + def associations(assoc, parent) + @associations_pot ||= {} + @associations_pot[assoc] = parent + end + else - def associations(assoc, parent) - @associations_pot ||= {} - @associations_pot[assoc] = parent + def build_or_find_association(name, parent = @base, klass = nil) + found_association = @join_dependency.join_associations + .detect do |assoc| + assoc.reflection.name == name && + assoc.parent == parent && + (!klass || assoc.reflection.klass == klass) + end + unless found_association + @join_dependency.send(:build, Polyamorous::Join.new( + name, @join_type, klass), parent) + found_association = @join_dependency.join_associations.last + # Leverage the stashed association functionality in AR + @object = @object.joins(found_association) + end + + found_association + end end end end diff --git a/lib/ransack/context.rb b/lib/ransack/context.rb index 51895c5..c395010 100644 --- a/lib/ransack/context.rb +++ b/lib/ransack/context.rb @@ -36,8 +36,15 @@ module Ransack @join_dependency = join_dependency(@object) @join_type = options[:join_type] || Arel::OuterJoin @search_key = options[:search_key] || Ransack.options[:search_key] - @base = @join_dependency.join_root - @engine = @base.base_klass.arel_engine + + if ::ActiveRecord::VERSION::STRING >= "4.1" + @base = @join_dependency.join_root + @engine = @base.base_klass.arel_engine + else + @base = @join_dependency.join_base + @engine = @base.arel_engine + end + @default_table = Arel::Table.new( @base.table_name, as: @base.aliased_table_name, engine: @engine ) @@ -141,6 +148,5 @@ module Ransack def sortable_attributes(str = '') traverse(str).ransortable_attributes(auth_object) end - end end