store_attribute 2.2.0 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: afa3af4ac1b9c521e3547dffef96b70bf2b0d3c900ef2601136b2eb7d3023ea8
4
- data.tar.gz: 7aa3033868705bcff1cdf90a9533c0b0cc77f13e88380d7d7f1cca2a5327e50b
3
+ metadata.gz: 46c476e06aeb5cca4ab802523d9dd38c368ec49224431433dcaff06cfbb0334c
4
+ data.tar.gz: 13f8a345e9dc94b7c0cbc0c651ee782d5bf02105298d9d14f6b35f1cd511f308
5
5
  SHA512:
6
- metadata.gz: c97a1ecbe6e3903b1acaca20bdac15a1e105035046270aa00d93a32068e313ac20fcb6631af78c27067f2ff87c855e32b8b6e42e7a5c42a3339938cf76c4e8ef
7
- data.tar.gz: 1bc2ca346cb6356373d9a9fa99d3fc12f0571989a535096920c740b138fd38027cda366d5513c47e7788ce55698daa658b890dcfd7f647fbc79edcd14ee29a12
6
+ metadata.gz: 10f829e18cf5c642e03a871a591a4f332686379e38b49c472e88549f8542d40926f6b186519a677a2ed1d6a2606d9d9707274c2894d124f95b7ca4ac155cd1f1
7
+ data.tar.gz: a6761ae641095df40c94127ec69bfe18b01b78bf27aec4d41bbe8171229bfff28ab3dc3fb368854aca5dd8c318ac380870ae1172410d24e37239740abd9ae6f5
data/CHANGELOG.md CHANGED
@@ -2,6 +2,10 @@
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
+
5
9
  ## 2.2.0 (2026-07-17)
6
10
 
7
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][])
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).
@@ -101,7 +101,13 @@ module ActiveRecord
101
101
  store_accessor.write(object, attribute, key, value)
102
102
  end
103
103
 
104
- delegate :get, :read, :prepare, to: :store_accessor
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module StoreAttribute # :nodoc:
4
- VERSION = "2.2.0"
4
+ VERSION = "2.3.0"
5
5
  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.2.0
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.2.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.2.0
39
+ version: 0.2.2
40
40
  - !ruby/object:Gem::Dependency
41
41
  name: rake
42
42
  requirement: !ruby/object:Gem::Requirement