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 +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +16 -0
- data/lib/deferred_columns/record.rb +18 -0
- data/lib/deferred_columns/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 72c6f0ab8e022d76415e4d51f004e3f5b157650ae62b3c93efad601228032602
|
|
4
|
+
data.tar.gz: adace3b0ccc470d66382033d2c84d15fba35142ac7d750147845c6c7f59457ca
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
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, &)
|