busload 0.1.1 → 0.1.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '09d9798877116d2186960b5c766122a951901fd3227aeb313b75c65a1d0d5a53'
4
- data.tar.gz: 7263aa5982b548da1edbb75e614e21b0004af69a945a0c02a10f719b1a525ef1
3
+ metadata.gz: e1048e2f379939ede513cb17721d78c590ff26d03aa6f20209c0310c7a2f4f1f
4
+ data.tar.gz: 54ce60b221db91fb6e04ab404ced358415f002aaf8fa6fdb01e10a81f0298b41
5
5
  SHA512:
6
- metadata.gz: 6b07591f0b447d971a07ed5fbc3e9acf68ac98c82f1b51063f343de42aafd1a1ee0defdd624b8ad6a38dc4c30cffc1105190d2bf615dcec0673f576d6a13c299
7
- data.tar.gz: 90f1bdd069f47353df473590ab044c98526bed448d4924bf7982eb774d21106c10e6d8f9e64b021c365db95cc1e86517822b8eb39d09fa35f2a99b297a5b5dd4
6
+ metadata.gz: f1460415af42d0a255b59ce98d3edf7ec994b89814a67f8f209bee5a6665bf095222cbf272e98c891926aff735dab792892772e5f907e81de6d3936f10e7a156
7
+ data.tar.gz: 25d5d61d874119f7efdeb7a68384986686cf7985b49e902d8754c681c61eeb2676f19cd12d52cec7878f33693010933d051792dd2e8ef5fcd3faac77838b2549
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.1.2] - 2026-12-01
4
+
5
+ - Fixed error for associations whose key column types differ
6
+ - Fixed `Busload::Preloader` skipping nested preloads under associations already loaded
7
+
3
8
  ## [0.1.1] - 2026-11-30
4
9
 
5
10
  - Fixed error for deeply nested or long-named preloads
data/README.md CHANGED
@@ -109,6 +109,7 @@ These preloads load afterwards, with Active Record as usual. Busload logs why at
109
109
  - associations through other associations
110
110
  - scopes that take the owner as an argument
111
111
  - composite keys
112
+ - keys whose column types differ, like a `uuid` id and a `varchar` foreign key
112
113
  - preloads under a relation that's eager loaded or has a custom `select`
113
114
 
114
115
  ### Bulk writes
@@ -20,7 +20,7 @@ module Busload
20
20
  elsif reflection.polymorphic? then "it's polymorphic"
21
21
  elsif reflection.through_reflection? then "it goes through another association"
22
22
  elsif reflection.scope&.arity.to_i.positive? then "its scope depends on the owner"
23
- elsif reflection.join_foreign_key.is_a?(Array) then "it has a composite key"
23
+ else key_reason
24
24
  end
25
25
  end
26
26
 
@@ -46,6 +46,15 @@ module Busload
46
46
 
47
47
  private
48
48
 
49
+ def key_reason
50
+ if reflection.join_foreign_key.is_a?(Array)
51
+ "it has a composite key"
52
+ elsif @model.columns_hash[reflection.join_foreign_key]&.sql_type !=
53
+ reflection.klass.columns_hash[reflection.join_primary_key]&.sql_type
54
+ "its key types don't match"
55
+ end
56
+ end
57
+
49
58
  # Styled like the names on Active Record's SQL log lines.
50
59
  def log_name
51
60
  ActiveSupport::LogSubscriber.colorize_logging ? "\e[1m\e[36mBusload\e[0m" : "Busload"
@@ -26,8 +26,9 @@ module Busload
26
26
  return active_record_preloader(records, associations).call unless busloadable?
27
27
  return [] if empty?
28
28
 
29
- roots = plan
30
- Load.new(roots).run if roots.any?
29
+ @roots = []
30
+ plan(records, associations)
31
+ Load.new(@roots).run if @roots.any?
31
32
  []
32
33
  end
33
34
 
@@ -37,35 +38,34 @@ module Busload
37
38
  scope.nil? && @available_records.empty? && associate_by_default
38
39
  end
39
40
 
40
- # The associations to load, each with the preloader that attaches them to
41
- # their owners.
42
- def plan
43
- records.group_by(&:class).flat_map do |model, owners|
44
- preloads.filter_map { |name, nested| plan_preload(model, owners, name, nested) }
41
+ def plan(records, associations)
42
+ records.group_by(&:class).each do |model, owners|
43
+ preloads(associations).each { |name, nested| plan_preload(model, owners, name, nested) }
45
44
  end
46
45
  end
47
46
 
48
47
  def plan_preload(model, owners, name, nested)
49
48
  association = Association.new(model, name)
50
- if (reason = association.reason)
51
- association.log_loads_separately(reason)
52
- active_record_preloader(owners, name => nested).call
53
- return
54
- end
49
+ return load_separately(association, owners, name, nested) if association.reason
55
50
 
56
- owners = owners.reject { |owner| owner.association(name).loaded? }
57
- return if owners.empty?
51
+ loaded, unloaded = owners.partition { |owner| owner.association(name).loaded? }
52
+ plan(loaded.flat_map { |owner| Array(owner.association(name).target) }, nested)
53
+ @roots << root_for(association, unloaded, nested) if unloaded.any?
54
+ end
58
55
 
59
- Load::Root.new(relation: relation_for(association, owners, nested), preloader: association.preloader(owners))
56
+ def load_separately(association, owners, name, nested)
57
+ association.log_loads_separately(association.reason)
58
+ active_record_preloader(owners, name => nested).call
60
59
  end
61
60
 
62
- def relation_for(association, owners, nested)
61
+ def root_for(association, owners, nested)
63
62
  keys = owners.filter_map { |owner| owner[association.reflection.join_foreign_key] }.uniq
64
63
  relation = association.relation_for(keys)
65
- nested.empty? ? relation : relation.preload(nested)
64
+ relation = relation.preload(nested) if nested.any?
65
+ Load::Root.new(relation:, preloader: association.preloader(owners))
66
66
  end
67
67
 
68
- def preloads
68
+ def preloads(associations)
69
69
  flatten(associations).group_by(&:first).map do |name, same|
70
70
  [name, same.filter_map(&:last)]
71
71
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Busload
4
- VERSION = "0.1.1"
4
+ VERSION = "0.1.2"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: busload
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Dougherty