batch-loader-active-record 0.2.0 → 0.3.0

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: c7c24c7d661f0d2449d676892c1d328f8c5147e3
4
- data.tar.gz: 83d9fa13f7dcab069ed2b1d1f11fc4e1f36834f9
3
+ metadata.gz: 21895ec7eb240edbb662157510b7c1f2e0e09ece
4
+ data.tar.gz: 73a9d404f37d37432b74dc34156614f591cfd4c5
5
5
  SHA512:
6
- metadata.gz: 0677e7a74dfe10a238bd431d171d44bbff07b3c8a6f775bfbe9bf595f345ea0fce7c35a494eceb7702e5abb56ffcada2ea426c23097286a77bfb200a0022ebb3
7
- data.tar.gz: 0e0e3edd2866a7c578567f5171e23fae3356b8469ada0123781d5d7b0276c655af33149baa1da985e8c4d198b8ddf3ad48f961b38609b03ed959ea7dfaeff58b
6
+ metadata.gz: 5917a41861fd8b5bced3f6b22f5d981028615fb7e6a4a0f674fde403261d6d29da4dbb8d2327a873cae3eb750f5ffee034d819507ca004e67b44d3d4f2b9ebeb
7
+ data.tar.gz: d03ff1d186f23a10d3f65766ab8ac57555e91b0bd2e7647751e3d3f0ece000c221a5313050abc2a9633f79a460056e4b2564e023053bd5fb9ce824a3fc85404b
@@ -0,0 +1,21 @@
1
+ # Changelog
2
+
3
+ Unreleased
4
+
5
+ * nothing yet
6
+
7
+ v0.3.0
8
+
9
+ * allow to specify an association scope with `belongs_to_lazy`, `has_one_lazy` and `has_many_lazy`
10
+
11
+ v0.2.0
12
+
13
+ * allow to use `has_many_lazy` with `through: ...` option
14
+
15
+ v0.1.0
16
+
17
+ * initial release
18
+ * doens't support `has_and_belongs_to_lazy`
19
+ * doesn't support `has_many_lazy ... through: ...`
20
+ * doesn't support association scope
21
+ * doesn't support polymorphic associations
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- batch-loader-active-record (0.2.0)
4
+ batch-loader-active-record (0.3.0)
5
5
  activerecord (>= 4.2.0, < 5.2.0)
6
6
  activesupport (>= 4.2.0, < 5.2.0)
7
7
  batch-loader (~> 1.2.0)
data/README.md CHANGED
@@ -12,7 +12,7 @@ It is not intended to be used for all associations though, but only where necess
12
12
 
13
13
  ## Description
14
14
 
15
- This is a very simple gem which is basically a mixin containg replacement macros for the 3 active record association macros (note that **polymorphic associations and association scopes are not yet supported**):
15
+ This is a very simple gem which is basically a mixin containg replacement macros for the 3 active record association macros (refer to the [CHANGELOG](https://github.com/mathieul/batch-loader-active-record/blob/master/CHANGELOG.md) to know what is supported and what is not):
16
16
 
17
17
  * `belongs_to_lazy`
18
18
  * `has_one_lazy`
@@ -12,11 +12,17 @@ module BatchLoaderActiveRecord
12
12
  def belongs_to_lazy(*args)
13
13
  belongs_to(*args).tap do |reflections|
14
14
  reflect = reflections.values.last
15
+ assert_not_polymorphic(reflect)
15
16
  batch_key = [table_name, reflect.name]
16
17
  define_method(:"#{reflect.name}_lazy") do
18
+ assoc_scope = if reflect.scope.nil?
19
+ reflect.klass
20
+ else
21
+ reflect.klass.instance_eval(&reflect.scope)
22
+ end
17
23
  foreign_key_value = send(reflect.foreign_key) or return nil
18
24
  BatchLoader.for(foreign_key_value).batch(key: batch_key) do |foreign_key_values, loader|
19
- reflect.klass.where(id: foreign_key_values).each { |instance| loader.call(instance.id, instance) }
25
+ assoc_scope.where(id: foreign_key_values).each { |instance| loader.call(instance.id, instance) }
20
26
  end
21
27
  end
22
28
  end
@@ -25,10 +31,16 @@ module BatchLoaderActiveRecord
25
31
  def has_one_lazy(*args)
26
32
  has_one(*args).tap do |reflections|
27
33
  reflect = reflections.values.last
34
+ assert_not_polymorphic(reflect)
28
35
  batch_key = [table_name, reflect.name]
29
36
  define_method(:"#{reflect.name}_lazy") do
37
+ assoc_scope = if reflect.scope.nil?
38
+ reflect.klass
39
+ else
40
+ reflect.klass.instance_eval(&reflect.scope)
41
+ end
30
42
  BatchLoader.for(id).batch(key: batch_key) do |model_ids, loader|
31
- reflect.klass.where(reflect.foreign_key => model_ids).each do |instance|
43
+ assoc_scope.where(reflect.foreign_key => model_ids).each do |instance|
32
44
  loader.call(instance.public_send(reflect.foreign_key), instance)
33
45
  end
34
46
  end
@@ -39,12 +51,22 @@ module BatchLoaderActiveRecord
39
51
  def has_many_lazy(*args)
40
52
  has_many(*args).tap do |reflections|
41
53
  reflect = reflections.values.last
54
+ assert_not_polymorphic(reflect)
42
55
  base_key = [table_name, reflect.name]
43
56
  define_method(:"#{reflect.name}_lazy") do |instance_scope = nil|
44
57
  batch_key = base_key
45
58
  batch_key += [instance_scope.to_sql.hash] unless instance_scope.nil?
59
+ assoc_scope = if reflect.scope.nil?
60
+ reflect.klass
61
+ else
62
+ reflect.klass.instance_eval(&reflect.scope)
63
+ end
46
64
  BatchLoader.for(id).batch(default_value: [], key: batch_key) do |model_ids, loader|
47
- relation = instance_scope || reflect.klass
65
+ relation = if instance_scope.nil?
66
+ assoc_scope
67
+ else
68
+ assoc_scope.instance_eval { instance_scope }
69
+ end
48
70
  if reflect.through_reflection?
49
71
  instances = self.class.fetch_for_model_ids(model_ids, relation: relation, reflection: reflect)
50
72
  instances.each do |instance|
@@ -76,6 +98,14 @@ module BatchLoaderActiveRecord
76
98
  .select("#{relation.table_name}.*, #{instance_id_path} AS _instance_id")
77
99
  end
78
100
 
101
+ private
102
+
103
+ def assert_not_polymorphic(reflection)
104
+ if reflection.polymorphic? || reflection.options.has_key?(:as) || reflection.options.has_key?(:source_type)
105
+ raise NotImplementedError, "polymorphic associations are not yet supported (#{reflection.name})"
106
+ end
107
+ end
108
+
79
109
  def reflection_chain(reflection)
80
110
  reflections = [reflection]
81
111
  begin
@@ -1,3 +1,3 @@
1
1
  module BatchLoaderActiveRecord
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: batch-loader-active-record
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - mathieul
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-11-23 00:00:00.000000000 Z
11
+ date: 2017-11-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: batch-loader
@@ -145,6 +145,7 @@ files:
145
145
  - ".gitignore"
146
146
  - ".rspec"
147
147
  - ".travis.yml"
148
+ - CHANGELOG.md
148
149
  - CODE_OF_CONDUCT.md
149
150
  - Gemfile
150
151
  - Gemfile.lock