store_attribute 2.1.1 → 2.3.0
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 +14 -0
- data/README.md +20 -0
- data/lib/store_attribute/active_record/store.rb +13 -0
- data/lib/store_attribute/active_record/type/typed_store.rb +17 -3
- data/lib/store_attribute/active_record.rb +5 -1
- data/lib/store_attribute/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 46c476e06aeb5cca4ab802523d9dd38c368ec49224431433dcaff06cfbb0334c
|
|
4
|
+
data.tar.gz: 13f8a345e9dc94b7c0cbc0c651ee782d5bf02105298d9d14f6b35f1cd511f308
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 10f829e18cf5c642e03a871a591a4f332686379e38b49c472e88549f8542d40926f6b186519a677a2ed1d6a2606d9d9707274c2894d124f95b7ca4ac155cd1f1
|
|
7
|
+
data.tar.gz: a6761ae641095df40c94127ec69bfe18b01b78bf27aec4d41bbe8171229bfff28ab3dc3fb368854aca5dd8c318ac380870ae1172410d24e37239740abd9ae6f5
|
data/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,20 @@
|
|
|
2
2
|
|
|
3
3
|
## master
|
|
4
4
|
|
|
5
|
+
## 2.3.0 (2026-11-24)
|
|
6
|
+
|
|
7
|
+
- Preserve store attribute defaults when the store is `nil` on Active Record 8.1+. Accessors with defaults return the default values for a `nil` (`NULL`) store, as on Rails <8.1, but no longer assign the store or mark the record dirty: reading the store directly returns `nil`. ([@HoneyryderChuck](https://github.com/HoneyryderChuck))
|
|
8
|
+
|
|
9
|
+
## 2.2.0 (2026-07-17)
|
|
10
|
+
|
|
11
|
+
- Add regression coverage for store key dirty-tracking methods (`saved_change_to_KEY?`, `KEY_changed?`, `KEY_was`, `KEY_change`, `KEY_before_last_save`), which rely on `TypedStore` delegating `#get` to the underlying store accessor (fixed in 2.0.1). ([@palkan][])
|
|
12
|
+
|
|
13
|
+
- Defer patching Active Record via `ActiveSupport.on_load(:active_record)` so requiring the gem no longer force-loads the JSON column type before app initializers run. ([@MattyMc](https://github.com/MattyMc))
|
|
14
|
+
|
|
15
|
+
- Fix Rails 8.2 compatibility (named store accessors module). ([@palkan][])
|
|
16
|
+
|
|
17
|
+
- Cast values coming from attribute defaults ([@honeyryderchuck][]).
|
|
18
|
+
|
|
5
19
|
## 2.1.1 (2026-01-13)
|
|
6
20
|
|
|
7
21
|
- Fix defining store attributes after schema loading. ([@palkan][])
|
data/README.md
CHANGED
|
@@ -8,6 +8,10 @@ Active Record extension which adds type casting support to store accessors.
|
|
|
8
8
|
|
|
9
9
|
Originally extracted from the (unmerged) PR to Ruby on Rails: [rails/rails#18942](https://github.com/rails/rails/pull/18942).
|
|
10
10
|
|
|
11
|
+
<br/>
|
|
12
|
+
|
|
13
|
+
<img src="https://cdn.evilmartians.com/badges/logo-no-label.svg" alt="Evil Martians logo" width="22" height="16" /> <b>Store Attribute</b> is built by <b><a href="https://evilmartians.com/">Evil Martians</a></b>, an American design and engineering consultancy for <b>developer tools, AI, and cybersecurity startups</b>.
|
|
14
|
+
|
|
11
15
|
### Install
|
|
12
16
|
|
|
13
17
|
In your Gemfile:
|
|
@@ -173,6 +177,22 @@ You can also configure the global default for this option in an initializer or a
|
|
|
173
177
|
StoreAttribute.store_attribute_unset_values_fallback_to_default = false
|
|
174
178
|
```
|
|
175
179
|
|
|
180
|
+
#### `NULL` stores
|
|
181
|
+
|
|
182
|
+
When the store column itself is `NULL` (e.g., a nullable column without a database default, or `extra: nil` was assigned), accessors with defaults still return the default values, but the store is left untouched:
|
|
183
|
+
|
|
184
|
+
```ruby
|
|
185
|
+
user = User.find(id) # extra is NULL in the database
|
|
186
|
+
|
|
187
|
+
user.expired_at #=> 2022-03-19
|
|
188
|
+
user.extra #=> nil
|
|
189
|
+
user.changed? #=> false
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
This preserves the behaviour of Rails versions prior to 8.1, where reading an accessor implicitly assigned an empty hash (populated with defaults) to a `nil` store. Starting from Rails 8.1, Active Record no longer does that, so reading the store directly returns `nil`, while accessors keep returning defaults.
|
|
193
|
+
|
|
194
|
+
To keep the store and its accessors consistent, prefer `NOT NULL` store columns (with `{}` as a database default) and use `{}` instead of `nil` to reset a store.
|
|
195
|
+
|
|
176
196
|
## Contributing
|
|
177
197
|
|
|
178
198
|
Bug reports and pull requests are welcome on GitHub at [https://github.com/palkan/store_attribute](https://github.com/palkan/store_attribute).
|
|
@@ -272,6 +272,19 @@ module ActiveRecord
|
|
|
272
272
|
end
|
|
273
273
|
end
|
|
274
274
|
end
|
|
275
|
+
|
|
276
|
+
unless method_defined?(:_store_accessors_module)
|
|
277
|
+
# Rails 8.2 introduced named accessors module
|
|
278
|
+
def _store_accessors_module
|
|
279
|
+
if const_defined?(:GeneratedStoreMethods, false)
|
|
280
|
+
const_get(:GeneratedStoreMethods, false)
|
|
281
|
+
else
|
|
282
|
+
mod = const_set(:GeneratedStoreMethods, Module.new)
|
|
283
|
+
include mod # rubocop:disable Layout/EmptyLinesAfterModuleInclusion
|
|
284
|
+
mod
|
|
285
|
+
end
|
|
286
|
+
end
|
|
287
|
+
end
|
|
275
288
|
end
|
|
276
289
|
end
|
|
277
290
|
end
|
|
@@ -52,7 +52,7 @@ module ActiveRecord
|
|
|
52
52
|
if hash.key?(key)
|
|
53
53
|
hash[key] = type.deserialize(hash[key])
|
|
54
54
|
elsif fallback_to_default?(key)
|
|
55
|
-
hash[key] = built_defaults[key]
|
|
55
|
+
hash[key] = type.deserialize(built_defaults[key])
|
|
56
56
|
end
|
|
57
57
|
end
|
|
58
58
|
hash
|
|
@@ -82,7 +82,7 @@ module ActiveRecord
|
|
|
82
82
|
if hash.key?(key)
|
|
83
83
|
hash[key] = type.cast(hash[key])
|
|
84
84
|
elsif fallback_to_default?(key)
|
|
85
|
-
hash[key] = built_defaults[key]
|
|
85
|
+
hash[key] = type.cast(built_defaults[key])
|
|
86
86
|
end
|
|
87
87
|
end
|
|
88
88
|
hash
|
|
@@ -101,7 +101,13 @@ module ActiveRecord
|
|
|
101
101
|
store_accessor.write(object, attribute, key, value)
|
|
102
102
|
end
|
|
103
103
|
|
|
104
|
-
|
|
104
|
+
def read(object, attribute, key)
|
|
105
|
+
return default_for(key) if object.public_send(attribute).nil?
|
|
106
|
+
|
|
107
|
+
store_accessor.read(object, attribute, key)
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
delegate :get, :prepare, to: :store_accessor
|
|
105
111
|
|
|
106
112
|
def build_defaults
|
|
107
113
|
defaults.transform_values do |val|
|
|
@@ -118,6 +124,14 @@ module ActiveRecord
|
|
|
118
124
|
|
|
119
125
|
protected
|
|
120
126
|
|
|
127
|
+
def default_for(key)
|
|
128
|
+
key = key.to_s
|
|
129
|
+
return unless fallback_to_default?(key)
|
|
130
|
+
|
|
131
|
+
value = built_defaults[key].deep_dup
|
|
132
|
+
typed?(key) ? type_for(key).cast(value) : value
|
|
133
|
+
end
|
|
134
|
+
|
|
121
135
|
def built_defaults
|
|
122
136
|
@built_defaults ||= build_defaults
|
|
123
137
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: store_attribute
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 2.
|
|
4
|
+
version: 2.3.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- palkan
|
|
@@ -29,14 +29,14 @@ dependencies:
|
|
|
29
29
|
requirements:
|
|
30
30
|
- - ">="
|
|
31
31
|
- !ruby/object:Gem::Version
|
|
32
|
-
version: 0.
|
|
32
|
+
version: 0.2.2
|
|
33
33
|
type: :development
|
|
34
34
|
prerelease: false
|
|
35
35
|
version_requirements: !ruby/object:Gem::Requirement
|
|
36
36
|
requirements:
|
|
37
37
|
- - ">="
|
|
38
38
|
- !ruby/object:Gem::Version
|
|
39
|
-
version: 0.
|
|
39
|
+
version: 0.2.2
|
|
40
40
|
- !ruby/object:Gem::Dependency
|
|
41
41
|
name: rake
|
|
42
42
|
requirement: !ruby/object:Gem::Requirement
|