polyamorous 1.2.0 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +4 -4
- data/README.md +11 -3
- data/lib/polyamorous.rb +2 -9
- data/lib/polyamorous/activerecord_4.1_ruby_1.9/join_dependency.rb +1 -1
- data/lib/polyamorous/activerecord_4.1_ruby_2/join_association.rb +1 -1
- data/lib/polyamorous/activerecord_4.1_ruby_2/join_dependency.rb +1 -1
- data/lib/polyamorous/activerecord_4.1_ruby_2/{make_joins.rb → make_polyamorous_inner_joins.rb} +4 -2
- data/lib/polyamorous/activerecord_4.2_ruby_1.9/join_dependency.rb +7 -28
- data/lib/polyamorous/activerecord_4.2_ruby_2/join_association.rb +1 -36
- data/lib/polyamorous/activerecord_4.2_ruby_2/join_dependency.rb +5 -86
- data/lib/polyamorous/activerecord_5.0_ruby_2/join_association.rb +38 -0
- data/lib/polyamorous/activerecord_5.0_ruby_2/join_dependency.rb +116 -0
- data/lib/polyamorous/version.rb +1 -1
- data/spec/polyamorous/join_association_spec.rb +47 -4
- data/spec/polyamorous/join_dependency_spec.rb +79 -4
- data/spec/polyamorous/join_spec.rb +1 -1
- metadata +6 -12
- data/spec/support/shared_examples/join_association_3_and_4.0.rb +0 -42
- data/spec/support/shared_examples/join_association_4.1.rb +0 -42
- data/spec/support/shared_examples/join_dependency_3_and_4.0.rb +0 -65
- data/spec/support/shared_examples/join_dependency_4.1.rb +0 -65
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 974ca93cd847897179ae0ad4d5a2628bd65a8444
|
4
|
+
data.tar.gz: 7183fb57fe8068011d9add6efd750d9386c87342
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1e8ddde2bf2c992b252712c63c96cac958a59289887650ac6c611481f77a4b7f5f69e7375f010afddb065dbfc955bbefe91b4dc397404978ccb6f10cff278a82
|
7
|
+
data.tar.gz: d193721ba969fd2ff9f08bf227be4a7d9192cb303fa7b3c51c37d762fe363c01f6b7b89d1b08c714cfbd69afec97a8c16f715b0347181ca87734e61fdfdb0da0
|
data/.travis.yml
CHANGED
@@ -6,7 +6,7 @@ before_install:
|
|
6
6
|
- travis_retry gem install bundler
|
7
7
|
|
8
8
|
rvm:
|
9
|
-
- 2.2.
|
9
|
+
- 2.2.3
|
10
10
|
- 2.1
|
11
11
|
- 2.0
|
12
12
|
- 1.9
|
@@ -38,11 +38,11 @@ env:
|
|
38
38
|
|
39
39
|
matrix:
|
40
40
|
include:
|
41
|
-
- rvm: 2.2.
|
41
|
+
- rvm: 2.2.3
|
42
42
|
env: RAILS=master DB=sqlite3
|
43
|
-
- rvm: 2.2.
|
43
|
+
- rvm: 2.2.3
|
44
44
|
env: RAILS=master DB=mysql
|
45
|
-
- rvm: 2.2.
|
45
|
+
- rvm: 2.2.3
|
46
46
|
env: RAILS=master DB=postgres
|
47
47
|
allow_failures:
|
48
48
|
- env: RAILS=master DB=sqlite3
|
data/README.md
CHANGED
@@ -4,15 +4,23 @@
|
|
4
4
|
(https://travis-ci.org/activerecord-hackery/polyamorous)
|
5
5
|
[]
|
6
6
|
(http://badge.fury.io/rb/polyamorous)
|
7
|
+
[]
|
8
|
+
(https://codeclimate.com/github/activerecord-hackery/polyamorous)
|
7
9
|
|
8
|
-
Polyamorous is an extraction
|
10
|
+
Polyamorous is an extraction of shared code from the
|
11
|
+
[Active Record Hackery](https://github.com/activerecord-hackery) gems
|
12
|
+
[Ransack](https://github.com/activerecord-hackery/ransack),
|
13
|
+
[Squeel](https://github.com/activerecord-hackery/squeel) and
|
14
|
+
[MetaSearch](https://github.com/activerecord-hackery/meta_search) by
|
9
15
|
[Ernie Miller](http://twitter.com/erniemiller) and maintained by
|
10
16
|
[Ryan Bigg](http://twitter.com/ryanbigg),
|
11
17
|
[Xiang Li](http://bigxiang.github.io),
|
12
|
-
[Jon Atack](http://twitter.com/jonatack) and a great group of
|
18
|
+
[Jon Atack](http://twitter.com/jonatack) and a great little group of
|
13
19
|
[contributors]
|
14
20
|
(https://github.com/activerecord-hackery/polyamorous/graphs/contributors).
|
15
21
|
|
16
|
-
|
22
|
+
It is an internal library for extending various versions of Active Record with
|
23
|
+
polymorphism. There is no public API, so it's ":nodoc:". Move along. Nothing to
|
24
|
+
see here.
|
17
25
|
|
18
26
|
Copyright © 2011-2015 [Ernie Miller](http://twitter.com/erniemiller)
|
data/lib/polyamorous.rb
CHANGED
@@ -25,15 +25,8 @@ if defined?(::ActiveRecord)
|
|
25
25
|
require 'polyamorous/join'
|
26
26
|
require 'polyamorous/swapping_reflection_class'
|
27
27
|
|
28
|
-
ar_version =
|
29
|
-
|
30
|
-
when '4.2', '5.0'
|
31
|
-
'4.2'
|
32
|
-
when '4.1'
|
33
|
-
'4.1'
|
34
|
-
else
|
35
|
-
'3_and_4.0'
|
36
|
-
end
|
28
|
+
ar_version = ::ActiveRecord::VERSION::STRING[0,3]
|
29
|
+
ar_version = '3_and_4.0' if ar_version < '4.1'
|
37
30
|
|
38
31
|
method, ruby_version =
|
39
32
|
if RUBY_VERSION >= '2.0' && ar_version >= '4.1'
|
@@ -1,4 +1,4 @@
|
|
1
1
|
# active_record_4.1_ruby_1.9/join_dependency.rb
|
2
2
|
require 'polyamorous/activerecord_4.2_ruby_2/join_dependency'
|
3
3
|
require 'polyamorous/activerecord_4.2_ruby_1.9/join_dependency'
|
4
|
-
require 'polyamorous/activerecord_4.1_ruby_2/
|
4
|
+
require 'polyamorous/activerecord_4.1_ruby_2/make_polyamorous_inner_joins'
|
@@ -1,2 +1,2 @@
|
|
1
1
|
# active_record_4.1_ruby_2/join_association.rb
|
2
|
-
require 'polyamorous/
|
2
|
+
require 'polyamorous/activerecord_5.0_ruby_2/join_association'
|
data/lib/polyamorous/activerecord_4.1_ruby_2/{make_joins.rb → make_polyamorous_inner_joins.rb}
RENAMED
@@ -1,11 +1,13 @@
|
|
1
1
|
module Polyamorous
|
2
2
|
module JoinDependencyExtensions
|
3
3
|
# Replaces ActiveRecord::Associations::JoinDependency#make_inner_joins
|
4
|
-
def
|
4
|
+
def make_polyamorous_inner_joins(parent, child)
|
5
5
|
make_constraints(
|
6
6
|
parent, child, child.tables, child.join_type || Arel::Nodes::InnerJoin
|
7
7
|
)
|
8
|
-
.concat child.children.flat_map { |c|
|
8
|
+
.concat child.children.flat_map { |c|
|
9
|
+
make_polyamorous_inner_joins(child, c)
|
10
|
+
}
|
9
11
|
end
|
10
12
|
end
|
11
13
|
end
|
@@ -25,21 +25,12 @@ module Polyamorous
|
|
25
25
|
if name.is_a? Join
|
26
26
|
reflection = find_reflection base_klass, name.name
|
27
27
|
reflection.check_validity!
|
28
|
-
if reflection.polymorphic?
|
29
|
-
|
30
|
-
reflection,
|
31
|
-
build(right, name.klass || base_klass),
|
32
|
-
name.klass,
|
33
|
-
name.type
|
34
|
-
)
|
28
|
+
klass = if reflection.polymorphic?
|
29
|
+
name.klass || base_klass
|
35
30
|
else
|
36
|
-
|
37
|
-
reflection,
|
38
|
-
build(right, reflection.klass),
|
39
|
-
name.klass,
|
40
|
-
name.type
|
41
|
-
)
|
31
|
+
reflection.klass
|
42
32
|
end
|
33
|
+
JoinAssociation.new(reflection, build(right, klass), name.klass, name.type)
|
43
34
|
else
|
44
35
|
reflection = find_reflection base_klass, name
|
45
36
|
reflection.check_validity!
|
@@ -52,10 +43,10 @@ module Polyamorous
|
|
52
43
|
end
|
53
44
|
|
54
45
|
# Replaces ActiveRecord::Associations::JoinDependency#join_constraints
|
55
|
-
# to call #
|
46
|
+
# to call #make_polyamorous_inner_joins instead of #make_inner_joins.
|
56
47
|
def join_constraints_with_polymorphism(outer_joins)
|
57
48
|
joins = join_root.children.flat_map { |child|
|
58
|
-
|
49
|
+
make_polyamorous_inner_joins join_root, child
|
59
50
|
}
|
60
51
|
joins.concat outer_joins.flat_map { |oj|
|
61
52
|
if join_root.match? oj.join_root
|
@@ -71,20 +62,8 @@ module Polyamorous
|
|
71
62
|
module ClassMethods
|
72
63
|
# Replaces ActiveRecord::Associations::JoinDependency#self.walk_tree
|
73
64
|
def walk_tree_with_polymorphism(associations, hash)
|
74
|
-
|
75
|
-
when TreeNode
|
65
|
+
if TreeNode === associations
|
76
66
|
associations.add_to_tree(hash)
|
77
|
-
when Hash
|
78
|
-
associations.each do |k, v|
|
79
|
-
cache =
|
80
|
-
case k
|
81
|
-
when TreeNode
|
82
|
-
k.add_to_tree(hash)
|
83
|
-
else
|
84
|
-
hash[k] ||= {}
|
85
|
-
end
|
86
|
-
walk_tree(v, cache)
|
87
|
-
end
|
88
67
|
else
|
89
68
|
walk_tree_without_polymorphism(associations, hash)
|
90
69
|
end
|
@@ -1,37 +1,2 @@
|
|
1
1
|
# active_record_4.2_ruby_2/join_association.rb
|
2
|
-
|
3
|
-
module JoinAssociationExtensions
|
4
|
-
include SwappingReflectionClass
|
5
|
-
def self.prepended(base)
|
6
|
-
base.class_eval { attr_reader :join_type }
|
7
|
-
end
|
8
|
-
|
9
|
-
def initialize(reflection, children, polymorphic_class = nil,
|
10
|
-
join_type = Arel::Nodes::InnerJoin)
|
11
|
-
@join_type = join_type
|
12
|
-
if polymorphic_class && ::ActiveRecord::Base > polymorphic_class
|
13
|
-
swapping_reflection_klass(reflection, polymorphic_class) do |reflection|
|
14
|
-
super(reflection, children)
|
15
|
-
self.reflection.options[:polymorphic] = true
|
16
|
-
end
|
17
|
-
else
|
18
|
-
super(reflection, children)
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
# Reference https://github.com/rails/rails/commit/9b15db51b78028bfecdb85595624de4b838adbd1
|
23
|
-
# NOTE Not sure we still need it?
|
24
|
-
def ==(other)
|
25
|
-
base_klass == other.base_klass
|
26
|
-
end
|
27
|
-
|
28
|
-
def build_constraint(klass, table, key, foreign_table, foreign_key)
|
29
|
-
if reflection.polymorphic?
|
30
|
-
super(klass, table, key, foreign_table, foreign_key)
|
31
|
-
.and(foreign_table[reflection.foreign_type].eq(reflection.klass.name))
|
32
|
-
else
|
33
|
-
super(klass, table, key, foreign_table, foreign_key)
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
2
|
+
require 'polyamorous/activerecord_5.0_ruby_2/join_association'
|
@@ -1,61 +1,14 @@
|
|
1
1
|
# active_record_4.2_ruby_2/join_dependency.rb
|
2
|
+
require 'polyamorous/activerecord_5.0_ruby_2/join_dependency'
|
3
|
+
|
2
4
|
module Polyamorous
|
3
5
|
module JoinDependencyExtensions
|
4
|
-
# Replaces ActiveRecord::Associations::JoinDependency#build
|
5
|
-
def build(associations, base_klass)
|
6
|
-
associations.map do |name, right|
|
7
|
-
if name.is_a? Join
|
8
|
-
reflection = find_reflection base_klass, name.name
|
9
|
-
reflection.check_validity!
|
10
|
-
if reflection.polymorphic?
|
11
|
-
JoinAssociation.new(
|
12
|
-
reflection,
|
13
|
-
build(right, name.klass || base_klass),
|
14
|
-
name.klass,
|
15
|
-
name.type
|
16
|
-
)
|
17
|
-
else
|
18
|
-
JoinAssociation.new(
|
19
|
-
reflection,
|
20
|
-
build(right, reflection.klass),
|
21
|
-
name.klass,
|
22
|
-
name.type
|
23
|
-
)
|
24
|
-
end
|
25
|
-
else
|
26
|
-
reflection = find_reflection base_klass, name
|
27
|
-
reflection.check_validity!
|
28
|
-
if reflection.polymorphic?
|
29
|
-
raise ActiveRecord::EagerLoadPolymorphicError.new(reflection)
|
30
|
-
end
|
31
|
-
JoinAssociation.new reflection, build(right, reflection.klass)
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
def find_join_association_respecting_polymorphism(reflection, parent, klass)
|
37
|
-
if association = parent.children.find { |j| j.reflection == reflection }
|
38
|
-
unless reflection.polymorphic?
|
39
|
-
association
|
40
|
-
else
|
41
|
-
association if association.base_klass == klass
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
def build_join_association_respecting_polymorphism(reflection, parent, klass)
|
47
|
-
if reflection.polymorphic? && klass
|
48
|
-
JoinAssociation.new(reflection, self, klass)
|
49
|
-
else
|
50
|
-
JoinAssociation.new(reflection, self)
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
6
|
# Replaces ActiveRecord::Associations::JoinDependency#join_constraints
|
55
|
-
# to call #
|
7
|
+
# to call #make_polyamorous_inner_joins instead of #make_inner_joins.
|
8
|
+
#
|
56
9
|
def join_constraints(outer_joins)
|
57
10
|
joins = join_root.children.flat_map { |child|
|
58
|
-
|
11
|
+
make_polyamorous_inner_joins join_root, child
|
59
12
|
}
|
60
13
|
joins.concat outer_joins.flat_map { |oj|
|
61
14
|
if join_root.match? oj.join_root
|
@@ -67,39 +20,5 @@ module Polyamorous
|
|
67
20
|
end
|
68
21
|
}
|
69
22
|
end
|
70
|
-
|
71
|
-
# Replaces ActiveRecord::Associations::JoinDependency#make_inner_joins
|
72
|
-
def make_joins(parent, child)
|
73
|
-
[
|
74
|
-
make_constraints(
|
75
|
-
parent, child, child.tables, child.join_type || Arel::Nodes::InnerJoin
|
76
|
-
)
|
77
|
-
] + child.children.flat_map { |c| make_inner_joins(child, c) }
|
78
|
-
end
|
79
|
-
|
80
|
-
private :make_joins
|
81
|
-
|
82
|
-
module ClassMethods
|
83
|
-
# Prepended before ActiveRecord::Associations::JoinDependency#self.walk_tree
|
84
|
-
def walk_tree(associations, hash)
|
85
|
-
case associations
|
86
|
-
when TreeNode
|
87
|
-
associations.add_to_tree(hash)
|
88
|
-
when Hash
|
89
|
-
associations.each do |k, v|
|
90
|
-
cache =
|
91
|
-
case k
|
92
|
-
when TreeNode
|
93
|
-
k.add_to_tree(hash)
|
94
|
-
else
|
95
|
-
hash[k] ||= {}
|
96
|
-
end
|
97
|
-
walk_tree(v, cache)
|
98
|
-
end
|
99
|
-
else
|
100
|
-
super(associations, hash)
|
101
|
-
end
|
102
|
-
end
|
103
|
-
end
|
104
23
|
end
|
105
24
|
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# active_record_5.0_ruby_2/join_association.rb
|
2
|
+
|
3
|
+
module Polyamorous
|
4
|
+
module JoinAssociationExtensions
|
5
|
+
include SwappingReflectionClass
|
6
|
+
def self.prepended(base)
|
7
|
+
base.class_eval { attr_reader :join_type }
|
8
|
+
end
|
9
|
+
|
10
|
+
def initialize(reflection, children, polymorphic_class = nil,
|
11
|
+
join_type = Arel::Nodes::InnerJoin)
|
12
|
+
@join_type = join_type
|
13
|
+
if polymorphic_class && ::ActiveRecord::Base > polymorphic_class
|
14
|
+
swapping_reflection_klass(reflection, polymorphic_class) do |reflection|
|
15
|
+
super(reflection, children)
|
16
|
+
self.reflection.options[:polymorphic] = true
|
17
|
+
end
|
18
|
+
else
|
19
|
+
super(reflection, children)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
# Reference: https://github.com/rails/rails/commit/9b15db5
|
24
|
+
# NOTE: Not sure we still need it?
|
25
|
+
def ==(other)
|
26
|
+
base_klass == other.base_klass
|
27
|
+
end
|
28
|
+
|
29
|
+
def build_constraint(klass, table, key, foreign_table, foreign_key)
|
30
|
+
if reflection.polymorphic?
|
31
|
+
super(klass, table, key, foreign_table, foreign_key)
|
32
|
+
.and(foreign_table[reflection.foreign_type].eq(reflection.klass.name))
|
33
|
+
else
|
34
|
+
super(klass, table, key, foreign_table, foreign_key)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,116 @@
|
|
1
|
+
# active_record_5.0_ruby_2/join_dependency.rb
|
2
|
+
|
3
|
+
module Polyamorous
|
4
|
+
module JoinDependencyExtensions
|
5
|
+
|
6
|
+
# Replaces ActiveRecord::Associations::JoinDependency#build.
|
7
|
+
#
|
8
|
+
def build(associations, base_klass)
|
9
|
+
associations.map do |name, right|
|
10
|
+
if name.is_a? Join
|
11
|
+
reflection = find_reflection base_klass, name.name
|
12
|
+
reflection.check_validity!
|
13
|
+
klass = if reflection.polymorphic?
|
14
|
+
name.klass || base_klass
|
15
|
+
else
|
16
|
+
reflection.klass
|
17
|
+
end
|
18
|
+
JoinAssociation.new(reflection, build(right, klass), name.klass, name.type)
|
19
|
+
else
|
20
|
+
reflection = find_reflection base_klass, name
|
21
|
+
reflection.check_validity!
|
22
|
+
if reflection.polymorphic?
|
23
|
+
raise ActiveRecord::EagerLoadPolymorphicError.new(reflection)
|
24
|
+
end
|
25
|
+
JoinAssociation.new reflection, build(right, reflection.klass)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def find_join_association_respecting_polymorphism(reflection, parent, klass)
|
31
|
+
if association = parent.children.find { |j| j.reflection == reflection }
|
32
|
+
unless reflection.polymorphic?
|
33
|
+
association
|
34
|
+
else
|
35
|
+
association if association.base_klass == klass
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def build_join_association_respecting_polymorphism(reflection, parent, klass)
|
41
|
+
if reflection.polymorphic? && klass
|
42
|
+
JoinAssociation.new(reflection, self, klass)
|
43
|
+
else
|
44
|
+
JoinAssociation.new(reflection, self)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
# Replaces ActiveRecord::Associations::JoinDependency#join_constraints.
|
49
|
+
#
|
50
|
+
# This internal method was changed in Rails 5.0 by commit
|
51
|
+
# https://github.com/rails/rails/commit/e038975 which added
|
52
|
+
# left_outer_joins (see #make_polyamorous_left_outer_joins below) and added
|
53
|
+
# passing an additional argument, `join_type`, to #join_constraints.
|
54
|
+
#
|
55
|
+
def join_constraints(outer_joins, join_type)
|
56
|
+
joins = join_root.children.flat_map { |child|
|
57
|
+
if join_type == Arel::Nodes::OuterJoin
|
58
|
+
make_polyamorous_left_outer_joins join_root, child
|
59
|
+
else
|
60
|
+
make_polyamorous_inner_joins join_root, child
|
61
|
+
end
|
62
|
+
}
|
63
|
+
|
64
|
+
joins.concat outer_joins.flat_map { |oj|
|
65
|
+
if join_root.match? oj.join_root
|
66
|
+
walk(join_root, oj.join_root)
|
67
|
+
else
|
68
|
+
oj.join_root.children.flat_map { |child|
|
69
|
+
make_outer_joins(oj.join_root, child)
|
70
|
+
}
|
71
|
+
end
|
72
|
+
}
|
73
|
+
end
|
74
|
+
|
75
|
+
# Replaces ActiveRecord::Associations::JoinDependency#make_left_outer_joins,
|
76
|
+
# a new method that was added in Rails 5.0 with the following commit:
|
77
|
+
# https://github.com/rails/rails/commit/e038975
|
78
|
+
#
|
79
|
+
def make_polyamorous_left_outer_joins(parent, child)
|
80
|
+
tables = child.tables
|
81
|
+
join_type = child.join_type || Arel::Nodes::OuterJoin
|
82
|
+
info = make_constraints parent, child, tables, join_type
|
83
|
+
|
84
|
+
[info] + child.children.flat_map { |c|
|
85
|
+
make_polyamorous_left_outer_joins(child, c)
|
86
|
+
}
|
87
|
+
end
|
88
|
+
|
89
|
+
# Replaces ActiveRecord::Associations::JoinDependency#make_inner_joins.
|
90
|
+
#
|
91
|
+
def make_polyamorous_inner_joins(parent, child)
|
92
|
+
tables = child.tables
|
93
|
+
join_type = child.join_type || Arel::Nodes::InnerJoin
|
94
|
+
info = make_constraints parent, child, tables, join_type
|
95
|
+
|
96
|
+
[info] + child.children.flat_map { |c|
|
97
|
+
make_polyamorous_inner_joins(child, c)
|
98
|
+
}
|
99
|
+
end
|
100
|
+
|
101
|
+
private :make_polyamorous_inner_joins, :make_polyamorous_left_outer_joins
|
102
|
+
|
103
|
+
module ClassMethods
|
104
|
+
# Prepended before ActiveRecord::Associations::JoinDependency#walk_tree.
|
105
|
+
#
|
106
|
+
def walk_tree(associations, hash)
|
107
|
+
if TreeNode === associations
|
108
|
+
associations.add_to_tree(hash)
|
109
|
+
else
|
110
|
+
super(associations, hash)
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
end
|
116
|
+
end
|
data/lib/polyamorous/version.rb
CHANGED
@@ -2,10 +2,53 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
module Polyamorous
|
4
4
|
describe JoinAssociation do
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
5
|
+
|
6
|
+
join_base, join_association_args, polymorphic =
|
7
|
+
if ActiveRecord::VERSION::STRING >= '4.1'
|
8
|
+
[:join_root, 'parent.children', 'reflection.options[:polymorphic]']
|
9
|
+
else
|
10
|
+
[:join_base, 'join_dependency, parent', 'options[:polymorphic]']
|
11
|
+
end
|
12
|
+
|
13
|
+
let(:join_dependency) { new_join_dependency Note, {} }
|
14
|
+
let(:reflection) { Note.reflect_on_association(:notable) }
|
15
|
+
let(:parent) { join_dependency.send(join_base) }
|
16
|
+
let(:join_association) {
|
17
|
+
eval("new_join_association(reflection, #{join_association_args}, Article)")
|
18
|
+
}
|
19
|
+
|
20
|
+
subject {
|
21
|
+
join_dependency.build_join_association_respecting_polymorphism(
|
22
|
+
reflection, parent, Person
|
23
|
+
)
|
24
|
+
}
|
25
|
+
|
26
|
+
it 'respects polymorphism on equality test' do
|
27
|
+
expect(subject).to eq(
|
28
|
+
join_dependency.build_join_association_respecting_polymorphism(
|
29
|
+
reflection, parent, Person
|
30
|
+
)
|
31
|
+
)
|
32
|
+
expect(subject).not_to eq(
|
33
|
+
join_dependency.build_join_association_respecting_polymorphism(
|
34
|
+
reflection, parent, Article
|
35
|
+
)
|
36
|
+
)
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'leaves the orginal reflection intact for thread safety' do
|
40
|
+
reflection.instance_variable_set(:@klass, Article)
|
41
|
+
join_association
|
42
|
+
.swapping_reflection_klass(reflection, Person) do |new_reflection|
|
43
|
+
expect(new_reflection.options).not_to equal reflection.options
|
44
|
+
expect(new_reflection.options).not_to have_key(:polymorphic)
|
45
|
+
expect(new_reflection.klass).to eq(Person)
|
46
|
+
expect(reflection.klass).to eq(Article)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'sets the polmorphic option to true after initializing' do
|
51
|
+
expect(join_association.instance_eval(polymorphic)).to be_true
|
9
52
|
end
|
10
53
|
end
|
11
54
|
end
|
@@ -2,10 +2,85 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
module Polyamorous
|
4
4
|
describe JoinDependency do
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
5
|
+
|
6
|
+
method, join_associations, join_base =
|
7
|
+
if ActiveRecord::VERSION::STRING >= '4.1'
|
8
|
+
[:instance_eval, 'join_root.drop(1)', :join_root]
|
9
|
+
else
|
10
|
+
[:send, 'join_associations', :join_base]
|
11
|
+
end
|
12
|
+
|
13
|
+
context 'with symbol joins' do
|
14
|
+
subject { new_join_dependency Person, :articles => :comments }
|
15
|
+
|
16
|
+
specify { expect(subject.send(method, join_associations).size)
|
17
|
+
.to eq 2 }
|
18
|
+
specify { expect(subject.send(method, join_associations).map(&:join_type))
|
19
|
+
.to be_all { Polyamorous::InnerJoin } }
|
20
|
+
end
|
21
|
+
|
22
|
+
context 'with has_many :through association' do
|
23
|
+
subject { new_join_dependency Person, :authored_article_comments }
|
24
|
+
|
25
|
+
specify { expect(subject.send(method, join_associations).size)
|
26
|
+
.to eq 1 }
|
27
|
+
specify { expect(subject.send(method, join_associations).first.table_name)
|
28
|
+
.to eq 'comments' }
|
29
|
+
end
|
30
|
+
|
31
|
+
context 'with outer join' do
|
32
|
+
subject { new_join_dependency Person, new_join(:articles, :outer) }
|
33
|
+
|
34
|
+
specify { expect(subject.send(method, join_associations).size)
|
35
|
+
.to eq 1 }
|
36
|
+
specify { expect(subject.send(method, join_associations).first.join_type)
|
37
|
+
.to eq Polyamorous::OuterJoin }
|
38
|
+
end
|
39
|
+
|
40
|
+
context 'with nested outer joins' do
|
41
|
+
subject { new_join_dependency Person,
|
42
|
+
new_join(:articles, :outer) => new_join(:comments, :outer) }
|
43
|
+
|
44
|
+
specify { expect(subject.send(method, join_associations).size)
|
45
|
+
.to eq 2 }
|
46
|
+
specify { expect(subject.send(method, join_associations).map(&:join_type))
|
47
|
+
.to eq [Polyamorous::OuterJoin, Polyamorous::OuterJoin] }
|
48
|
+
specify { expect(subject.send(method, join_associations).map(&:join_type))
|
49
|
+
.to be_all { Polyamorous::OuterJoin } }
|
50
|
+
end
|
51
|
+
|
52
|
+
context 'with polymorphic belongs_to join' do
|
53
|
+
subject { new_join_dependency Note, new_join(:notable, :inner, Person) }
|
54
|
+
|
55
|
+
specify { expect(subject.send(method, join_associations).size)
|
56
|
+
.to eq 1 }
|
57
|
+
specify { expect(subject.send(method, join_associations).first.join_type)
|
58
|
+
.to eq Polyamorous::InnerJoin }
|
59
|
+
specify { expect(subject.send(method, join_associations).first.table_name)
|
60
|
+
.to eq 'people' }
|
61
|
+
|
62
|
+
it 'finds a join association respecting polymorphism' do
|
63
|
+
parent = subject.send(join_base)
|
64
|
+
reflection = Note.reflect_on_association(:notable)
|
65
|
+
|
66
|
+
expect(subject.find_join_association_respecting_polymorphism(
|
67
|
+
reflection, parent, Person))
|
68
|
+
.to eq subject.send(method, join_associations).first
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
context 'with polymorphic belongs_to join and nested symbol join' do
|
73
|
+
subject { new_join_dependency Note,
|
74
|
+
new_join(:notable, :inner, Person) => :comments }
|
75
|
+
|
76
|
+
specify { expect(subject.send(method, join_associations).size)
|
77
|
+
.to eq 2 }
|
78
|
+
specify { expect(subject.send(method, join_associations).map(&:join_type))
|
79
|
+
.to be_all { Polyamorous::InnerJoin } }
|
80
|
+
specify { expect(subject.send(method, join_associations).first.table_name)
|
81
|
+
.to eq 'people' }
|
82
|
+
specify { expect(subject.send(method, join_associations)[1].table_name)
|
83
|
+
.to eq 'comments' }
|
9
84
|
end
|
10
85
|
end
|
11
86
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: polyamorous
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ernie Miller
|
@@ -11,7 +11,7 @@ authors:
|
|
11
11
|
autorequire:
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
|
-
date: 2015-
|
14
|
+
date: 2015-12-09 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: activerecord
|
@@ -108,11 +108,13 @@ files:
|
|
108
108
|
- lib/polyamorous/activerecord_4.1_ruby_1.9/join_dependency.rb
|
109
109
|
- lib/polyamorous/activerecord_4.1_ruby_2/join_association.rb
|
110
110
|
- lib/polyamorous/activerecord_4.1_ruby_2/join_dependency.rb
|
111
|
-
- lib/polyamorous/activerecord_4.1_ruby_2/
|
111
|
+
- lib/polyamorous/activerecord_4.1_ruby_2/make_polyamorous_inner_joins.rb
|
112
112
|
- lib/polyamorous/activerecord_4.2_ruby_1.9/join_association.rb
|
113
113
|
- lib/polyamorous/activerecord_4.2_ruby_1.9/join_dependency.rb
|
114
114
|
- lib/polyamorous/activerecord_4.2_ruby_2/join_association.rb
|
115
115
|
- lib/polyamorous/activerecord_4.2_ruby_2/join_dependency.rb
|
116
|
+
- lib/polyamorous/activerecord_5.0_ruby_2/join_association.rb
|
117
|
+
- lib/polyamorous/activerecord_5.0_ruby_2/join_dependency.rb
|
116
118
|
- lib/polyamorous/join.rb
|
117
119
|
- lib/polyamorous/swapping_reflection_class.rb
|
118
120
|
- lib/polyamorous/tree_node.rb
|
@@ -129,10 +131,6 @@ files:
|
|
129
131
|
- spec/polyamorous/join_spec.rb
|
130
132
|
- spec/spec_helper.rb
|
131
133
|
- spec/support/schema.rb
|
132
|
-
- spec/support/shared_examples/join_association_3_and_4.0.rb
|
133
|
-
- spec/support/shared_examples/join_association_4.1.rb
|
134
|
-
- spec/support/shared_examples/join_dependency_3_and_4.0.rb
|
135
|
-
- spec/support/shared_examples/join_dependency_4.1.rb
|
136
134
|
homepage: https://github.com/activerecord-hackery/polyamorous
|
137
135
|
licenses:
|
138
136
|
- MIT
|
@@ -153,7 +151,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
153
151
|
version: '0'
|
154
152
|
requirements: []
|
155
153
|
rubyforge_project: polyamorous
|
156
|
-
rubygems_version: 2.
|
154
|
+
rubygems_version: 2.5.0
|
157
155
|
signing_key:
|
158
156
|
specification_version: 4
|
159
157
|
summary: Loves/is loved by polymorphic belongs_to associations, Ransack, Squeel, MetaSearch...
|
@@ -169,7 +167,3 @@ test_files:
|
|
169
167
|
- spec/polyamorous/join_spec.rb
|
170
168
|
- spec/spec_helper.rb
|
171
169
|
- spec/support/schema.rb
|
172
|
-
- spec/support/shared_examples/join_association_3_and_4.0.rb
|
173
|
-
- spec/support/shared_examples/join_association_4.1.rb
|
174
|
-
- spec/support/shared_examples/join_dependency_3_and_4.0.rb
|
175
|
-
- spec/support/shared_examples/join_dependency_4.1.rb
|
@@ -1,42 +0,0 @@
|
|
1
|
-
shared_examples 'Join Association on ActiveRecord 3 and 4.0' do
|
2
|
-
let(:join_dependency) { new_join_dependency Note, {} }
|
3
|
-
let(:reflection) { Note.reflect_on_association(:notable) }
|
4
|
-
let(:parent) { join_dependency.join_base }
|
5
|
-
let(:join_association) {
|
6
|
-
new_join_association(reflection, join_dependency, parent, Article)
|
7
|
-
}
|
8
|
-
|
9
|
-
subject {
|
10
|
-
join_dependency.build_join_association_respecting_polymorphism(
|
11
|
-
reflection, parent, Person
|
12
|
-
)
|
13
|
-
}
|
14
|
-
|
15
|
-
it 'respects polymorphism on equality test' do
|
16
|
-
expect(subject).to eq(
|
17
|
-
join_dependency.build_join_association_respecting_polymorphism(
|
18
|
-
reflection, parent, Person
|
19
|
-
)
|
20
|
-
)
|
21
|
-
expect(subject).not_to eq(
|
22
|
-
join_dependency.build_join_association_respecting_polymorphism(
|
23
|
-
reflection, parent, Article
|
24
|
-
)
|
25
|
-
)
|
26
|
-
end
|
27
|
-
|
28
|
-
it 'leaves the orginal reflection intact for thread safety' do
|
29
|
-
reflection.instance_variable_set(:@klass, Article)
|
30
|
-
join_association
|
31
|
-
.swapping_reflection_klass(reflection, Person) do |new_reflection|
|
32
|
-
expect(new_reflection.options).not_to equal reflection.options
|
33
|
-
expect(new_reflection.options).not_to have_key(:polymorphic)
|
34
|
-
expect(new_reflection.klass).to eq(Person)
|
35
|
-
expect(reflection.klass).to eq(Article)
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
it 'sets the polmorphic option to true after initializing' do
|
40
|
-
expect(join_association.options[:polymorphic]).to be_true
|
41
|
-
end
|
42
|
-
end
|
@@ -1,42 +0,0 @@
|
|
1
|
-
shared_examples 'Join Association on ActiveRecord 4.1' do
|
2
|
-
let(:join_dependency) { new_join_dependency Note, {} }
|
3
|
-
let(:reflection) { Note.reflect_on_association(:notable) }
|
4
|
-
let(:parent) { join_dependency.join_root }
|
5
|
-
let(:join_association) {
|
6
|
-
new_join_association(reflection, parent.children, Article)
|
7
|
-
}
|
8
|
-
|
9
|
-
subject {
|
10
|
-
join_dependency.build_join_association_respecting_polymorphism(
|
11
|
-
reflection, parent, Person
|
12
|
-
)
|
13
|
-
}
|
14
|
-
|
15
|
-
it 'respects polymorphism on equality test' do
|
16
|
-
expect(subject).to eq(
|
17
|
-
join_dependency.build_join_association_respecting_polymorphism(
|
18
|
-
reflection, parent, Person
|
19
|
-
)
|
20
|
-
)
|
21
|
-
expect(subject).not_to eq(
|
22
|
-
join_dependency.build_join_association_respecting_polymorphism(
|
23
|
-
reflection, parent, Article
|
24
|
-
)
|
25
|
-
)
|
26
|
-
end
|
27
|
-
|
28
|
-
it 'leaves the orginal reflection intact for thread safety' do
|
29
|
-
reflection.instance_variable_set(:@klass, Article)
|
30
|
-
join_association
|
31
|
-
.swapping_reflection_klass(reflection, Person) do |new_reflection|
|
32
|
-
expect(new_reflection.options).not_to equal reflection.options
|
33
|
-
expect(new_reflection.options).not_to have_key(:polymorphic)
|
34
|
-
expect(new_reflection.klass).to eq(Person)
|
35
|
-
expect(reflection.klass).to eq(Article)
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
it 'sets the polmorphic option to true after initializing' do
|
40
|
-
expect(join_association.reflection.options[:polymorphic]).to be_true
|
41
|
-
end
|
42
|
-
end
|
@@ -1,65 +0,0 @@
|
|
1
|
-
shared_examples "Join Dependency on ActiveRecord 3 and 4.0" do
|
2
|
-
context 'with symbol joins' do
|
3
|
-
subject { new_join_dependency Person, :articles => :comments }
|
4
|
-
|
5
|
-
specify { expect(subject.join_associations.size).to eq(2) }
|
6
|
-
specify { expect(subject.join_associations)
|
7
|
-
.to be_all { |a| a.join_type == Polyamorous::InnerJoin } }
|
8
|
-
end
|
9
|
-
|
10
|
-
context 'with has_many :through association' do
|
11
|
-
subject { new_join_dependency Person, :authored_article_comments }
|
12
|
-
|
13
|
-
specify { expect(subject.join_associations.size).to eq(1) }
|
14
|
-
specify { expect(subject.join_associations.first.table_name)
|
15
|
-
.to eq 'comments' }
|
16
|
-
end
|
17
|
-
|
18
|
-
context 'with outer join' do
|
19
|
-
subject { new_join_dependency Person, new_join(:articles, :outer) }
|
20
|
-
|
21
|
-
specify { expect(subject.join_associations.size).to eq(1) }
|
22
|
-
specify { expect(subject.join_associations)
|
23
|
-
.to be_all { |a| a.join_type == Polyamorous::OuterJoin } }
|
24
|
-
end
|
25
|
-
|
26
|
-
context 'with nested outer joins' do
|
27
|
-
subject { new_join_dependency Person,
|
28
|
-
new_join(:articles, :outer) => new_join(:comments, :outer) }
|
29
|
-
|
30
|
-
specify { expect(subject.join_associations.size).to eq(2) }
|
31
|
-
specify { expect(subject.join_associations)
|
32
|
-
.to be_all { |a| a.join_type == Polyamorous::OuterJoin } }
|
33
|
-
end
|
34
|
-
|
35
|
-
context 'with polymorphic belongs_to join' do
|
36
|
-
subject { new_join_dependency Note, new_join(:notable, :inner, Person) }
|
37
|
-
|
38
|
-
specify { expect(subject.join_associations.size).to eq(1) }
|
39
|
-
specify { expect(subject.join_associations)
|
40
|
-
.to be_all { |a| a.join_type == Polyamorous::InnerJoin } }
|
41
|
-
specify { expect(subject.join_associations.first.table_name)
|
42
|
-
.to eq 'people' }
|
43
|
-
|
44
|
-
it 'finds a join association respecting polymorphism' do
|
45
|
-
parent = subject.join_base
|
46
|
-
reflection = Note.reflect_on_association(:notable)
|
47
|
-
expect(subject.find_join_association_respecting_polymorphism(
|
48
|
-
reflection, parent, Person
|
49
|
-
)).to eq subject.join_associations.first
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
context 'with polymorphic belongs_to join and nested symbol join' do
|
54
|
-
subject { new_join_dependency Note,
|
55
|
-
new_join(:notable, :inner, Person) => :comments }
|
56
|
-
|
57
|
-
specify { expect(subject.join_associations.size).to eq(2) }
|
58
|
-
specify { expect(subject.join_associations)
|
59
|
-
.to be_all { |a| a.join_type == Polyamorous::InnerJoin } }
|
60
|
-
specify { expect(subject.join_associations.first.table_name)
|
61
|
-
.to eq 'people' }
|
62
|
-
specify { expect(subject.join_associations[1].table_name)
|
63
|
-
.to eq 'comments' }
|
64
|
-
end
|
65
|
-
end
|
@@ -1,65 +0,0 @@
|
|
1
|
-
shared_examples "Join Dependency on ActiveRecord 4.1" do
|
2
|
-
context 'with symbol joins' do
|
3
|
-
subject { new_join_dependency Person, :articles => :comments }
|
4
|
-
|
5
|
-
specify { expect(subject.join_root.drop(1).size).to eq(2) }
|
6
|
-
specify { expect(subject.join_root.drop(1).map(&:join_type))
|
7
|
-
.to be_all { Polyamorous::InnerJoin } }
|
8
|
-
end
|
9
|
-
|
10
|
-
context 'with has_many :through association' do
|
11
|
-
subject { new_join_dependency Person, :authored_article_comments }
|
12
|
-
|
13
|
-
specify { expect(subject.join_root.drop(1).size).to eq(1) }
|
14
|
-
specify { expect(subject.join_root.drop(1).first.table_name)
|
15
|
-
.to eq 'comments' }
|
16
|
-
end
|
17
|
-
|
18
|
-
context 'with outer join' do
|
19
|
-
subject { new_join_dependency Person, new_join(:articles, :outer) }
|
20
|
-
|
21
|
-
specify { expect(subject.join_root.drop(1).size).to eq(1) }
|
22
|
-
specify { expect(subject.join_root.drop(1).first.join_type)
|
23
|
-
.to eq Polyamorous::OuterJoin }
|
24
|
-
end
|
25
|
-
|
26
|
-
context 'with nested outer joins' do
|
27
|
-
subject { new_join_dependency Person,
|
28
|
-
new_join(:articles, :outer) => new_join(:comments, :outer) }
|
29
|
-
|
30
|
-
specify { expect(subject.join_root.drop(1).size).to eq(2) }
|
31
|
-
specify { expect(subject.join_root.drop(1).map(&:join_type))
|
32
|
-
.to be_all { Polyamorous::OuterJoin } }
|
33
|
-
end
|
34
|
-
|
35
|
-
context 'with polymorphic belongs_to join' do
|
36
|
-
subject { new_join_dependency Note, new_join(:notable, :inner, Person) }
|
37
|
-
|
38
|
-
specify { expect(subject.join_root.drop(1).size).to eq(1) }
|
39
|
-
specify { expect(subject.join_root.drop(1).first.join_type)
|
40
|
-
.to eq Polyamorous::InnerJoin }
|
41
|
-
specify { expect(subject.join_root.drop(1).first.table_name)
|
42
|
-
.to eq 'people' }
|
43
|
-
|
44
|
-
it 'finds a join association respecting polymorphism' do
|
45
|
-
parent = subject.join_root
|
46
|
-
reflection = Note.reflect_on_association(:notable)
|
47
|
-
expect(subject.find_join_association_respecting_polymorphism(
|
48
|
-
reflection, parent, Person
|
49
|
-
)).to eq subject.join_root.drop(1).first
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
context 'with polymorphic belongs_to join and nested symbol join' do
|
54
|
-
subject { new_join_dependency Note,
|
55
|
-
new_join(:notable, :inner, Person) => :comments }
|
56
|
-
|
57
|
-
specify { expect(subject.join_root.drop(1).size).to eq(2) }
|
58
|
-
specify { expect(subject.join_root.drop(1).map(&:join_type))
|
59
|
-
.to be_all { Polyamorous::InnerJoin } }
|
60
|
-
specify { expect(subject.join_root.drop(1).first.table_name)
|
61
|
-
.to eq 'people' }
|
62
|
-
specify { expect(subject.join_root.drop(1)[1].table_name)
|
63
|
-
.to eq 'comments' }
|
64
|
-
end
|
65
|
-
end
|