polyamorous 1.3.3 → 2.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +5 -5
  2. data/lib/polyamorous.rb +13 -40
  3. data/lib/polyamorous/activerecord_5.1_ruby_2/join_association.rb +1 -9
  4. data/lib/polyamorous/activerecord_5.1_ruby_2/join_dependency.rb +2 -20
  5. data/lib/polyamorous/{activerecord_5.2_ruby_2 → activerecord_5.2.0_ruby_2}/join_association.rb +2 -10
  6. data/lib/polyamorous/{activerecord_5.2_ruby_2 → activerecord_5.2.0_ruby_2}/join_dependency.rb +3 -21
  7. data/lib/polyamorous/activerecord_5.2.0_ruby_2/reflection.rb +12 -0
  8. data/lib/polyamorous/activerecord_5.2.1_ruby_2/join_association.rb +22 -0
  9. data/lib/polyamorous/activerecord_5.2.1_ruby_2/join_dependency.rb +81 -0
  10. data/lib/polyamorous/activerecord_5.2.1_ruby_2/reflection.rb +2 -0
  11. data/lib/polyamorous/activerecord_6.0_ruby_2/join_association.rb +2 -0
  12. data/lib/polyamorous/activerecord_6.0_ruby_2/join_dependency.rb +81 -0
  13. data/lib/polyamorous/activerecord_6.0_ruby_2/reflection.rb +2 -0
  14. data/lib/polyamorous/activerecord_6.1_ruby_2/join_association.rb +2 -0
  15. data/lib/polyamorous/activerecord_6.1_ruby_2/join_dependency.rb +2 -0
  16. data/lib/polyamorous/activerecord_6.1_ruby_2/reflection.rb +2 -0
  17. data/lib/polyamorous/version.rb +1 -1
  18. data/polyamorous.gemspec +2 -4
  19. metadata +19 -49
  20. data/.gitignore +0 -6
  21. data/.travis.yml +0 -59
  22. data/Gemfile +0 -33
  23. data/LICENSE +0 -20
  24. data/README.md +0 -21
  25. data/Rakefile +0 -17
  26. data/lib/polyamorous/activerecord_3_and_4.0_ruby_1.9/join_association.rb +0 -76
  27. data/lib/polyamorous/activerecord_3_and_4.0_ruby_1.9/join_dependency.rb +0 -96
  28. data/lib/polyamorous/activerecord_4.1_ruby_1.9/join_association.rb +0 -2
  29. data/lib/polyamorous/activerecord_4.1_ruby_1.9/join_dependency.rb +0 -4
  30. data/lib/polyamorous/activerecord_4.1_ruby_2/join_association.rb +0 -2
  31. data/lib/polyamorous/activerecord_4.1_ruby_2/join_dependency.rb +0 -3
  32. data/lib/polyamorous/activerecord_4.1_ruby_2/make_polyamorous_inner_joins.rb +0 -14
  33. data/lib/polyamorous/activerecord_4.2_ruby_1.9/join_association.rb +0 -46
  34. data/lib/polyamorous/activerecord_4.2_ruby_1.9/join_dependency.rb +0 -87
  35. data/lib/polyamorous/activerecord_4.2_ruby_2/join_association.rb +0 -2
  36. data/lib/polyamorous/activerecord_4.2_ruby_2/join_dependency.rb +0 -24
  37. data/spec/blueprints/articles.rb +0 -5
  38. data/spec/blueprints/comments.rb +0 -5
  39. data/spec/blueprints/notes.rb +0 -3
  40. data/spec/blueprints/people.rb +0 -4
  41. data/spec/blueprints/tags.rb +0 -3
  42. data/spec/helpers/polyamorous_helper.rb +0 -26
  43. data/spec/polyamorous/join_association_spec.rb +0 -54
  44. data/spec/polyamorous/join_dependency_spec.rb +0 -102
  45. data/spec/polyamorous/join_spec.rb +0 -19
  46. data/spec/spec_helper.rb +0 -43
  47. data/spec/support/schema.rb +0 -98
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: c99b340d1137da69048738077239bf10471388fe
4
- data.tar.gz: 289568abe8acfc3dd93b8c3fb45f7a6910fd1752
2
+ SHA256:
3
+ metadata.gz: e492b2c26e7349c7319dcbeef6873fc5a86e2a8c6eb8e9fa7d0a9a7797f3bf1d
4
+ data.tar.gz: 46334d1ae1c9223ee8bb100785a91e0601faaf653b7c43ae7be1c6740731974b
5
5
  SHA512:
6
- metadata.gz: 0f035d8d3d7ec83992e5eb63f2bd608a7131c1104ad86b5320d7eb953d7e6f8159bf4458636cb03ca0506e18c1bba83f996ede2772bbd11671eec38b251314b0
7
- data.tar.gz: 7186a137a3299ce00fdf66526e5a71763cbd99737811e48f0285b2dd2ea1b1bc0952fbf1cb750c24e26393708071e3f753a794022261871e88d1a6958edd0fe6
6
+ metadata.gz: 4046a135237cd578403749be629af46ecec8e14841f930dc0524dbec9906219ec8d45e681e402a0fe874c632f03babca710f7f993db7afdfd85ad5f2882b2bf6
7
+ data.tar.gz: 1f6bb027ca8d1c1b2e90586c05b59282f0355422b550f33436b98deb1738fd7c5a17370560f087f7f4cd6e85d4faebcc7221375886f89b29d7936364722c3859
@@ -1,24 +1,10 @@
1
- require 'polyamorous/version'
2
-
3
1
  if defined?(::ActiveRecord)
4
2
  module Polyamorous
5
- if defined?(Arel::InnerJoin)
6
- InnerJoin = Arel::InnerJoin
7
- OuterJoin = Arel::OuterJoin
8
- else
9
- InnerJoin = Arel::Nodes::InnerJoin
10
- OuterJoin = Arel::Nodes::OuterJoin
11
- end
3
+ InnerJoin = Arel::Nodes::InnerJoin
4
+ OuterJoin = Arel::Nodes::OuterJoin
12
5
 
13
- if defined?(::ActiveRecord::Associations::JoinDependency)
14
- JoinDependency = ::ActiveRecord::Associations::JoinDependency
15
- JoinAssociation = ::ActiveRecord::Associations::JoinDependency::JoinAssociation
16
- JoinBase = ::ActiveRecord::Associations::JoinDependency::JoinBase
17
- else
18
- JoinDependency = ::ActiveRecord::Associations::ClassMethods::JoinDependency
19
- JoinAssociation = ::ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation
20
- JoinBase = ::ActiveRecord::Associations::ClassMethods::JoinDependency::JoinBase
21
- end
6
+ JoinDependency = ::ActiveRecord::Associations::JoinDependency
7
+ JoinAssociation = ::ActiveRecord::Associations::JoinDependency::JoinAssociation
22
8
  end
23
9
 
24
10
  require 'polyamorous/tree_node'
@@ -26,31 +12,18 @@ if defined?(::ActiveRecord)
26
12
  require 'polyamorous/swapping_reflection_class'
27
13
 
28
14
  ar_version = ::ActiveRecord::VERSION::STRING[0,3]
29
- ar_version = '3_and_4.0' if ar_version < '4.1'
30
-
31
- method, ruby_version =
32
- if RUBY_VERSION >= '2.0' && ar_version >= '4.1'
33
- # Ruby 2; we can use `prepend` to patch Active Record cleanly.
34
- [:prepend, '2']
35
- else
36
- # Ruby 1.9; we must use `alias_method` to patch Active Record.
37
- [:include, '1.9']
38
- end
39
-
15
+ ar_version = ::ActiveRecord::VERSION::STRING[0,5] if ar_version >= "5.2" && ::ActiveRecord.version < ::Gem::Version.new("6.0")
16
+ ar_version = "5.2.1" if ::ActiveRecord::VERSION::STRING >= "5.2.1" && ::ActiveRecord.version < ::Gem::Version.new("6.0")
40
17
  %w(join_association join_dependency).each do |file|
41
- require "polyamorous/activerecord_#{ar_version}_ruby_#{ruby_version}/#{file}"
18
+ require "polyamorous/activerecord_#{ar_version}_ruby_2/#{file}"
42
19
  end
43
20
 
44
- Polyamorous::JoinDependency.send(method, Polyamorous::JoinDependencyExtensions)
45
- if method == :prepend
46
- Polyamorous::JoinDependency.singleton_class
47
- .send(:prepend, Polyamorous::JoinDependencyExtensions::ClassMethods)
21
+ if ar_version >= "5.2.0"
22
+ require "polyamorous/activerecord_#{ar_version}_ruby_2/reflection.rb"
23
+ ::ActiveRecord::Reflection::AbstractReflection.send(:prepend, Polyamorous::ReflectionExtensions)
48
24
  end
49
- Polyamorous::JoinAssociation.send(method, Polyamorous::JoinAssociationExtensions)
50
25
 
51
- Polyamorous::JoinBase.class_eval do
52
- if method_defined?(:active_record)
53
- alias_method :base_klass, :active_record
54
- end
55
- end
26
+ Polyamorous::JoinDependency.send(:prepend, Polyamorous::JoinDependencyExtensions)
27
+ Polyamorous::JoinDependency.singleton_class.send(:prepend, Polyamorous::JoinDependencyExtensions::ClassMethods)
28
+ Polyamorous::JoinAssociation.send(:prepend, Polyamorous::JoinAssociationExtensions)
56
29
  end
@@ -7,8 +7,7 @@ module Polyamorous
7
7
  base.class_eval { attr_reader :join_type }
8
8
  end
9
9
 
10
- def initialize(reflection, children, polymorphic_class = nil,
11
- join_type = Arel::Nodes::InnerJoin)
10
+ def initialize(reflection, children, polymorphic_class = nil, join_type = Arel::Nodes::InnerJoin)
12
11
  @join_type = join_type
13
12
  if polymorphic_class && ::ActiveRecord::Base > polymorphic_class
14
13
  swapping_reflection_klass(reflection, polymorphic_class) do |reflection|
@@ -20,13 +19,6 @@ module Polyamorous
20
19
  end
21
20
  end
22
21
 
23
- # Reference: https://github.com/rails/rails/commit/9b15db5
24
- # NOTE: Not sure we still need it?
25
- #
26
- def ==(other)
27
- base_klass == other.base_klass
28
- end
29
-
30
22
  def build_constraint(klass, table, key, foreign_table, foreign_key)
31
23
  if reflection.polymorphic?
32
24
  super(klass, table, key, foreign_table, foreign_key)
@@ -9,7 +9,7 @@ module Polyamorous
9
9
  if name.is_a? Join
10
10
  reflection = find_reflection base_klass, name.name
11
11
  reflection.check_validity!
12
- reflection.check_eager_loadable! if ActiveRecord::VERSION::MAJOR >= 5
12
+ reflection.check_eager_loadable!
13
13
 
14
14
  klass = if reflection.polymorphic?
15
15
  name.klass || base_klass
@@ -20,7 +20,7 @@ module Polyamorous
20
20
  else
21
21
  reflection = find_reflection base_klass, name
22
22
  reflection.check_validity!
23
- reflection.check_eager_loadable! if ActiveRecord::VERSION::MAJOR >= 5
23
+ reflection.check_eager_loadable!
24
24
 
25
25
  if reflection.polymorphic?
26
26
  raise ActiveRecord::EagerLoadPolymorphicError.new(reflection)
@@ -30,24 +30,6 @@ module Polyamorous
30
30
  end
31
31
  end
32
32
 
33
- def find_join_association_respecting_polymorphism(reflection, parent, klass)
34
- if association = parent.children.find { |j| j.reflection == reflection }
35
- unless reflection.polymorphic?
36
- association
37
- else
38
- association if association.base_klass == klass
39
- end
40
- end
41
- end
42
-
43
- def build_join_association_respecting_polymorphism(reflection, parent, klass)
44
- if reflection.polymorphic? && klass
45
- JoinAssociation.new(reflection, self, klass)
46
- else
47
- JoinAssociation.new(reflection, self)
48
- end
49
- end
50
-
51
33
  # Replaces ActiveRecord::Associations::JoinDependency#join_constraints
52
34
  #
53
35
  # This internal method was changed in Rails 5.0 by commit
@@ -7,8 +7,7 @@ module Polyamorous
7
7
  base.class_eval { attr_reader :join_type }
8
8
  end
9
9
 
10
- def initialize(reflection, children, alias_tracker, polymorphic_class = nil,
11
- join_type = Arel::Nodes::InnerJoin)
10
+ def initialize(reflection, children, alias_tracker, polymorphic_class = nil, join_type = Arel::Nodes::InnerJoin)
12
11
  @join_type = join_type
13
12
  if polymorphic_class && ::ActiveRecord::Base > polymorphic_class
14
13
  swapping_reflection_klass(reflection, polymorphic_class) do |reflection|
@@ -20,17 +19,10 @@ module Polyamorous
20
19
  end
21
20
  end
22
21
 
23
- # Reference: https://github.com/rails/rails/commit/9b15db5
24
- # NOTE: Not sure we still need it?
25
- #
26
- def ==(other)
27
- base_klass == other.base_klass
28
- end
29
-
30
22
  def build_constraint(klass, table, key, foreign_table, foreign_key)
31
23
  if reflection.polymorphic?
32
24
  super(klass, table, key, foreign_table, foreign_key)
33
- .and(foreign_table[reflection.foreign_type].eq(reflection.klass.name))
25
+ .and(foreign_table[reflection.foreign_type].eq(reflection.klass.name))
34
26
  else
35
27
  super(klass, table, key, foreign_table, foreign_key)
36
28
  end
@@ -9,7 +9,7 @@ module Polyamorous
9
9
  if name.is_a? Join
10
10
  reflection = find_reflection base_klass, name.name
11
11
  reflection.check_validity!
12
- reflection.check_eager_loadable! if ActiveRecord::VERSION::MAJOR >= 5
12
+ reflection.check_eager_loadable!
13
13
 
14
14
  klass = if reflection.polymorphic?
15
15
  name.klass || base_klass
@@ -20,7 +20,7 @@ module Polyamorous
20
20
  else
21
21
  reflection = find_reflection base_klass, name
22
22
  reflection.check_validity!
23
- reflection.check_eager_loadable! if ActiveRecord::VERSION::MAJOR >= 5
23
+ reflection.check_eager_loadable!
24
24
 
25
25
  if reflection.polymorphic?
26
26
  raise ActiveRecord::EagerLoadPolymorphicError.new(reflection)
@@ -30,24 +30,6 @@ module Polyamorous
30
30
  end
31
31
  end
32
32
 
33
- def find_join_association_respecting_polymorphism(reflection, parent, klass)
34
- if association = parent.children.find { |j| j.reflection == reflection }
35
- unless reflection.polymorphic?
36
- association
37
- else
38
- association if association.base_klass == klass
39
- end
40
- end
41
- end
42
-
43
- def build_join_association_respecting_polymorphism(reflection, parent, klass)
44
- if reflection.polymorphic? && klass
45
- JoinAssociation.new(reflection, self, alias_tracker, klass)
46
- else
47
- JoinAssociation.new(reflection, self, alias_tracker)
48
- end
49
- end
50
-
51
33
  # Replaces ActiveRecord::Associations::JoinDependency#join_constraints
52
34
  #
53
35
  # This internal method was changed in Rails 5.0 by commit
@@ -65,7 +47,7 @@ module Polyamorous
65
47
  }
66
48
 
67
49
  joins.concat outer_joins.flat_map { |oj|
68
- if join_root.match? oj.join_root
50
+ if join_root.match?(oj.join_root) && join_root.table.name == oj.join_root.table.name
69
51
  walk(join_root, oj.join_root)
70
52
  else
71
53
  oj.join_root.children.flat_map { |child|
@@ -0,0 +1,12 @@
1
+ module Polyamorous
2
+ module ReflectionExtensions
3
+ def build_join_constraint(table, foreign_table)
4
+ if polymorphic?
5
+ super(table, foreign_table)
6
+ .and(foreign_table[foreign_type].eq(klass.name))
7
+ else
8
+ super(table, foreign_table)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,22 @@
1
+ # active_record_5.2.1_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, 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
+ end
22
+ end
@@ -0,0 +1,81 @@
1
+ # active_record_5.2.1_ruby_2/join_dependency.rb
2
+
3
+ module Polyamorous
4
+ module JoinDependencyExtensions
5
+ # Replaces ActiveRecord::Associations::JoinDependency#build
6
+ def build(associations, base_klass)
7
+ associations.map do |name, right|
8
+ if name.is_a? Join
9
+ reflection = find_reflection base_klass, name.name
10
+ reflection.check_validity!
11
+ reflection.check_eager_loadable!
12
+
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
+ reflection.check_eager_loadable!
23
+
24
+ if reflection.polymorphic?
25
+ raise ActiveRecord::EagerLoadPolymorphicError.new(reflection)
26
+ end
27
+ JoinAssociation.new(reflection, build(right, reflection.klass))
28
+ end
29
+ end
30
+ end
31
+
32
+ def join_constraints(joins_to_add, join_type, alias_tracker)
33
+ @alias_tracker = alias_tracker
34
+
35
+ construct_tables!(join_root)
36
+ joins = make_join_constraints(join_root, join_type)
37
+
38
+ joins.concat joins_to_add.flat_map { |oj|
39
+ construct_tables!(oj.join_root)
40
+ if join_root.match?(oj.join_root) && join_root.table.name == oj.join_root.table.name
41
+ walk join_root, oj.join_root
42
+ else
43
+ make_join_constraints(oj.join_root, join_type)
44
+ end
45
+ }
46
+ end
47
+
48
+ private
49
+ def make_constraints(parent, child, join_type = Arel::Nodes::OuterJoin)
50
+ foreign_table = parent.table
51
+ foreign_klass = parent.base_klass
52
+ join_type = child.join_type || join_type if join_type == Arel::Nodes::InnerJoin
53
+ joins = child.join_constraints(foreign_table, foreign_klass, join_type, alias_tracker)
54
+ joins.concat child.children.flat_map { |c| make_constraints(child, c, join_type) }
55
+ end
56
+
57
+ module ClassMethods
58
+ # Prepended before ActiveRecord::Associations::JoinDependency#walk_tree
59
+ #
60
+ def walk_tree(associations, hash)
61
+ case associations
62
+ when TreeNode
63
+ associations.add_to_tree(hash)
64
+ when Hash
65
+ associations.each do |k, v|
66
+ cache =
67
+ if TreeNode === k
68
+ k.add_to_tree(hash)
69
+ else
70
+ hash[k] ||= {}
71
+ end
72
+ walk_tree(v, cache)
73
+ end
74
+ else
75
+ super(associations, hash)
76
+ end
77
+ end
78
+ end
79
+
80
+ end
81
+ end
@@ -0,0 +1,2 @@
1
+ # active_record_5.2.1_ruby_2/reflection.rb
2
+ require 'polyamorous/activerecord_5.2.0_ruby_2/reflection'
@@ -0,0 +1,2 @@
1
+ # active_record_6.0_ruby_2/join_association
2
+ require 'polyamorous/activerecord_5.2.1_ruby_2/join_association'
@@ -0,0 +1,81 @@
1
+ # active_record_6.0_ruby_2/join_dependency.rb
2
+
3
+ module Polyamorous
4
+ module JoinDependencyExtensions
5
+ # Replaces ActiveRecord::Associations::JoinDependency#build
6
+ def build(associations, base_klass)
7
+ associations.map do |name, right|
8
+ if name.is_a? Join
9
+ reflection = find_reflection base_klass, name.name
10
+ reflection.check_validity!
11
+ reflection.check_eager_loadable!
12
+
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
+ reflection.check_eager_loadable!
23
+
24
+ if reflection.polymorphic?
25
+ raise ActiveRecord::EagerLoadPolymorphicError.new(reflection)
26
+ end
27
+ JoinAssociation.new(reflection, build(right, reflection.klass))
28
+ end
29
+ end
30
+ end
31
+
32
+ def join_constraints(joins_to_add, alias_tracker)
33
+ @alias_tracker = alias_tracker
34
+
35
+ construct_tables!(join_root)
36
+ joins = make_join_constraints(join_root, join_type)
37
+
38
+ joins.concat joins_to_add.flat_map { |oj|
39
+ construct_tables!(oj.join_root)
40
+ if join_root.match?(oj.join_root) && join_root.table.name == oj.join_root.table.name
41
+ walk join_root, oj.join_root, oj.join_type
42
+ else
43
+ make_join_constraints(oj.join_root, oj.join_type)
44
+ end
45
+ }
46
+ end
47
+
48
+ private
49
+ def make_constraints(parent, child, join_type = Arel::Nodes::OuterJoin)
50
+ foreign_table = parent.table
51
+ foreign_klass = parent.base_klass
52
+ join_type = child.join_type || join_type if join_type == Arel::Nodes::InnerJoin
53
+ joins = child.join_constraints(foreign_table, foreign_klass, join_type, alias_tracker)
54
+ joins.concat child.children.flat_map { |c| make_constraints(child, c, join_type) }
55
+ end
56
+
57
+ module ClassMethods
58
+ # Prepended before ActiveRecord::Associations::JoinDependency#walk_tree
59
+ #
60
+ def walk_tree(associations, hash)
61
+ case associations
62
+ when TreeNode
63
+ associations.add_to_tree(hash)
64
+ when Hash
65
+ associations.each do |k, v|
66
+ cache =
67
+ if TreeNode === k
68
+ k.add_to_tree(hash)
69
+ else
70
+ hash[k] ||= {}
71
+ end
72
+ walk_tree(v, cache)
73
+ end
74
+ else
75
+ super(associations, hash)
76
+ end
77
+ end
78
+ end
79
+
80
+ end
81
+ end
@@ -0,0 +1,2 @@
1
+ # active_record_6.0_ruby_2/reflection.rb
2
+ require 'polyamorous/activerecord_5.2.0_ruby_2/reflection'
@@ -0,0 +1,2 @@
1
+ # active_record_6.1_ruby_2/join_association
2
+ require 'polyamorous/activerecord_6.0_ruby_2/join_association'