blueprinter-activerecord 1.0.1 → 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: f6a85ca09945cbc4bd824aef6b755904df11ac6438efb639a7f07789ace4834d
4
- data.tar.gz: c851665fed8b689589f3cde2a176cb7a50292f48d46bcddd8b4b88790a6adb83
3
+ metadata.gz: 1a433613eafed67560509b44a75f9d8468eb98addf5262865cef731b63c2175b
4
+ data.tar.gz: 37ed9a65f70a441dfc23806b0c67bf7a1622170bb607f416e280a4739cc1c472
5
5
  SHA512:
6
- metadata.gz: 49f97296a38d1765754c31ba00c585ec78c978ca29ac4642eba4c100dc5f2cec52703349150feddca1f55cacf5a8bb2addb2dce50db33ca35c7ab20851615faf
7
- data.tar.gz: e5e92ab4e1fc3b7c9b3124d1b56573283e525539e0212bcbf73e38913dea83f8ee34775e85c3511e6b6d12f39100aff9f17d11a6b7e097e37a380fb9612f6381
6
+ metadata.gz: f5ca1480aeaae70cef964408594dd4b72b12101925daf12d8233c0c454652c451c427a0e91c430ad18612d1bb47002496289caffdb0b8cdd48d8a537056fcb36
7
+ data.tar.gz: ae53e10d27f7c93cab9920151bee1b3a10ed3d1b46b807b5420a297ee3cdfa2bae05ebae5f26ca1e5809616d05609771b5a9368537e9c980d43b6031e1f0622f
@@ -28,16 +28,17 @@ module BlueprinterActiveRecord
28
28
 
29
29
  #
30
30
  # Implements the "pre_render" Blueprinter Extension to preload associations from a view.
31
- # If auto is true, all ActiveRecord::Relation objects will be preloaded. If auto is false,
32
- # only queries that have called `.preload_blueprint` will be preloaded.
31
+ # If auto is true, all ActiveRecord::Relation and ActiveRecord::AssociationRelation objects
32
+ # will be preloaded. If auto is false, only queries that have called `.preload_blueprint`
33
+ # will be preloaded.
33
34
  #
34
35
  # NOTE: If auto is on, *don't* be concerned that you'll end up with duplicate preloads. Even if
35
36
  # the query ends up with overlapping members in 'preload' and 'includes', ActiveRecord
36
37
  # intelligently handles them. There are several unit tests which confirm this behavior.
37
38
  #
38
39
  def pre_render(object, blueprint, view, options)
39
- case object
40
- when ActiveRecord::Relation
40
+ case object.class.name
41
+ when "ActiveRecord::Relation", "ActiveRecord::AssociationRelation"
41
42
  if object.preload_blueprint_method || auto || auto_proc&.call(object, blueprint, view, options) == true
42
43
  object.before_preload_blueprint = extract_preloads object
43
44
  blueprint_preloads = self.class.preloads(blueprint, view, object.model)
@@ -56,6 +57,10 @@ module BlueprinterActiveRecord
56
57
  #
57
58
  # Returns an ActiveRecord preload plan extracted from the Blueprint and view (recursive).
58
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
+ #
59
64
  # Example:
60
65
  #
61
66
  # preloads = BlueprinterActiveRecord::Preloader.preloads(WidgetBlueprint, :extended, Widget)
@@ -68,12 +73,25 @@ module BlueprinterActiveRecord
68
73
  #
69
74
  def self.preloads(blueprint, view_name, model=nil)
70
75
  view = blueprint.reflections.fetch(view_name)
71
- 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
72
78
  ref = model ? model.reflections[assoc.name.to_s] : nil
73
79
  if (ref || model.nil?) && !assoc.blueprint.is_a?(Proc)
74
80
  ref_model = ref && !(ref.belongs_to? && ref.polymorphic?) ? ref.klass : nil
75
81
  acc[assoc.name] = preloads(assoc.blueprint, assoc.view, ref_model)
76
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
77
95
  }
78
96
  end
79
97
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BlueprinterActiveRecord
4
- VERSION = "1.0.1"
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.1
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-02-09 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
@@ -148,7 +148,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
148
148
  requirements:
149
149
  - - ">="
150
150
  - !ruby/object:Gem::Version
151
- version: '2.7'
151
+ version: '3.0'
152
152
  required_rubygems_version: !ruby/object:Gem::Requirement
153
153
  requirements:
154
154
  - - ">="