From bc13ccbdf17644bbe6fb3d2733aed9f6077465f2 Mon Sep 17 00:00:00 2001 From: Shadab Malik Date: Tue, 21 Jan 2020 14:14:42 +0530 Subject: [PATCH 1/4] Move polyamorous from gem to a module. --- .../join_association.rb | 0 .../join_dependency.rb | 0 .../activerecord_5.2_ruby_2/reflection.rb | 0 .../join_association.rb | 1 - .../join_dependency.rb | 1 - .../activerecord_6.0_ruby_2/reflection.rb | 1 - .../join_association.rb | 1 - .../join_dependency.rb | 1 - .../activerecord_6.1_ruby_2/reflection.rb | 1 - {polyamorous/lib => lib}/polyamorous/join.rb | 0 .../lib => lib/polyamorous}/polyamorous.rb | 0 .../polyamorous/swapping_reflection_class.rb | 0 .../lib => lib}/polyamorous/tree_node.rb | 0 lib/ransack.rb | 2 +- lib/ransack/adapters/active_record/context.rb | 2 +- polyamorous/lib/polyamorous/version.rb | 3 --- polyamorous/polyamorous.gemspec | 27 ------------------- ransack.gemspec | 7 +++-- .../join_association_spec.rb | 0 .../join_dependency_spec.rb | 0 spec/{ransack => polyamorous}/join_spec.rb | 0 .../adapters/active_record/base_spec.rb | 2 -- spec/ransack/search_spec.rb | 6 ++--- spec/spec_helper.rb | 1 + 24 files changed, 9 insertions(+), 47 deletions(-) rename {polyamorous/lib => lib}/polyamorous/activerecord_5.2_ruby_2/join_association.rb (100%) rename {polyamorous/lib => lib}/polyamorous/activerecord_5.2_ruby_2/join_dependency.rb (100%) rename {polyamorous/lib => lib}/polyamorous/activerecord_5.2_ruby_2/reflection.rb (100%) rename {polyamorous/lib => lib}/polyamorous/activerecord_6.0_ruby_2/join_association.rb (58%) rename {polyamorous/lib => lib}/polyamorous/activerecord_6.0_ruby_2/join_dependency.rb (99%) rename {polyamorous/lib => lib}/polyamorous/activerecord_6.0_ruby_2/reflection.rb (58%) rename {polyamorous/lib => lib}/polyamorous/activerecord_6.1_ruby_2/join_association.rb (58%) rename {polyamorous/lib => lib}/polyamorous/activerecord_6.1_ruby_2/join_dependency.rb (57%) rename {polyamorous/lib => lib}/polyamorous/activerecord_6.1_ruby_2/reflection.rb (58%) rename {polyamorous/lib => lib}/polyamorous/join.rb (100%) rename {polyamorous/lib => lib/polyamorous}/polyamorous.rb (100%) rename {polyamorous/lib => lib}/polyamorous/swapping_reflection_class.rb (100%) rename {polyamorous/lib => lib}/polyamorous/tree_node.rb (100%) delete mode 100644 polyamorous/lib/polyamorous/version.rb delete mode 100644 polyamorous/polyamorous.gemspec rename spec/{ransack => polyamorous}/join_association_spec.rb (100%) rename spec/{ransack => polyamorous}/join_dependency_spec.rb (100%) rename spec/{ransack => polyamorous}/join_spec.rb (100%) diff --git a/polyamorous/lib/polyamorous/activerecord_5.2_ruby_2/join_association.rb b/lib/polyamorous/activerecord_5.2_ruby_2/join_association.rb similarity index 100% rename from polyamorous/lib/polyamorous/activerecord_5.2_ruby_2/join_association.rb rename to lib/polyamorous/activerecord_5.2_ruby_2/join_association.rb diff --git a/polyamorous/lib/polyamorous/activerecord_5.2_ruby_2/join_dependency.rb b/lib/polyamorous/activerecord_5.2_ruby_2/join_dependency.rb similarity index 100% rename from polyamorous/lib/polyamorous/activerecord_5.2_ruby_2/join_dependency.rb rename to lib/polyamorous/activerecord_5.2_ruby_2/join_dependency.rb diff --git a/polyamorous/lib/polyamorous/activerecord_5.2_ruby_2/reflection.rb b/lib/polyamorous/activerecord_5.2_ruby_2/reflection.rb similarity index 100% rename from polyamorous/lib/polyamorous/activerecord_5.2_ruby_2/reflection.rb rename to lib/polyamorous/activerecord_5.2_ruby_2/reflection.rb diff --git a/polyamorous/lib/polyamorous/activerecord_6.0_ruby_2/join_association.rb b/lib/polyamorous/activerecord_6.0_ruby_2/join_association.rb similarity index 58% rename from polyamorous/lib/polyamorous/activerecord_6.0_ruby_2/join_association.rb rename to lib/polyamorous/activerecord_6.0_ruby_2/join_association.rb index 25c1bcc..7a03623 100644 --- a/polyamorous/lib/polyamorous/activerecord_6.0_ruby_2/join_association.rb +++ b/lib/polyamorous/activerecord_6.0_ruby_2/join_association.rb @@ -1,2 +1 @@ -# active_record_6.0_ruby_2/join_association require 'polyamorous/activerecord_5.2_ruby_2/join_association' diff --git a/polyamorous/lib/polyamorous/activerecord_6.0_ruby_2/join_dependency.rb b/lib/polyamorous/activerecord_6.0_ruby_2/join_dependency.rb similarity index 99% rename from polyamorous/lib/polyamorous/activerecord_6.0_ruby_2/join_dependency.rb rename to lib/polyamorous/activerecord_6.0_ruby_2/join_dependency.rb index bbe00a8..e2ce3e3 100644 --- a/polyamorous/lib/polyamorous/activerecord_6.0_ruby_2/join_dependency.rb +++ b/lib/polyamorous/activerecord_6.0_ruby_2/join_dependency.rb @@ -1,5 +1,4 @@ # active_record_6.0_ruby_2/join_dependency.rb - module Polyamorous module JoinDependencyExtensions # Replaces ActiveRecord::Associations::JoinDependency#build diff --git a/polyamorous/lib/polyamorous/activerecord_6.0_ruby_2/reflection.rb b/lib/polyamorous/activerecord_6.0_ruby_2/reflection.rb similarity index 58% rename from polyamorous/lib/polyamorous/activerecord_6.0_ruby_2/reflection.rb rename to lib/polyamorous/activerecord_6.0_ruby_2/reflection.rb index bb92287..54ff04e 100644 --- a/polyamorous/lib/polyamorous/activerecord_6.0_ruby_2/reflection.rb +++ b/lib/polyamorous/activerecord_6.0_ruby_2/reflection.rb @@ -1,2 +1 @@ -# active_record_6.0_ruby_2/reflection.rb require 'polyamorous/activerecord_5.2_ruby_2/reflection' diff --git a/polyamorous/lib/polyamorous/activerecord_6.1_ruby_2/join_association.rb b/lib/polyamorous/activerecord_6.1_ruby_2/join_association.rb similarity index 58% rename from polyamorous/lib/polyamorous/activerecord_6.1_ruby_2/join_association.rb rename to lib/polyamorous/activerecord_6.1_ruby_2/join_association.rb index 7e0159a..ba5722d 100644 --- a/polyamorous/lib/polyamorous/activerecord_6.1_ruby_2/join_association.rb +++ b/lib/polyamorous/activerecord_6.1_ruby_2/join_association.rb @@ -1,2 +1 @@ -# active_record_6.1_ruby_2/join_association require 'polyamorous/activerecord_6.0_ruby_2/join_association' diff --git a/polyamorous/lib/polyamorous/activerecord_6.1_ruby_2/join_dependency.rb b/lib/polyamorous/activerecord_6.1_ruby_2/join_dependency.rb similarity index 57% rename from polyamorous/lib/polyamorous/activerecord_6.1_ruby_2/join_dependency.rb rename to lib/polyamorous/activerecord_6.1_ruby_2/join_dependency.rb index 6be42da..6c2d3dd 100644 --- a/polyamorous/lib/polyamorous/activerecord_6.1_ruby_2/join_dependency.rb +++ b/lib/polyamorous/activerecord_6.1_ruby_2/join_dependency.rb @@ -1,2 +1 @@ -# active_record_6.1_ruby_2/join_dependency.rb require 'polyamorous/activerecord_6.0_ruby_2/join_dependency' diff --git a/polyamorous/lib/polyamorous/activerecord_6.1_ruby_2/reflection.rb b/lib/polyamorous/activerecord_6.1_ruby_2/reflection.rb similarity index 58% rename from polyamorous/lib/polyamorous/activerecord_6.1_ruby_2/reflection.rb rename to lib/polyamorous/activerecord_6.1_ruby_2/reflection.rb index 6d378de..f09e424 100644 --- a/polyamorous/lib/polyamorous/activerecord_6.1_ruby_2/reflection.rb +++ b/lib/polyamorous/activerecord_6.1_ruby_2/reflection.rb @@ -1,2 +1 @@ -# active_record_6.1_ruby_2/reflection.rb require 'polyamorous/activerecord_6.0_ruby_2/reflection' diff --git a/polyamorous/lib/polyamorous/join.rb b/lib/polyamorous/join.rb similarity index 100% rename from polyamorous/lib/polyamorous/join.rb rename to lib/polyamorous/join.rb diff --git a/polyamorous/lib/polyamorous.rb b/lib/polyamorous/polyamorous.rb similarity index 100% rename from polyamorous/lib/polyamorous.rb rename to lib/polyamorous/polyamorous.rb diff --git a/polyamorous/lib/polyamorous/swapping_reflection_class.rb b/lib/polyamorous/swapping_reflection_class.rb similarity index 100% rename from polyamorous/lib/polyamorous/swapping_reflection_class.rb rename to lib/polyamorous/swapping_reflection_class.rb diff --git a/polyamorous/lib/polyamorous/tree_node.rb b/lib/polyamorous/tree_node.rb similarity index 100% rename from polyamorous/lib/polyamorous/tree_node.rb rename to lib/polyamorous/tree_node.rb diff --git a/lib/ransack.rb b/lib/ransack.rb index 1fe2ee6..75b3830 100644 --- a/lib/ransack.rb +++ b/lib/ransack.rb @@ -1,7 +1,7 @@ require 'active_support/core_ext' require 'ransack/configuration' require 'ransack/adapters' -require 'polyamorous' +require 'polyamorous/polyamorous.rb' Ransack::Adapters.object_mapper.require_constants diff --git a/lib/ransack/adapters/active_record/context.rb b/lib/ransack/adapters/active_record/context.rb index 0313210..57eec1f 100644 --- a/lib/ransack/adapters/active_record/context.rb +++ b/lib/ransack/adapters/active_record/context.rb @@ -1,5 +1,5 @@ require 'ransack/context' -require 'polyamorous' +require 'polyamorous/polyamorous' module Ransack module Adapters diff --git a/polyamorous/lib/polyamorous/version.rb b/polyamorous/lib/polyamorous/version.rb deleted file mode 100644 index 7d0463e..0000000 --- a/polyamorous/lib/polyamorous/version.rb +++ /dev/null @@ -1,3 +0,0 @@ -module Polyamorous - VERSION = '2.3.2' -end diff --git a/polyamorous/polyamorous.gemspec b/polyamorous/polyamorous.gemspec deleted file mode 100644 index 57e07e0..0000000 --- a/polyamorous/polyamorous.gemspec +++ /dev/null @@ -1,27 +0,0 @@ -# -*- encoding: utf-8 -*- -$:.push File.expand_path("../lib", __FILE__) -require "polyamorous/version" - -Gem::Specification.new do |s| - s.name = "polyamorous" - s.version = Polyamorous::VERSION - s.authors = ["Ernie Miller", "Ryan Bigg", "Jon Atack", "Xiang Li"] - s.email = ["ernie@erniemiller.org", "radarlistener@gmail.com", "jonnyatack@gmail.com", "bigxiang@gmail.com"] - s.homepage = "https://github.com/activerecord-hackery/ransack/tree/master/polyamorous" - s.license = "MIT" - s.summary = %q{ - Loves/is loved by polymorphic belongs_to associations, Ransack, Squeel, MetaSearch... - } - s.description = %q{ - This is just an extraction from Ransack/Squeel. You probably don't want to use this - directly. It extends ActiveRecord's associations to support polymorphic belongs_to - associations. - } - - s.add_dependency 'activerecord', '>= 5.2.1' - - s.files = `git ls-files`.split("\n") - s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") - s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } - s.require_paths = ["lib"] -end diff --git a/ransack.gemspec b/ransack.gemspec index c8c9985..dedaf8c 100644 --- a/ransack.gemspec +++ b/ransack.gemspec @@ -17,10 +17,9 @@ Gem::Specification.new do |s| s.add_dependency 'activerecord', '>= 5.2.1' s.add_dependency 'activesupport', '>= 5.2.1' s.add_dependency 'i18n' - s.add_dependency 'polyamorous', '2.3.2' - - s.files = `git ls-files`.split("\n").reject { |f| f.match(%r{^(polyamorous/|logo/|coverage/|spec/)}) } - s.test_files = `git ls-files -- spec/*`.split("\n") + s.files = `git ls-files`.split("\n") + s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") + s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } s.require_paths = ["lib"] end diff --git a/spec/ransack/join_association_spec.rb b/spec/polyamorous/join_association_spec.rb similarity index 100% rename from spec/ransack/join_association_spec.rb rename to spec/polyamorous/join_association_spec.rb diff --git a/spec/ransack/join_dependency_spec.rb b/spec/polyamorous/join_dependency_spec.rb similarity index 100% rename from spec/ransack/join_dependency_spec.rb rename to spec/polyamorous/join_dependency_spec.rb diff --git a/spec/ransack/join_spec.rb b/spec/polyamorous/join_spec.rb similarity index 100% rename from spec/ransack/join_spec.rb rename to spec/polyamorous/join_spec.rb diff --git a/spec/ransack/adapters/active_record/base_spec.rb b/spec/ransack/adapters/active_record/base_spec.rb index da60751..54255ce 100644 --- a/spec/ransack/adapters/active_record/base_spec.rb +++ b/spec/ransack/adapters/active_record/base_spec.rb @@ -143,14 +143,12 @@ module Ransack it 'removes redundant joins from top query' do s = Article.ransack(tags_name_not_eq: "Fantasy") sql = s.result.to_sql - expect(sql).to_not include('LEFT OUTER JOIN') end it 'handles != for single values' do s = Article.ransack(tags_name_not_eq: "Fantasy") articles = s.result.to_a - expect(articles).to include marco expect(articles).to_not include arthur end diff --git a/spec/ransack/search_spec.rb b/spec/ransack/search_spec.rb index 2dfdc90..c44af0a 100644 --- a/spec/ransack/search_spec.rb +++ b/spec/ransack/search_spec.rb @@ -272,11 +272,11 @@ module Ransack parent_articles_title_eq: 'parents_article_title_query' }).result real_query = remove_quotes_and_backticks(s.to_sql) - expect(real_query) - .to match(%r{LEFT OUTER JOIN articles ON (\('default_scope' = 'default_scope'\) AND )?articles.person_id = people.id}) + .to include("LEFT OUTER JOIN articles ON articles.person_id = parents_people.id AND ('default_scope' = 'default_scope')") expect(real_query) - .to match(%r{LEFT OUTER JOIN articles articles_people ON (\('default_scope' = 'default_scope'\) AND )?articles_people.person_id = parents_people.id}) + .to include("LEFT OUTER JOIN articles articles_people ON articles_people.person_id = people.id AND ('default_scope' = 'default_scope')") + expect(real_query) .to include "people.name = 'person_name_query'" expect(real_query) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 38229de..ce2a625 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,4 +1,5 @@ require 'machinist/active_record' +require 'polyamorous/polyamorous.rb' require 'sham' require 'faker' require 'ransack' From d054f114fd9edece1f7972fe1cd0557200aa6816 Mon Sep 17 00:00:00 2001 From: Shadab Malik Date: Tue, 21 Jan 2020 17:10:38 +0530 Subject: [PATCH 2/4] Fix spec based on rails version. --- .../adapters/active_record/context_spec.rb | 17 ------------ spec/ransack/search_spec.rb | 26 ++++++++++++------- 2 files changed, 16 insertions(+), 27 deletions(-) diff --git a/spec/ransack/adapters/active_record/context_spec.rb b/spec/ransack/adapters/active_record/context_spec.rb index af43b13..6fb135b 100644 --- a/spec/ransack/adapters/active_record/context_spec.rb +++ b/spec/ransack/adapters/active_record/context_spec.rb @@ -110,23 +110,6 @@ module Ransack context: shared_context) end - describe '#join_associations', if: AR_version <= '4.0' do - it 'returns dependent join associations for all searches run - against the context' do - parents, children = shared_context.join_associations - - expect(children.aliased_table_name).to eq "children_people" - expect(parents.aliased_table_name).to eq "parents_people" - end - - it 'can be rejoined to execute a valid query' do - parents, children = shared_context.join_associations - - expect { Person.joins(parents).joins(children).to_a } - .to_not raise_error - end - end - describe '#join_sources' do it 'returns dependent arel join nodes for all searches run against the context' do diff --git a/spec/ransack/search_spec.rb b/spec/ransack/search_spec.rb index c44af0a..be41416 100644 --- a/spec/ransack/search_spec.rb +++ b/spec/ransack/search_spec.rb @@ -271,12 +271,21 @@ module Ransack parent_name_eq: "parent_name_query", parent_articles_title_eq: 'parents_article_title_query' }).result + real_query = remove_quotes_and_backticks(s.to_sql) - expect(real_query) - .to include("LEFT OUTER JOIN articles ON articles.person_id = parents_people.id AND ('default_scope' = 'default_scope')") - expect(real_query) - .to include("LEFT OUTER JOIN articles articles_people ON articles_people.person_id = people.id AND ('default_scope' = 'default_scope')") - + + if ::Gem::Version.new(::ActiveRecord::VERSION::STRING) > ::Gem::Version.new(Constants::RAILS_6_0) + expect(real_query) + .to match(%r{LEFT OUTER JOIN articles ON (\('default_scope' = 'default_scope'\) AND )?articles.person_id = parents_people.id}) + expect(real_query) + .to match(%r{LEFT OUTER JOIN articles articles_people ON (\('default_scope' = 'default_scope'\) AND )?articles_people.person_id = people.id}) + else + expect(real_query) + .to match(%r{LEFT OUTER JOIN articles ON (\('default_scope' = 'default_scope'\) AND )?articles.person_id = people.id}) + expect(real_query) + .to match(%r{LEFT OUTER JOIN articles articles_people ON (\('default_scope' = 'default_scope'\) AND )?articles_people.person_id = parents_people.id}) + end + expect(real_query) .to include "people.name = 'person_name_query'" expect(real_query) @@ -357,11 +366,8 @@ module Ransack { m: 'or', comments_body_cont: 'e', articles_comments_body_cont: 'e' } ] ) - if ActiveRecord::VERSION::MAJOR == 3 - all_or_load, uniq_or_distinct = :all, :uniq - else - all_or_load, uniq_or_distinct = :load, :distinct - end + + all_or_load, uniq_or_distinct = :load, :distinct expect(s.result.send(all_or_load).size) .to eq(9000) expect(s.result(distinct: true).size) From ca51ed8a1749fb39e472fec72cc6f938798e7f0c Mon Sep 17 00:00:00 2001 From: Piers Chambers Date: Wed, 1 Apr 2020 18:06:50 -0400 Subject: [PATCH 3/4] Restore and update polyamorous join association equality spec, originally https://github.com/activerecord-hackery/polyamorous/commit/6e2e1cc5d0b53e4d8f02aa5286e6d11aa91f5ac3#diff-68f369577aaf68dd541acf26e438e413R20-R37 --- spec/polyamorous/join_association_spec.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/spec/polyamorous/join_association_spec.rb b/spec/polyamorous/join_association_spec.rb index 8766f02..b07db29 100644 --- a/spec/polyamorous/join_association_spec.rb +++ b/spec/polyamorous/join_association_spec.rb @@ -10,6 +10,13 @@ module Polyamorous new_join_association(reflection, parent.children, Article) } + subject { new_join_association(reflection, parent.children, Person) } + + it 'respects polymorphism on equality test' do + expect(subject).to eq new_join_association(reflection, parent.children, Person) + expect(subject).not_to eq new_join_association(reflection, parent.children, Article) + end + it 'leaves the orginal reflection intact for thread safety' do reflection.instance_variable_set(:@klass, Article) join_association From 3e7ec220f10e3dbc9b142980f40d3668624afd9b Mon Sep 17 00:00:00 2001 From: Piers Chambers Date: Thu, 2 Apr 2020 20:11:29 -0400 Subject: [PATCH 4/4] Revert db46230 to fix restored spec. --- lib/polyamorous/activerecord_5.2_ruby_2/join_association.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/polyamorous/activerecord_5.2_ruby_2/join_association.rb b/lib/polyamorous/activerecord_5.2_ruby_2/join_association.rb index fd836fa..edfff82 100644 --- a/lib/polyamorous/activerecord_5.2_ruby_2/join_association.rb +++ b/lib/polyamorous/activerecord_5.2_ruby_2/join_association.rb @@ -16,5 +16,9 @@ module Polyamorous super(reflection, children) end end + + def ==(other) + base_klass == other.base_klass + end end end