goldiloader 0.0.6 → 0.0.7
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 +9 -4
- data/CHANGELOG.md +9 -1
- data/goldiloader.gemspec +2 -2
- data/lib/goldiloader/active_record_patches.rb +2 -1
- data/lib/goldiloader/association_info.rb +39 -18
- data/lib/goldiloader/auto_include_context.rb +1 -1
- data/lib/goldiloader/version.rb +1 -1
- data/spec/db/schema.rb +1 -0
- data/spec/goldiloader/auto_include_context_spec.rb +31 -0
- data/spec/goldiloader/goldiloader_spec.rb +14 -0
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0abf48daca16a3eb1ee9eef6a059d1ff3eb77153
|
4
|
+
data.tar.gz: 06b4f7f3d16be572b0981c579141de46f363d28b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5fd2ab09fd21e0f18aa97210d9c3f6696bf96a4fa4d07bd5a163237431e24019641ab5586c42b59219f31b0827481364dea662f86c8c50c72e3f34eca5be2c3a
|
7
|
+
data.tar.gz: bcf2dbf9cc413af86f6dcd0cffafcc435fd52bde9b8e6c9752abe38b5dcae34b42b2952b53dc5af45fbc04ab229f7e13c04a1c6e5728225029bcd4619a543d9d
|
data/.travis.yml
CHANGED
@@ -1,11 +1,16 @@
|
|
1
1
|
language: ruby
|
2
2
|
env:
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
3
|
+
- RAILS_VERSION="~> 3.2.19" JRUBY_OPTS="$JRUBY_OPTS --debug"
|
4
|
+
- RAILS_VERSION="~> 4.0.10" JRUBY_OPTS="$JRUBY_OPTS --debug"
|
5
|
+
- RAILS_VERSION="~> 4.1.6" JRUBY_OPTS="$JRUBY_OPTS --debug"
|
6
|
+
- RAILS_VERSION="~> 4.2.0.beta2" JRUBY_OPTS="$JRUBY_OPTS --debug"
|
7
7
|
rvm:
|
8
8
|
- 1.9.3
|
9
9
|
- 2.0.0
|
10
10
|
- 2.1.0
|
11
11
|
- jruby-19mode
|
12
|
+
matrix:
|
13
|
+
exclude:
|
14
|
+
# See https://github.com/salsify/goldiloader/issues/22
|
15
|
+
- rvm: jruby-19mode
|
16
|
+
env: RAILS_VERSION="~> 4.2.0.beta2" JRUBY_OPTS="$JRUBY_OPTS --debug"
|
data/CHANGELOG.md
CHANGED
@@ -1,6 +1,14 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
-
### 0.0.
|
3
|
+
### 0.0.7
|
4
|
+
* Fix [issue 20](https://github.com/salsify/goldiloader/issues/20) by not auto-eager loading
|
5
|
+
associations that are instance dependent. Eager loading these associations produces potentially
|
6
|
+
incorrect results and leads to a deprecation warning in Rails 4.2.
|
7
|
+
* Fix [issue 21](https://github.com/salsify/goldiloader/issues/21) - Handle explicit eager loads
|
8
|
+
of singular associations that are nil.
|
9
|
+
* Rails 4.2 support.
|
10
|
+
|
11
|
+
### 0.0.6
|
4
12
|
* Workaround [issue 16](https://github.com/salsify/goldiloader/issues/16) by not auto-eager loading
|
5
13
|
has_and_belongs_to_many associations with a uniq in Rails 3.2 since Rails doesn't eager load them
|
6
14
|
properly.
|
data/goldiloader.gemspec
CHANGED
@@ -17,8 +17,8 @@ Gem::Specification.new do |spec|
|
|
17
17
|
spec.test_files = Dir.glob('spec/**/*')
|
18
18
|
spec.require_paths = ['lib']
|
19
19
|
|
20
|
-
spec.add_dependency 'activerecord', ENV.fetch('RAILS_VERSION', ['>= 3.2', '< 4.
|
21
|
-
spec.add_dependency 'activesupport', ENV.fetch('RAILS_VERSION', ['>= 3.2', '< 4.
|
20
|
+
spec.add_dependency 'activerecord', ENV.fetch('RAILS_VERSION', ['>= 3.2', '< 4.3'])
|
21
|
+
spec.add_dependency 'activesupport', ENV.fetch('RAILS_VERSION', ['>= 3.2', '< 4.3'])
|
22
22
|
|
23
23
|
spec.add_development_dependency 'coveralls'
|
24
24
|
spec.add_development_dependency 'database_cleaner', '>= 1.2'
|
@@ -67,7 +67,8 @@ ActiveRecord::Associations::Association.class_eval do
|
|
67
67
|
# Joins not properly eager loaded - See https://github.com/salsify/goldiloader/issues/11
|
68
68
|
!association_info.joins? &&
|
69
69
|
# Unscope not properly eager loaded - See https://github.com/salsify/goldiloader/issues/13
|
70
|
-
!association_info.unscope?
|
70
|
+
!association_info.unscope? &&
|
71
|
+
!association_info.instance_dependent?
|
71
72
|
end
|
72
73
|
|
73
74
|
def load_with_auto_include(load_method, *args)
|
@@ -7,44 +7,50 @@ module Goldiloader
|
|
7
7
|
@association = association
|
8
8
|
end
|
9
9
|
|
10
|
-
def unscope?
|
11
|
-
Goldiloader::Compatibility.unscope_query_method_enabled? &&
|
12
|
-
@association.association_scope.unscope_values.present?
|
13
|
-
end
|
14
|
-
|
15
10
|
def finder_sql?
|
16
11
|
Goldiloader::Compatibility.association_finder_sql_enabled? &&
|
17
|
-
|
12
|
+
association_options[:finder_sql].present?
|
18
13
|
end
|
19
14
|
|
20
15
|
if ActiveRecord::VERSION::MAJOR >= 4
|
16
|
+
delegate :association_scope, :reflection, to: :@association
|
17
|
+
|
21
18
|
def read_only?
|
22
|
-
|
19
|
+
association_scope.readonly_value.present?
|
23
20
|
end
|
24
21
|
|
25
22
|
def offset?
|
26
|
-
|
23
|
+
association_scope.offset_value.present?
|
27
24
|
end
|
28
25
|
|
29
26
|
def limit?
|
30
|
-
|
27
|
+
association_scope.limit_value.present?
|
31
28
|
end
|
32
29
|
|
33
30
|
def from?
|
34
|
-
|
31
|
+
association_scope.from_value.present?
|
35
32
|
end
|
36
33
|
|
37
34
|
def group?
|
38
|
-
|
35
|
+
association_scope.group_values.present?
|
39
36
|
end
|
40
37
|
|
41
38
|
def joins?
|
42
39
|
# Yuck - Through associations will always have a join for *each* 'through' table
|
43
|
-
(
|
40
|
+
(association_scope.joins_values.size - num_through_joins) > 0
|
44
41
|
end
|
45
42
|
|
46
43
|
def uniq?
|
47
|
-
|
44
|
+
association_scope.uniq_value
|
45
|
+
end
|
46
|
+
|
47
|
+
def instance_dependent?
|
48
|
+
reflection.scope.present? && reflection.scope.arity > 0
|
49
|
+
end
|
50
|
+
|
51
|
+
def unscope?
|
52
|
+
Goldiloader::Compatibility.unscope_query_method_enabled? &&
|
53
|
+
association_scope.unscope_values.present?
|
48
54
|
end
|
49
55
|
|
50
56
|
private
|
@@ -60,15 +66,15 @@ module Goldiloader
|
|
60
66
|
end
|
61
67
|
else
|
62
68
|
def read_only?
|
63
|
-
|
69
|
+
association_options[:readonly].present?
|
64
70
|
end
|
65
71
|
|
66
72
|
def offset?
|
67
|
-
|
73
|
+
association_options[:offset].present?
|
68
74
|
end
|
69
75
|
|
70
76
|
def limit?
|
71
|
-
|
77
|
+
association_options[:limit].present?
|
72
78
|
end
|
73
79
|
|
74
80
|
def from?
|
@@ -76,7 +82,7 @@ module Goldiloader
|
|
76
82
|
end
|
77
83
|
|
78
84
|
def group?
|
79
|
-
|
85
|
+
association_options[:group].present?
|
80
86
|
end
|
81
87
|
|
82
88
|
def joins?
|
@@ -85,9 +91,24 @@ module Goldiloader
|
|
85
91
|
end
|
86
92
|
|
87
93
|
def uniq?
|
88
|
-
|
94
|
+
association_options[:uniq]
|
95
|
+
end
|
96
|
+
|
97
|
+
def instance_dependent?
|
98
|
+
# Rails 3 didn't support this
|
99
|
+
false
|
100
|
+
end
|
101
|
+
|
102
|
+
def unscope?
|
103
|
+
# Rails 3 didn't support this
|
104
|
+
false
|
89
105
|
end
|
90
106
|
end
|
91
107
|
|
108
|
+
private
|
109
|
+
|
110
|
+
def association_options
|
111
|
+
@association.options
|
112
|
+
end
|
92
113
|
end
|
93
114
|
end
|
data/lib/goldiloader/version.rb
CHANGED
data/spec/db/schema.rb
CHANGED
@@ -67,6 +67,7 @@ class Blog < ActiveRecord::Base
|
|
67
67
|
has_many :grouped_posts, -> { group(:blog_id) }, class_name: 'Post'
|
68
68
|
has_many :offset_posts, -> { offset(2) }, class_name: 'Post'
|
69
69
|
has_many :from_posts, -> { from('(select distinct blog_id from posts) as posts') }, class_name: 'Post'
|
70
|
+
has_many :instance_dependent_posts, ->(instance) { Post.where(blog_id: instance.id) }, class_name: 'Post'
|
70
71
|
|
71
72
|
has_many :posts_ordered_by_author, -> { joins(:author).order('users.name') }, class_name: 'Post'
|
72
73
|
|
@@ -114,6 +114,37 @@ describe Goldiloader::AutoIncludeContext do
|
|
114
114
|
end
|
115
115
|
end
|
116
116
|
|
117
|
+
context "when nested associations are nil" do
|
118
|
+
let!(:roots) do
|
119
|
+
[
|
120
|
+
create_mock_model(car: cars.first),
|
121
|
+
create_mock_model(car: nil),
|
122
|
+
create_mock_model(car: cars.last)
|
123
|
+
]
|
124
|
+
end
|
125
|
+
|
126
|
+
let!(:cars) do
|
127
|
+
[
|
128
|
+
create_mock_model,
|
129
|
+
create_mock_model
|
130
|
+
]
|
131
|
+
end
|
132
|
+
|
133
|
+
before do
|
134
|
+
Goldiloader::AutoIncludeContext.register_models(roots, :car)
|
135
|
+
end
|
136
|
+
|
137
|
+
it "sets the AutoIncludeContext for roots" do
|
138
|
+
expect(roots.map(&:auto_include_context).uniq.size).to eq 1
|
139
|
+
expect(roots.first.auto_include_context.models).to match_array(roots)
|
140
|
+
end
|
141
|
+
|
142
|
+
it "sets the AutoIncludeContext for child nested associations" do
|
143
|
+
expect(cars.map(&:auto_include_context).uniq.size).to eq 1
|
144
|
+
expect(cars.first.auto_include_context.models).to match_array(cars)
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
117
148
|
def create_mock_models(num)
|
118
149
|
num.times.map { create_mock_model }
|
119
150
|
end
|
@@ -440,6 +440,20 @@ describe Goldiloader do
|
|
440
440
|
end
|
441
441
|
end
|
442
442
|
end
|
443
|
+
|
444
|
+
if ActiveRecord::VERSION::MAJOR >= 4
|
445
|
+
context "associations with an instance dependent scope" do
|
446
|
+
before do
|
447
|
+
blogs.first.instance_dependent_posts.to_a
|
448
|
+
end
|
449
|
+
|
450
|
+
it "applies the scope correctly" do
|
451
|
+
expect(blogs.first.instance_dependent_posts.to_a).to match_array(blogs.first.posts)
|
452
|
+
end
|
453
|
+
|
454
|
+
it_behaves_like "it doesn't auto eager the association", :instance_dependent_posts
|
455
|
+
end
|
456
|
+
end
|
443
457
|
end
|
444
458
|
|
445
459
|
context "associations with a uniq" do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: goldiloader
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joel Turkel
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-10-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -19,7 +19,7 @@ dependencies:
|
|
19
19
|
version: '3.2'
|
20
20
|
- - <
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version: '4.
|
22
|
+
version: '4.3'
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -29,7 +29,7 @@ dependencies:
|
|
29
29
|
version: '3.2'
|
30
30
|
- - <
|
31
31
|
- !ruby/object:Gem::Version
|
32
|
-
version: '4.
|
32
|
+
version: '4.3'
|
33
33
|
- !ruby/object:Gem::Dependency
|
34
34
|
name: activesupport
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|
@@ -39,7 +39,7 @@ dependencies:
|
|
39
39
|
version: '3.2'
|
40
40
|
- - <
|
41
41
|
- !ruby/object:Gem::Version
|
42
|
-
version: '4.
|
42
|
+
version: '4.3'
|
43
43
|
type: :runtime
|
44
44
|
prerelease: false
|
45
45
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -49,7 +49,7 @@ dependencies:
|
|
49
49
|
version: '3.2'
|
50
50
|
- - <
|
51
51
|
- !ruby/object:Gem::Version
|
52
|
-
version: '4.
|
52
|
+
version: '4.3'
|
53
53
|
- !ruby/object:Gem::Dependency
|
54
54
|
name: coveralls
|
55
55
|
requirement: !ruby/object:Gem::Requirement
|