polyamorous 1.3.1 → 1.3.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9ff05c13352acf27880e21028691c8bd6b6f89e0
4
- data.tar.gz: 5e6d545c5402a4b32425de4f0e929377b4681492
3
+ metadata.gz: ba8814592b049107b1eba9e3a5ae6688cb204be0
4
+ data.tar.gz: accde4c264086caab8643396bb8d17a95ecbefbf
5
5
  SHA512:
6
- metadata.gz: 9ba07c1dc59826858869435c4d2a816c543208c4c87ab6ac57df31de05c32eed600f416b38982f3b0a5678b4b92a4f9443d866b89c029f1c7fe40fca36636ccc
7
- data.tar.gz: ae5c40ed036a5d584dd134a2208fd37009a58962410b0ad2fe212285295889cf59d7211864eb1ebdc71062296f5c24589f7cd8c23d9a8402b42b5668a9345d4e
6
+ metadata.gz: 72f2e30aebe8916dd516d303a14bed69f25bc88784faca7f0cf9abaaeddfd610801cb4eb21f87e26765282965bc8ed214d5dc751975d4e829487eec5a68d927a
7
+ data.tar.gz: d65afd228a8693875fbdc9d5210952c78e31bffe4c1fa033028e769489f3e218c37bd55fe2ec30779a9b4b3cb7f14ba6f555165022c8e276766d8966383bf4ac
data/.travis.yml CHANGED
@@ -1,76 +1,57 @@
1
1
  language: ruby
2
2
 
3
3
  sudo: false
4
+ cache: bundler
4
5
 
5
6
  before_install:
6
7
  - travis_retry gem install bundler
7
8
 
8
9
  rvm:
9
- - 2.3.1
10
- - 2.2.5
11
- - 2.1.10
12
- - 2.0
13
- - 1.9
10
+ - 2.5.0
11
+ - 2.4.3
12
+ - 2.3.6
13
+ - 2.2.9
14
14
 
15
15
  env:
16
16
  - RAILS=4-2-stable AREL=6-0-stable DB=sqlite
17
17
  - RAILS=4-2-stable AREL=6-0-stable DB=mysql
18
18
  - RAILS=4-2-stable AREL=6-0-stable DB=postgres
19
-
20
- - RAILS=4-1-stable AREL=5-0-stable DB=sqlite
21
- - RAILS=4-1-stable AREL=5-0-stable DB=mysql
22
- - RAILS=4-1-stable AREL=5-0-stable DB=postgres
23
-
24
- - RAILS=4-0-stable AREL=4-0-stable DB=sqlite
25
- - RAILS=4-0-stable AREL=4-0-stable DB=mysql
26
- - RAILS=4-0-stable AREL=4-0-stable DB=postgres
27
-
28
- - RAILS=3-2-stable AREL=3-0-stable DB=sqlite
29
- - RAILS=3-2-stable AREL=3-0-stable DB=mysql
30
- - RAILS=3-2-stable AREL=3-0-stable DB=postgres
31
-
32
- - RAILS=3-1-stable AREL=2-2-stable DB=sqlite
33
- - RAILS=3-1-stable AREL=2-2-stable DB=mysql
34
- - RAILS=3-1-stable AREL=2-2-stable DB=postgres
35
-
36
- - RAILS=3-0-stable AREL=2-0-stable DB=sqlite
37
- - RAILS=3-0-stable AREL=2-0-stable DB=mysql
38
- - RAILS=3-0-stable AREL=2-0-stable DB=postgres
19
+ - RAILS=5-0-stable AREL=7-0-stable DB=sqlite
20
+ - RAILS=5-0-stable AREL=7-0-stable DB=mysql
21
+ - RAILS=5-0-stable AREL=7-0-stable DB=postgres
22
+ - RAILS=5-1-stable AREL=8-0-stable DB=sqlite
23
+ - RAILS=5-1-stable AREL=8-0-stable DB=mysql
24
+ - RAILS=5-1-stable AREL=8-0-stable DB=postgres
39
25
 
40
26
  matrix:
41
27
  include:
42
- - rvm: 2.3.1
28
+ - rvm: 2.5.0
43
29
  env: RAILS=master DB=sqlite3
44
- - rvm: 2.3.1
30
+ - rvm: 2.5.0
45
31
  env: RAILS=master DB=mysql
46
- - rvm: 2.3.1
32
+ - rvm: 2.5.0
47
33
  env: RAILS=master DB=postgres
48
34
 
49
- - rvm: 2.2.5
35
+ - rvm: 2.4.3
50
36
  env: RAILS=master DB=sqlite3
51
- - rvm: 2.2.5
37
+ - rvm: 2.4.3
52
38
  env: RAILS=master DB=mysql
53
- - rvm: 2.2.5
39
+ - rvm: 2.4.3
54
40
  env: RAILS=master DB=postgres
55
41
 
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
42
+ - rvm: 2.3.6
43
+ env: RAILS=master DB=sqlite3
44
+ - rvm: 2.3.6
45
+ env: RAILS=master DB=mysql
46
+ - rvm: 2.3.6
47
+ env: RAILS=master DB=postgres
69
48
 
70
- allow_failures:
71
- - env: RAILS=master DB=sqlite3
72
- - env: RAILS=master DB=mysql
73
- - env: RAILS=master DB=postgres
49
+ - rvm: 2.2.9
50
+ env: RAILS=master DB=sqlite3
51
+ - rvm: 2.2.9
52
+ env: RAILS=master DB=mysql
53
+ - rvm: 2.2.9
54
+ env: RAILS=master DB=postgres
74
55
 
75
56
  before_script:
76
57
  - mysql -e 'create database ransack collate utf8_general_ci;'
data/README.md CHANGED
@@ -1,11 +1,7 @@
1
1
  # Polyamorous
2
-
3
- [![Build Status](https://travis-ci.org/activerecord-hackery/polyamorous.svg)]
4
- (https://travis-ci.org/activerecord-hackery/polyamorous)
5
- [![Gem Version](https://badge.fury.io/rb/polyamorous.svg)]
6
- (http://badge.fury.io/rb/polyamorous)
7
- [![Code Climate](https://codeclimate.com/github/activerecord-hackery/polyamorous/badges/gpa.svg)]
8
- (https://codeclimate.com/github/activerecord-hackery/polyamorous)
2
+ [![Build Status](https://travis-ci.org/activerecord-hackery/polyamorous.svg?branch=master)](https://travis-ci.org/activerecord-hackery/polyamorous)
3
+ [![Gem Version](https://badge.fury.io/rb/polyamorous.svg)](https://badge.fury.io/rb/polyamorous)
4
+ [![Code Climate](https://codeclimate.com/github/activerecord-hackery/polyamorous/badges/gpa.svg)](https://codeclimate.com/github/activerecord-hackery/polyamorous)
9
5
 
10
6
  Polyamorous is an extraction of shared code from the
11
7
  [Active Record Hackery](https://github.com/activerecord-hackery) gems
@@ -15,7 +11,8 @@ Polyamorous is an extraction of shared code from the
15
11
  [Ernie Miller](http://twitter.com/erniemiller) and maintained by
16
12
  [Ryan Bigg](http://twitter.com/ryanbigg),
17
13
  [Xiang Li](http://bigxiang.github.io),
18
- [Jon Atack](http://twitter.com/jonatack) and a great little group of
14
+ [Jon Atack](http://twitter.com/jonatack),
15
+ [Sean Carroll](https://github.com/seanfcarroll) and a great little group of
19
16
  [contributors]
20
17
  (https://github.com/activerecord-hackery/polyamorous/graphs/contributors).
21
18
 
@@ -1,2 +1,2 @@
1
1
  # active_record_5.0_ruby_2/join_association.rb
2
- require 'polyamorous/activerecord_5.1_ruby_2/join_association'
2
+ require 'polyamorous/activerecord_5.1_ruby_2/join_association'
@@ -1,2 +1,2 @@
1
1
  # active_record_5.0_ruby_2/join_dependency.rb
2
- require 'polyamorous/activerecord_5.1_ruby_2/join_dependency'
2
+ require 'polyamorous/activerecord_5.1_ruby_2/join_dependency'
@@ -8,7 +8,7 @@ module Polyamorous
8
8
  end
9
9
 
10
10
  def initialize(reflection, children, polymorphic_class = nil,
11
- join_type = Arel::Nodes::InnerJoin)
11
+ join_type = Arel::Nodes::InnerJoin)
12
12
  @join_type = join_type
13
13
  if polymorphic_class && ::ActiveRecord::Base > polymorphic_class
14
14
  swapping_reflection_klass(reflection, polymorphic_class) do |reflection|
@@ -30,7 +30,7 @@ module Polyamorous
30
30
  def build_constraint(klass, table, key, foreign_table, foreign_key)
31
31
  if reflection.polymorphic?
32
32
  super(klass, table, key, foreign_table, foreign_key)
33
- .and(foreign_table[reflection.foreign_type].eq(reflection.klass.name))
33
+ .and(foreign_table[reflection.foreign_type].eq(reflection.klass.name))
34
34
  else
35
35
  super(klass, table, key, foreign_table, foreign_key)
36
36
  end
@@ -9,15 +9,19 @@ 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
13
+
12
14
  klass = if reflection.polymorphic?
13
- name.klass || base_klass
14
- else
15
- reflection.klass
16
- end
15
+ name.klass || base_klass
16
+ else
17
+ reflection.klass
18
+ end
17
19
  JoinAssociation.new(reflection, build(right, klass), name.klass, name.type)
18
20
  else
19
21
  reflection = find_reflection base_klass, name
20
22
  reflection.check_validity!
23
+ reflection.check_eager_loadable! if ActiveRecord::VERSION::MAJOR >= 5
24
+
21
25
  if reflection.polymorphic?
22
26
  raise ActiveRecord::EagerLoadPolymorphicError.new(reflection)
23
27
  end
@@ -115,7 +119,7 @@ module Polyamorous
115
119
  hash[k] ||= {}
116
120
  end
117
121
  walk_tree(v, cache)
118
- end
122
+ end
119
123
  else
120
124
  super(associations, hash)
121
125
  end
@@ -0,0 +1,39 @@
1
+ # active_record_5.2_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, alias_tracker, 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, alias_tracker)
16
+ self.reflection.options[:polymorphic] = true
17
+ end
18
+ else
19
+ super(reflection, children, alias_tracker)
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,130 @@
1
+ # active_record_5.2_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
+ reflection.check_eager_loadable! if ActiveRecord::VERSION::MAJOR >= 5
13
+
14
+ klass = if reflection.polymorphic?
15
+ name.klass || base_klass
16
+ else
17
+ reflection.klass
18
+ end
19
+ JoinAssociation.new(reflection, build(right, klass), alias_tracker, name.klass, name.type)
20
+ else
21
+ reflection = find_reflection base_klass, name
22
+ reflection.check_validity!
23
+ reflection.check_eager_loadable! if ActiveRecord::VERSION::MAJOR >= 5
24
+
25
+ if reflection.polymorphic?
26
+ raise ActiveRecord::EagerLoadPolymorphicError.new(reflection)
27
+ end
28
+ JoinAssociation.new(reflection, build(right, reflection.klass), alias_tracker)
29
+ end
30
+ end
31
+ end
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
+ # Replaces ActiveRecord::Associations::JoinDependency#join_constraints
52
+ #
53
+ # This internal method was changed in Rails 5.0 by commit
54
+ # https://github.com/rails/rails/commit/e038975 which added
55
+ # left_outer_joins (see #make_polyamorous_left_outer_joins below) and added
56
+ # passing an additional argument, `join_type`, to #join_constraints.
57
+ #
58
+ def join_constraints(outer_joins, join_type)
59
+ joins = join_root.children.flat_map { |child|
60
+ if join_type == Arel::Nodes::OuterJoin
61
+ make_polyamorous_left_outer_joins join_root, child
62
+ else
63
+ make_polyamorous_inner_joins join_root, child
64
+ end
65
+ }
66
+
67
+ joins.concat outer_joins.flat_map { |oj|
68
+ if join_root.match? oj.join_root
69
+ walk(join_root, oj.join_root)
70
+ else
71
+ oj.join_root.children.flat_map { |child|
72
+ make_outer_joins(oj.join_root, child)
73
+ }
74
+ end
75
+ }
76
+ end
77
+
78
+ # Replaces ActiveRecord::Associations::JoinDependency#make_left_outer_joins,
79
+ # a new method that was added in Rails 5.0 with the following commit:
80
+ # https://github.com/rails/rails/commit/e038975
81
+ #
82
+ def make_polyamorous_left_outer_joins(parent, child)
83
+ tables = child.tables
84
+ join_type = Arel::Nodes::OuterJoin
85
+ info = make_constraints parent, child, tables, join_type
86
+
87
+ [info] + child.children.flat_map { |c|
88
+ make_polyamorous_left_outer_joins(child, c)
89
+ }
90
+ end
91
+
92
+ # Replaces ActiveRecord::Associations::JoinDependency#make_inner_joins
93
+ #
94
+ def make_polyamorous_inner_joins(parent, child)
95
+ tables = child.tables
96
+ join_type = child.join_type || Arel::Nodes::InnerJoin
97
+ info = make_constraints parent, child, tables, join_type
98
+
99
+ [info] + child.children.flat_map { |c|
100
+ make_polyamorous_inner_joins(child, c)
101
+ }
102
+ end
103
+
104
+ private :make_polyamorous_inner_joins, :make_polyamorous_left_outer_joins
105
+
106
+ module ClassMethods
107
+ # Prepended before ActiveRecord::Associations::JoinDependency#walk_tree
108
+ #
109
+ def walk_tree(associations, hash)
110
+ case associations
111
+ when TreeNode
112
+ associations.add_to_tree(hash)
113
+ when Hash
114
+ associations.each do |k, v|
115
+ cache =
116
+ if TreeNode === k
117
+ k.add_to_tree(hash)
118
+ else
119
+ hash[k] ||= {}
120
+ end
121
+ walk_tree(v, cache)
122
+ end
123
+ else
124
+ super(associations, hash)
125
+ end
126
+ end
127
+ end
128
+
129
+ end
130
+ end
@@ -1,3 +1,3 @@
1
1
  module Polyamorous
2
- VERSION = '1.3.1'
2
+ VERSION = '1.3.2'
3
3
  end
@@ -9,12 +9,18 @@ module PolyamorousHelper
9
9
  end
10
10
  end
11
11
 
12
- def new_join_dependency(klass, associations = {})
13
- Polyamorous::JoinDependency.new klass, associations, []
12
+ if ActiveRecord::VERSION::STRING >= "5.2"
13
+ def new_join_dependency(klass, associations = {})
14
+ alias_tracker = ::ActiveRecord::Associations::AliasTracker.create(klass.connection, klass.table_name, [])
15
+ Polyamorous::JoinDependency.new klass, klass.arel_table, associations, alias_tracker
16
+ end
17
+ else
18
+ def new_join_dependency(klass, associations = {})
19
+ Polyamorous::JoinDependency.new klass, associations, []
20
+ end
14
21
  end
15
22
 
16
23
  def new_join(name, type = Polyamorous::InnerJoin, klass = nil)
17
24
  Polyamorous::Join.new name, type, klass
18
25
  end
19
-
20
26
  end
@@ -11,10 +11,10 @@ module Polyamorous
11
11
  end
12
12
 
13
13
  context 'with symbol joins' do
14
- subject { new_join_dependency Person, :articles => :comments }
14
+ subject { new_join_dependency Person, articles: :comments }
15
15
 
16
16
  specify { expect(subject.send(method, join_associations).size)
17
- .to eq 2 }
17
+ .to eq(2) }
18
18
  specify { expect(subject.send(method, join_associations).map(&:join_type))
19
19
  .to be_all { Polyamorous::InnerJoin } }
20
20
  end
@@ -84,12 +84,17 @@ module Polyamorous
84
84
  end
85
85
 
86
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
- }
87
+ if: ActiveRecord::VERSION::MAJOR >= 5 && ActiveRecord::VERSION::MINOR < 2 do
88
+
89
+ let(:join_type_class) do
90
+ new_join_dependency(
91
+ Person,
92
+ new_join(:articles)
93
+ ).join_constraints(
94
+ [],
95
+ Arel::Nodes::OuterJoin
96
+ ).first.joins.map(&:class)
97
+ end
93
98
 
94
99
  specify { expect(join_type_class).to eq [Arel::Nodes::OuterJoin] }
95
100
  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.1
4
+ version: 1.3.2
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: 2016-07-13 00:00:00.000000000 Z
14
+ date: 2017-12-29 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: activerecord
@@ -117,6 +117,8 @@ files:
117
117
  - lib/polyamorous/activerecord_5.0_ruby_2/join_dependency.rb
118
118
  - lib/polyamorous/activerecord_5.1_ruby_2/join_association.rb
119
119
  - lib/polyamorous/activerecord_5.1_ruby_2/join_dependency.rb
120
+ - lib/polyamorous/activerecord_5.2_ruby_2/join_association.rb
121
+ - lib/polyamorous/activerecord_5.2_ruby_2/join_dependency.rb
120
122
  - lib/polyamorous/join.rb
121
123
  - lib/polyamorous/swapping_reflection_class.rb
122
124
  - lib/polyamorous/tree_node.rb
@@ -153,7 +155,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
153
155
  version: '0'
154
156
  requirements: []
155
157
  rubyforge_project: polyamorous
156
- rubygems_version: 2.6.4
158
+ rubygems_version: 2.6.13
157
159
  signing_key:
158
160
  specification_version: 4
159
161
  summary: Loves/is loved by polymorphic belongs_to associations, Ransack, Squeel, MetaSearch...