Skip to content

Commit

Permalink
Merge pull request #1113 from varyonic/polyamorous-module
Browse files Browse the repository at this point in the history
Polyamorous module
  • Loading branch information
scarroll32 authored Apr 4, 2020
2 parents 55a83f0 + 3e7ec22 commit ab8c34d
Show file tree
Hide file tree
Showing 25 changed files with 32 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,9 @@ def initialize(reflection, children, polymorphic_class = nil, join_type = Arel::
super(reflection, children)
end
end

def ==(other)
base_klass == other.base_klass
end
end
end
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
# active_record_6.0_ruby_2/join_association
require 'polyamorous/activerecord_5.2_ruby_2/join_association'
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# active_record_6.0_ruby_2/join_dependency.rb

module Polyamorous
module JoinDependencyExtensions
# Replaces ActiveRecord::Associations::JoinDependency#build
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
# active_record_6.0_ruby_2/reflection.rb
require 'polyamorous/activerecord_5.2_ruby_2/reflection'
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
# active_record_6.1_ruby_2/join_association
require 'polyamorous/activerecord_6.0_ruby_2/join_association'
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
# active_record_6.1_ruby_2/join_dependency.rb
require 'polyamorous/activerecord_6.0_ruby_2/join_dependency'
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
# active_record_6.1_ruby_2/reflection.rb
require 'polyamorous/activerecord_6.0_ruby_2/reflection'
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion lib/ransack.rb
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/ransack/adapters/active_record/context.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'ransack/context'
require 'polyamorous'
require 'polyamorous/polyamorous'

module Ransack
module Adapters
Expand Down
3 changes: 0 additions & 3 deletions polyamorous/lib/polyamorous/version.rb

This file was deleted.

27 changes: 0 additions & 27 deletions polyamorous/polyamorous.gemspec

This file was deleted.

7 changes: 3 additions & 4 deletions ransack.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 0 additions & 2 deletions spec/ransack/adapters/active_record/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,12 @@ module ActiveRecord
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
Expand Down
17 changes: 0 additions & 17 deletions spec/ransack/adapters/active_record/context_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,23 +110,6 @@ module ActiveRecord
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
Expand Down
24 changes: 15 additions & 9 deletions spec/ransack/search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'machinist/active_record'
require 'polyamorous/polyamorous.rb'
require 'sham'
require 'faker'
require 'ransack'
Expand Down

0 comments on commit ab8c34d

Please sign in to comment.