mirror of
https://github.com/activerecord-hackery/ransack.git
synced 2022-11-09 13:47:45 -05:00
Merge pull request #1113 from varyonic/polyamorous-module
Polyamorous module
This commit is contained in:
commit
ab8c34d9c2
25 changed files with 32 additions and 70 deletions
|
@ -16,5 +16,9 @@ module Polyamorous
|
|||
super(reflection, children)
|
||||
end
|
||||
end
|
||||
|
||||
def ==(other)
|
||||
base_klass == other.base_klass
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,2 +1 @@
|
|||
# active_record_6.0_ruby_2/join_association
|
||||
require 'polyamorous/activerecord_5.2_ruby_2/join_association'
|
|
@ -1,5 +1,4 @@
|
|||
# active_record_6.0_ruby_2/join_dependency.rb
|
||||
|
||||
module Polyamorous
|
||||
module JoinDependencyExtensions
|
||||
# Replaces ActiveRecord::Associations::JoinDependency#build
|
|
@ -1,2 +1 @@
|
|||
# active_record_6.0_ruby_2/reflection.rb
|
||||
require 'polyamorous/activerecord_5.2_ruby_2/reflection'
|
|
@ -1,2 +1 @@
|
|||
# active_record_6.1_ruby_2/join_association
|
||||
require 'polyamorous/activerecord_6.0_ruby_2/join_association'
|
|
@ -1,2 +1 @@
|
|||
# active_record_6.1_ruby_2/join_dependency.rb
|
||||
require 'polyamorous/activerecord_6.0_ruby_2/join_dependency'
|
|
@ -1,2 +1 @@
|
|||
# active_record_6.1_ruby_2/reflection.rb
|
||||
require 'polyamorous/activerecord_6.0_ruby_2/reflection'
|
|
@ -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
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
require 'ransack/context'
|
||||
require 'polyamorous'
|
||||
require 'polyamorous/polyamorous'
|
||||
|
||||
module Ransack
|
||||
module Adapters
|
||||
|
|
|
@ -1,3 +0,0 @@
|
|||
module Polyamorous
|
||||
VERSION = '2.3.2'
|
||||
end
|
|
@ -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
|
|
@ -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
|
||||
|
|
|
@ -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
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
require 'machinist/active_record'
|
||||
require 'polyamorous/polyamorous.rb'
|
||||
require 'sham'
|
||||
require 'faker'
|
||||
require 'ransack'
|
||||
|
|
Loading…
Reference in a new issue