blueprinter-activerecord 1.0.2 → 1.1.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
  SHA256:
3
- metadata.gz: 7bec64f8859b0d2cea3346675d4b2a3279245bcb5f7060b0ba0bfcf2f3bb4d83
4
- data.tar.gz: 4fe808fc8be1d043bc18d53dbe3e6a02c40904408ed51f6a8d0fbe0fc1349b2f
3
+ metadata.gz: 1a433613eafed67560509b44a75f9d8468eb98addf5262865cef731b63c2175b
4
+ data.tar.gz: 37ed9a65f70a441dfc23806b0c67bf7a1622170bb607f416e280a4739cc1c472
5
5
  SHA512:
6
- metadata.gz: 3f20ed5466369e302e0e8d73bae2bade4fa92cd7efa6ae2bba63eb49494af7990cbc74d371f6c7f4efe782a94ae4d001fbc2dfdbce7a2358771ce6fdc748442c
7
- data.tar.gz: 86b01a869b55e52ac6cfbac55dacb149112a01409ec004d7c55e4ef79451d5716327aa4aa04b8777aef2d68e000498d66f04c813e25d098ad985948bc103ad77
6
+ metadata.gz: f5ca1480aeaae70cef964408594dd4b72b12101925daf12d8233c0c454652c451c427a0e91c430ad18612d1bb47002496289caffdb0b8cdd48d8a537056fcb36
7
+ data.tar.gz: ae53e10d27f7c93cab9920151bee1b3a10ed3d1b46b807b5420a297ee3cdfa2bae05ebae5f26ca1e5809616d05609771b5a9368537e9c980d43b6031e1f0622f
@@ -57,6 +57,10 @@ module BlueprinterActiveRecord
57
57
  #
58
58
  # Returns an ActiveRecord preload plan extracted from the Blueprint and view (recursive).
59
59
  #
60
+ # Preloads are found when one of the model's associations matches:
61
+ # 1. A Blueprint association name.
62
+ # 2. A :preload option on a field or association.
63
+ #
60
64
  # Example:
61
65
  #
62
66
  # preloads = BlueprinterActiveRecord::Preloader.preloads(WidgetBlueprint, :extended, Widget)
@@ -69,12 +73,25 @@ module BlueprinterActiveRecord
69
73
  #
70
74
  def self.preloads(blueprint, view_name, model=nil)
71
75
  view = blueprint.reflections.fetch(view_name)
72
- view.associations.each_with_object({}) { |(_name, assoc), acc|
76
+ preload_vals = view.associations.each_with_object({}) { |(_name, assoc), acc|
77
+ # look for a matching association on the model
73
78
  ref = model ? model.reflections[assoc.name.to_s] : nil
74
79
  if (ref || model.nil?) && !assoc.blueprint.is_a?(Proc)
75
80
  ref_model = ref && !(ref.belongs_to? && ref.polymorphic?) ? ref.klass : nil
76
81
  acc[assoc.name] = preloads(assoc.blueprint, assoc.view, ref_model)
77
82
  end
83
+
84
+ # look for a :preload option on the association
85
+ if (custom = assoc.options[:preload])
86
+ Helpers.merge_values custom, acc
87
+ end
88
+ }
89
+
90
+ # look for a :preload options on fields
91
+ view.fields.each_with_object(preload_vals) { |(_name, field), acc|
92
+ if (custom = field.options[:preload])
93
+ Helpers.merge_values custom, acc
94
+ end
78
95
  }
79
96
  end
80
97
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BlueprinterActiveRecord
4
- VERSION = "1.0.2"
4
+ VERSION = "1.1.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: blueprinter-activerecord
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Procore Technologies, Inc.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-05-30 00:00:00.000000000 Z
11
+ date: 2024-06-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord