store_attribute 1.3.1 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +12 -0
- data/README.md +18 -4
- data/lib/store_attribute/active_record/store.rb +9 -17
- data/lib/store_attribute/active_record/type/typed_store.rb +2 -1
- data/lib/store_attribute/version.rb +1 -1
- data/lib/store_attribute.rb +7 -0
- 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: 8a7e7f09f536e3ca33ac3ade96579b1e21d44dc4d40ffa24d9b2450a6c91e9e1
|
4
|
+
data.tar.gz: e8fd0c4de5b2ecfdd0f2f3154ffa37d5ff5bf11cbf4c225c7681ec5294de3b78
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f3ed249e25a049a2a8cbe728f45e586c4504f5604e26c40914e1bfc5c6512b4929cae2a59141e2b198e81627ad4fc0504f6104bd9b58fc6c526699358c3f04d5
|
7
|
+
data.tar.gz: 906e39d1c5304a09688650afb5365b53363972cdd684cb1bfd58fe6ebf588cfd406309d3907b7766144510bb9325ba1102e10ede0c9e29e3291cde4988f902ad
|
data/CHANGELOG.md
CHANGED
@@ -2,6 +2,18 @@
|
|
2
2
|
|
3
3
|
## master
|
4
4
|
|
5
|
+
## 2.0.0 (2024-12-12)
|
6
|
+
|
7
|
+
- **Breaking:** The `store_attribute_unset_values_fallback_to_default` option is now true by default, meaning that the default value will be returned when the attribute key is not present in the serialized value.
|
8
|
+
|
9
|
+
For v1.x behavior, set the option to `false` globally as follows:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
StoreAttribute.store_attribute_unset_values_fallback_to_default = false
|
13
|
+
```
|
14
|
+
|
15
|
+
- Ruby >= 3.0 is required.
|
16
|
+
|
5
17
|
## 1.3.1 (2024-09-19)
|
6
18
|
|
7
19
|
- Populate missing defaults on user input when `store_attribute_unset_values_fallback_to_default` is true. ([@palkan][])
|
data/README.md
CHANGED
@@ -146,15 +146,29 @@ another_user.name #=> nil
|
|
146
146
|
another_user.expired_at #=> nil
|
147
147
|
```
|
148
148
|
|
149
|
-
|
149
|
+
By default, Store Attribute returns the default value even when the record is persisted but the attribute name is not present:
|
150
|
+
|
151
|
+
```ruby
|
152
|
+
user = User.create!(extra: {})
|
153
|
+
user.expired_at #=> 2022-03-19
|
154
|
+
```
|
155
|
+
|
156
|
+
You can disable this behaviour by setting the `store_attribute_unset_values_fallback_to_default` class option to `false` in your model:
|
150
157
|
|
151
158
|
```ruby
|
152
159
|
class User < ApplicationRecord
|
153
|
-
self.store_attribute_unset_values_fallback_to_default =
|
160
|
+
self.store_attribute_unset_values_fallback_to_default = false
|
154
161
|
end
|
155
162
|
|
156
163
|
user = User.create!(extra: {})
|
157
|
-
user.expired_at #=>
|
164
|
+
user.expired_at #=> nil
|
158
165
|
```
|
159
166
|
|
160
|
-
|
167
|
+
You can also configure the global default for this option in an initializer or application configuration:
|
168
|
+
|
169
|
+
```ruby
|
170
|
+
# config/initializers/store_attribute.rb
|
171
|
+
# # or
|
172
|
+
# config/application.rb
|
173
|
+
StoreAttribute.store_attribute_unset_values_fallback_to_default = false
|
174
|
+
```
|
@@ -142,7 +142,7 @@ module ActiveRecord
|
|
142
142
|
if superclass.respond_to?(:store_attribute_unset_values_fallback_to_default)
|
143
143
|
superclass.store_attribute_unset_values_fallback_to_default
|
144
144
|
else
|
145
|
-
|
145
|
+
StoreAttribute.store_attribute_unset_values_fallback_to_default
|
146
146
|
end
|
147
147
|
end
|
148
148
|
|
@@ -170,23 +170,8 @@ module ActiveRecord
|
|
170
170
|
|
171
171
|
owner = self
|
172
172
|
|
173
|
-
# For Rails <6.1
|
174
|
-
if respond_to?(:decorate_attribute_type) && method(:decorate_attribute_type).parameters.count { |type, _| type == :req } == 2
|
175
|
-
decorate_attribute_type(attr_name, "typed_accessor_for_#{attr_name}") do |subtype|
|
176
|
-
subtypes = _local_typed_stored_attributes[attr_name][:types]
|
177
|
-
type = Type::TypedStore.create_from_type(subtype)
|
178
|
-
type.owner = owner
|
179
|
-
defaultik.type = type
|
180
|
-
subtypes.each do |name, (cast_type, options)|
|
181
|
-
type.add_typed_key(name, cast_type, **options.symbolize_keys)
|
182
|
-
end
|
183
|
-
|
184
|
-
define_default_attribute(attr_name, defaultik.proc, type, from_user: true)
|
185
|
-
|
186
|
-
type
|
187
|
-
end
|
188
173
|
# Rails >7.1
|
189
|
-
|
174
|
+
if respond_to?(:decorate_attributes)
|
190
175
|
decorate_attributes([attr_name]) do |_, subtype|
|
191
176
|
subtypes = _local_typed_stored_attributes[attr_name][:types]
|
192
177
|
type = Type::TypedStore.create_from_type(subtype)
|
@@ -215,6 +200,13 @@ module ActiveRecord
|
|
215
200
|
type.add_typed_key(name, cast_type, **options.symbolize_keys)
|
216
201
|
end
|
217
202
|
|
203
|
+
# Make sure default attribute uses the correct type, so #changed? works as expected
|
204
|
+
# This is dirty hack that makes Rails <7.2 works similar to Rails >=7.2. Please, upgrade :)
|
205
|
+
if type.defaults.any? && _default_attributes[attr_name] && !_default_attributes[attr_name].type.is_a?(Type::TypedStore)
|
206
|
+
_default_attributes[attr_name] =
|
207
|
+
ActiveModel::Attribute.from_database(attr_name, _default_attributes[attr_name].value.deep_dup, type)
|
208
|
+
end
|
209
|
+
|
218
210
|
type
|
219
211
|
end
|
220
212
|
end
|
@@ -26,6 +26,7 @@ module ActiveRecord
|
|
26
26
|
end
|
27
27
|
|
28
28
|
attr_writer :owner
|
29
|
+
attr_reader :defaults
|
29
30
|
|
30
31
|
def initialize(subtype)
|
31
32
|
@accessor_types = {}
|
@@ -140,7 +141,7 @@ module ActiveRecord
|
|
140
141
|
subtype.accessor
|
141
142
|
end
|
142
143
|
|
143
|
-
attr_reader :accessor_types, :
|
144
|
+
attr_reader :accessor_types, :subtype, :owner
|
144
145
|
end
|
145
146
|
end
|
146
147
|
end
|
data/lib/store_attribute.rb
CHANGED
@@ -4,4 +4,11 @@ require "store_attribute/version"
|
|
4
4
|
require "store_attribute/active_record"
|
5
5
|
|
6
6
|
module StoreAttribute
|
7
|
+
class << self
|
8
|
+
# Global default value for `store_attribute_unset_values_fallback_to_default` option.
|
9
|
+
# Must be set before any model is loaded
|
10
|
+
attr_accessor :store_attribute_unset_values_fallback_to_default
|
11
|
+
end
|
12
|
+
|
13
|
+
self.store_attribute_unset_values_fallback_to_default = true
|
7
14
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: store_attribute
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- palkan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-12-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -99,7 +99,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
99
99
|
requirements:
|
100
100
|
- - ">="
|
101
101
|
- !ruby/object:Gem::Version
|
102
|
-
version:
|
102
|
+
version: 3.0.0
|
103
103
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
104
104
|
requirements:
|
105
105
|
- - ">="
|