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 90% 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 index fd836fa..edfff82 100644 --- a/polyamorous/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 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 76% rename from spec/ransack/join_association_spec.rb rename to spec/polyamorous/join_association_spec.rb index 8766f02..b07db29 100644 --- a/spec/ransack/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 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/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 2dfdc90..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 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}) + 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) 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'