deferred_columns 0.0.3 → 0.0.4

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: b5ee4e024494b9c3bf5677b3db75ad6b45e0d04720c1bc2531f785913cfeb529
4
- data.tar.gz: 9dcbd4c5df5a0c335fb0e3c1e4241faebc7d0b9a6ac60abde0e58f908bde8e12
3
+ metadata.gz: 72c6f0ab8e022d76415e4d51f004e3f5b157650ae62b3c93efad601228032602
4
+ data.tar.gz: adace3b0ccc470d66382033d2c84d15fba35142ac7d750147845c6c7f59457ca
5
5
  SHA512:
6
- metadata.gz: ec342be182915bf2caa13dff2de5f8d0c8795f97a7bca974165555b0486bc1a30a6e222972efd17d39550fb974658ad96c032428ac5d325332106a988789830a
7
- data.tar.gz: 93998c8e593c3c7add42080b1ec95896b63c63e932bde96bcd318129e166dea204b4bac0e71c7ed64826cdcbf885af8b101a8e2830317b8fcf33d5111043ca65
6
+ metadata.gz: 7f83d634c4f3e9146a6dad76a1bf4ebc7af57c1e50bb704e612cf160db8ab2fe3101058781419e4c43d9b66052531e4c7474385f92d220cbf5543d96f61085ed
7
+ data.tar.gz: 5165067ee8dc2322d92d4ca3b25d5c05deaebdb79d3a696920dc1331e9e0f6fdd7ba829a6f35de2028bbcda4a4e1aeb94e60b9c69835612a3318c14ef0645960
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.0.4 (2026-11-19)
4
+
5
+ - Add `unload_deferred`, to forget deferred columns in records created in tests, so they behave like records loaded by a query.
6
+
3
7
  ## 0.0.3 (2026-11-18)
4
8
 
5
9
  - Fix `NoMethodError` for the private `construct_join_dependency` when merging joins of another model, or in association scopes that reference an eager loaded association.
data/README.md CHANGED
@@ -125,6 +125,22 @@ DeferredColumns.load(album.songs, :lyrics)
125
125
 
126
126
  Both forms accept group names and leave values you've already loaded or assigned alone.
127
127
 
128
+ ### Testing
129
+
130
+ A record created in a test keeps every column in memory, so a test passes even
131
+ when the code under test reads a deferred column that production never loads.
132
+ Use `unload_deferred` to forget them, as if the record had been loaded by a query:
133
+
134
+ ```ruby
135
+ FactoryBot.define do
136
+ factory :song do
137
+ after(:create, &:unload_deferred)
138
+ end
139
+ end
140
+ ```
141
+
142
+ A test that does want the columns in memory calls `song.load_deferred(:lyrics)`.
143
+
128
144
  ### Reading deferred columns
129
145
 
130
146
  ```ruby
@@ -13,6 +13,24 @@ module DeferredColumns
13
13
  self
14
14
  end
15
15
 
16
+ # Forgets deferred columns or groups, as if the record had been loaded
17
+ # without them. Without arguments, forgets every deferred column.
18
+ #
19
+ # Meant for tests: a record created in a test keeps every column in memory,
20
+ # so code that reads a deferred column it never loaded would pass the test
21
+ # and fail in production.
22
+ def unload_deferred(*names)
23
+ columns = DeferredColumns.deferred_columns!(self.class, names)
24
+ columns = self.class.deferred_columns if columns.empty?
25
+ changed = columns & changed_attribute_names_to_save
26
+ raise ArgumentError, "#{self.class.name} has unsaved changes to #{changed.join(', ')}" if changed.any?
27
+
28
+ columns.each do |column|
29
+ @attributes[column] = ActiveModel::Attribute.uninitialized(column, self.class.type_for_attribute(column))
30
+ end
31
+ self
32
+ end
33
+
16
34
  # Raises instead of returning nil for deferred columns that weren't loaded,
17
35
  # which also applies to `record[column]`.
18
36
  def read_attribute(name, &)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DeferredColumns
4
- VERSION = '0.0.3'
4
+ VERSION = '0.0.4'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: deferred_columns
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Maximo Mussini