polyamorous 1.3.0 → 1.3.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 974ca93cd847897179ae0ad4d5a2628bd65a8444
4
- data.tar.gz: 7183fb57fe8068011d9add6efd750d9386c87342
3
+ metadata.gz: 9ff05c13352acf27880e21028691c8bd6b6f89e0
4
+ data.tar.gz: 5e6d545c5402a4b32425de4f0e929377b4681492
5
5
  SHA512:
6
- metadata.gz: 1e8ddde2bf2c992b252712c63c96cac958a59289887650ac6c611481f77a4b7f5f69e7375f010afddb065dbfc955bbefe91b4dc397404978ccb6f10cff278a82
7
- data.tar.gz: d193721ba969fd2ff9f08bf227be4a7d9192cb303fa7b3c51c37d762fe363c01f6b7b89d1b08c714cfbd69afec97a8c16f715b0347181ca87734e61fdfdb0da0
6
+ metadata.gz: 9ba07c1dc59826858869435c4d2a816c543208c4c87ab6ac57df31de05c32eed600f416b38982f3b0a5678b4b92a4f9443d866b89c029f1c7fe40fca36636ccc
7
+ data.tar.gz: ae5c40ed036a5d584dd134a2208fd37009a58962410b0ad2fe212285295889cf59d7211864eb1ebdc71062296f5c24589f7cd8c23d9a8402b42b5668a9345d4e
@@ -6,8 +6,9 @@ before_install:
6
6
  - travis_retry gem install bundler
7
7
 
8
8
  rvm:
9
- - 2.2.3
10
- - 2.1
9
+ - 2.3.1
10
+ - 2.2.5
11
+ - 2.1.10
11
12
  - 2.0
12
13
  - 1.9
13
14
 
@@ -38,12 +39,34 @@ env:
38
39
 
39
40
  matrix:
40
41
  include:
41
- - rvm: 2.2.3
42
+ - rvm: 2.3.1
42
43
  env: RAILS=master DB=sqlite3
43
- - rvm: 2.2.3
44
+ - rvm: 2.3.1
44
45
  env: RAILS=master DB=mysql
45
- - rvm: 2.2.3
46
+ - rvm: 2.3.1
46
47
  env: RAILS=master DB=postgres
48
+
49
+ - rvm: 2.2.5
50
+ env: RAILS=master DB=sqlite3
51
+ - rvm: 2.2.5
52
+ env: RAILS=master DB=mysql
53
+ - rvm: 2.2.5
54
+ env: RAILS=master DB=postgres
55
+
56
+ - rvm: 2.3.1
57
+ env: RAILS=5-0-stable DB=sqlite3
58
+ - rvm: 2.3.1
59
+ env: RAILS=5-0-stable DB=mysql
60
+ - rvm: 2.3.1
61
+ env: RAILS=5-0-stable DB=postgres
62
+
63
+ - rvm: 2.2.5
64
+ env: RAILS=5-0-stable DB=sqlite3
65
+ - rvm: 2.2.5
66
+ env: RAILS=5-0-stable DB=mysql
67
+ - rvm: 2.2.5
68
+ env: RAILS=5-0-stable DB=postgres
69
+
47
70
  allow_failures:
48
71
  - env: RAILS=master DB=sqlite3
49
72
  - env: RAILS=master DB=mysql
data/README.md CHANGED
@@ -20,7 +20,5 @@ Polyamorous is an extraction of shared code from the
20
20
  (https://github.com/activerecord-hackery/polyamorous/graphs/contributors).
21
21
 
22
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
23
+ polymorphism. There is no public API, so it's `:nodoc:`. Move along. Nothing to
24
24
  see here.
25
-
26
- Copyright © 2011-2015 [Ernie Miller](http://twitter.com/erniemiller)
@@ -1,6 +1,7 @@
1
1
  module Polyamorous
2
2
  module JoinDependencyExtensions
3
3
  # Replaces ActiveRecord::Associations::JoinDependency#make_inner_joins
4
+ #
4
5
  def make_polyamorous_inner_joins(parent, child)
5
6
  make_constraints(
6
7
  parent, child, child.tables, child.join_type || Arel::Nodes::InnerJoin
@@ -20,6 +20,7 @@ module Polyamorous
20
20
  end
21
21
 
22
22
  # Replaces ActiveRecord::Associations::JoinDependency#build
23
+ #
23
24
  def build_with_polymorphism(associations, base_klass)
24
25
  associations.map do |name, right|
25
26
  if name.is_a? Join
@@ -43,7 +44,8 @@ module Polyamorous
43
44
  end
44
45
 
45
46
  # Replaces ActiveRecord::Associations::JoinDependency#join_constraints
46
- # to call #make_polyamorous_inner_joins instead of #make_inner_joins.
47
+ # to call #make_polyamorous_inner_joins instead of #make_inner_joins
48
+ #
47
49
  def join_constraints_with_polymorphism(outer_joins)
48
50
  joins = join_root.children.flat_map { |child|
49
51
  make_polyamorous_inner_joins join_root, child
@@ -60,10 +62,22 @@ module Polyamorous
60
62
  end
61
63
 
62
64
  module ClassMethods
63
- # Replaces ActiveRecord::Associations::JoinDependency#self.walk_tree
65
+ # Replaces ActiveRecord::Associations::JoinDependency#self.walk_tree
66
+ #
64
67
  def walk_tree_with_polymorphism(associations, hash)
65
- if TreeNode === associations
68
+ case associations
69
+ when TreeNode
66
70
  associations.add_to_tree(hash)
71
+ when Hash
72
+ associations.each do |k, v|
73
+ cache =
74
+ if TreeNode === k
75
+ k.add_to_tree(hash)
76
+ else
77
+ hash[k] ||= {}
78
+ end
79
+ walk_tree(v, cache)
80
+ end
67
81
  else
68
82
  walk_tree_without_polymorphism(associations, hash)
69
83
  end
@@ -1,38 +1,2 @@
1
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
2
+ require 'polyamorous/activerecord_5.1_ruby_2/join_association'
@@ -1,116 +1,2 @@
1
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
2
+ require 'polyamorous/activerecord_5.1_ruby_2/join_dependency'
@@ -0,0 +1,39 @@
1
+ # active_record_5.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,
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
+ #
26
+ def ==(other)
27
+ base_klass == other.base_klass
28
+ end
29
+
30
+ def build_constraint(klass, table, key, foreign_table, foreign_key)
31
+ if reflection.polymorphic?
32
+ super(klass, table, key, foreign_table, foreign_key)
33
+ .and(foreign_table[reflection.foreign_type].eq(reflection.klass.name))
34
+ else
35
+ super(klass, table, key, foreign_table, foreign_key)
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,126 @@
1
+ # active_record_5.1_ruby_2/join_dependency.rb
2
+
3
+ module Polyamorous
4
+ module JoinDependencyExtensions
5
+ # Replaces ActiveRecord::Associations::JoinDependency#build
6
+ #
7
+ def build(associations, base_klass)
8
+ associations.map do |name, right|
9
+ if name.is_a? Join
10
+ reflection = find_reflection base_klass, name.name
11
+ reflection.check_validity!
12
+ klass = if reflection.polymorphic?
13
+ name.klass || base_klass
14
+ else
15
+ reflection.klass
16
+ end
17
+ JoinAssociation.new(reflection, build(right, klass), name.klass, name.type)
18
+ else
19
+ reflection = find_reflection base_klass, name
20
+ reflection.check_validity!
21
+ if reflection.polymorphic?
22
+ raise ActiveRecord::EagerLoadPolymorphicError.new(reflection)
23
+ end
24
+ JoinAssociation.new reflection, build(right, reflection.klass)
25
+ end
26
+ end
27
+ end
28
+
29
+ def find_join_association_respecting_polymorphism(reflection, parent, klass)
30
+ if association = parent.children.find { |j| j.reflection == reflection }
31
+ unless reflection.polymorphic?
32
+ association
33
+ else
34
+ association if association.base_klass == klass
35
+ end
36
+ end
37
+ end
38
+
39
+ def build_join_association_respecting_polymorphism(reflection, parent, klass)
40
+ if reflection.polymorphic? && klass
41
+ JoinAssociation.new(reflection, self, klass)
42
+ else
43
+ JoinAssociation.new(reflection, self)
44
+ end
45
+ end
46
+
47
+ # Replaces ActiveRecord::Associations::JoinDependency#join_constraints
48
+ #
49
+ # This internal method was changed in Rails 5.0 by commit
50
+ # https://github.com/rails/rails/commit/e038975 which added
51
+ # left_outer_joins (see #make_polyamorous_left_outer_joins below) and added
52
+ # passing an additional argument, `join_type`, to #join_constraints.
53
+ #
54
+ def join_constraints(outer_joins, join_type)
55
+ joins = join_root.children.flat_map { |child|
56
+ if join_type == Arel::Nodes::OuterJoin
57
+ make_polyamorous_left_outer_joins join_root, child
58
+ else
59
+ make_polyamorous_inner_joins join_root, child
60
+ end
61
+ }
62
+
63
+ joins.concat outer_joins.flat_map { |oj|
64
+ if join_root.match? oj.join_root
65
+ walk(join_root, oj.join_root)
66
+ else
67
+ oj.join_root.children.flat_map { |child|
68
+ make_outer_joins(oj.join_root, child)
69
+ }
70
+ end
71
+ }
72
+ end
73
+
74
+ # Replaces ActiveRecord::Associations::JoinDependency#make_left_outer_joins,
75
+ # a new method that was added in Rails 5.0 with the following commit:
76
+ # https://github.com/rails/rails/commit/e038975
77
+ #
78
+ def make_polyamorous_left_outer_joins(parent, child)
79
+ tables = child.tables
80
+ join_type = Arel::Nodes::OuterJoin
81
+ info = make_constraints parent, child, tables, join_type
82
+
83
+ [info] + child.children.flat_map { |c|
84
+ make_polyamorous_left_outer_joins(child, c)
85
+ }
86
+ end
87
+
88
+ # Replaces ActiveRecord::Associations::JoinDependency#make_inner_joins
89
+ #
90
+ def make_polyamorous_inner_joins(parent, child)
91
+ tables = child.tables
92
+ join_type = child.join_type || Arel::Nodes::InnerJoin
93
+ info = make_constraints parent, child, tables, join_type
94
+
95
+ [info] + child.children.flat_map { |c|
96
+ make_polyamorous_inner_joins(child, c)
97
+ }
98
+ end
99
+
100
+ private :make_polyamorous_inner_joins, :make_polyamorous_left_outer_joins
101
+
102
+ module ClassMethods
103
+ # Prepended before ActiveRecord::Associations::JoinDependency#walk_tree
104
+ #
105
+ def walk_tree(associations, hash)
106
+ case associations
107
+ when TreeNode
108
+ associations.add_to_tree(hash)
109
+ when Hash
110
+ associations.each do |k, v|
111
+ cache =
112
+ if TreeNode === k
113
+ k.add_to_tree(hash)
114
+ else
115
+ hash[k] ||= {}
116
+ end
117
+ walk_tree(v, cache)
118
+ end
119
+ else
120
+ super(associations, hash)
121
+ end
122
+ end
123
+ end
124
+
125
+ end
126
+ end
@@ -1,3 +1,3 @@
1
1
  module Polyamorous
2
- VERSION = '1.3.0'
2
+ VERSION = '1.3.1'
3
3
  end
@@ -21,9 +21,9 @@ Gem::Specification.new do |s|
21
21
  s.rubyforge_project = "polyamorous"
22
22
 
23
23
  s.add_dependency 'activerecord', '>= 3.0'
24
- s.add_development_dependency 'rspec', '~> 2.14.0'
24
+ s.add_development_dependency 'rspec', '~> 3'
25
25
  s.add_development_dependency 'machinist', '~> 1.0.6'
26
- s.add_development_dependency 'faker', '~> 0.9.5'
26
+ s.add_development_dependency 'faker', '~> 1.6.5'
27
27
  s.add_development_dependency 'sqlite3', '~> 1.3.3'
28
28
 
29
29
  s.files = `git ls-files`.split("\n")
@@ -47,8 +47,8 @@ module Polyamorous
47
47
  end
48
48
  end
49
49
 
50
- it 'sets the polmorphic option to true after initializing' do
51
- expect(join_association.instance_eval(polymorphic)).to be_true
50
+ it 'sets the polymorphic option to true after initializing' do
51
+ expect(join_association.instance_eval(polymorphic)).to be true
52
52
  end
53
53
  end
54
54
  end
@@ -82,5 +82,16 @@ module Polyamorous
82
82
  specify { expect(subject.send(method, join_associations)[1].table_name)
83
83
  .to eq 'comments' }
84
84
  end
85
+
86
+ context '#left_outer_join in Rails 5 overrides join type specified',
87
+ if: ActiveRecord::VERSION::MAJOR >= 5 do
88
+
89
+ let(:join_type_class) {
90
+ new_join_dependency(Person, new_join(:articles))
91
+ .join_constraints([], Arel::Nodes::OuterJoin).first.joins.map(&:class)
92
+ }
93
+
94
+ specify { expect(join_type_class).to eq [Arel::Nodes::OuterJoin] }
95
+ end
85
96
  end
86
97
  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.3.0
4
+ version: 1.3.1
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-12-09 00:00:00.000000000 Z
14
+ date: 2016-07-13 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: activerecord
@@ -33,14 +33,14 @@ dependencies:
33
33
  requirements:
34
34
  - - "~>"
35
35
  - !ruby/object:Gem::Version
36
- version: 2.14.0
36
+ version: '3'
37
37
  type: :development
38
38
  prerelease: false
39
39
  version_requirements: !ruby/object:Gem::Requirement
40
40
  requirements:
41
41
  - - "~>"
42
42
  - !ruby/object:Gem::Version
43
- version: 2.14.0
43
+ version: '3'
44
44
  - !ruby/object:Gem::Dependency
45
45
  name: machinist
46
46
  requirement: !ruby/object:Gem::Requirement
@@ -61,14 +61,14 @@ dependencies:
61
61
  requirements:
62
62
  - - "~>"
63
63
  - !ruby/object:Gem::Version
64
- version: 0.9.5
64
+ version: 1.6.5
65
65
  type: :development
66
66
  prerelease: false
67
67
  version_requirements: !ruby/object:Gem::Requirement
68
68
  requirements:
69
69
  - - "~>"
70
70
  - !ruby/object:Gem::Version
71
- version: 0.9.5
71
+ version: 1.6.5
72
72
  - !ruby/object:Gem::Dependency
73
73
  name: sqlite3
74
74
  requirement: !ruby/object:Gem::Requirement
@@ -115,6 +115,8 @@ files:
115
115
  - lib/polyamorous/activerecord_4.2_ruby_2/join_dependency.rb
116
116
  - lib/polyamorous/activerecord_5.0_ruby_2/join_association.rb
117
117
  - lib/polyamorous/activerecord_5.0_ruby_2/join_dependency.rb
118
+ - lib/polyamorous/activerecord_5.1_ruby_2/join_association.rb
119
+ - lib/polyamorous/activerecord_5.1_ruby_2/join_dependency.rb
118
120
  - lib/polyamorous/join.rb
119
121
  - lib/polyamorous/swapping_reflection_class.rb
120
122
  - lib/polyamorous/tree_node.rb
@@ -151,7 +153,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
151
153
  version: '0'
152
154
  requirements: []
153
155
  rubyforge_project: polyamorous
154
- rubygems_version: 2.5.0
156
+ rubygems_version: 2.6.4
155
157
  signing_key:
156
158
  specification_version: 4
157
159
  summary: Loves/is loved by polymorphic belongs_to associations, Ransack, Squeel, MetaSearch...