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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d79c58e23fa0838516c2eab7130ce8f121941bb99355d9ad7e24dbcb94f231be
4
- data.tar.gz: 70cb3d8ff0a587f726756b2d2a1d89cd162fa49e286ee343c8311c9013a4a7f9
3
+ metadata.gz: 8a7e7f09f536e3ca33ac3ade96579b1e21d44dc4d40ffa24d9b2450a6c91e9e1
4
+ data.tar.gz: e8fd0c4de5b2ecfdd0f2f3154ffa37d5ff5bf11cbf4c225c7681ec5294de3b78
5
5
  SHA512:
6
- metadata.gz: d15c7344b6f758cea9d405a3c5f765d4e9d6e734cae8866d44030c1788bf3c889ef6e3c2dae9bdb91976a45c7a0dbe3d4439f6cb5b81c169b97eefd1b264fc58
7
- data.tar.gz: 8913905537ab1a151d6194b481ec46c17564515346dfe6d79cedb02281879b7e8b3ac906e7eb8dcacfcbab19d2cbe14a5d4097c2285d19827335c4420e0bf572
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
- It is possible to configure `store_attribute` to return the default value even when the record is persisted and the attribute name is not present. By using the `store_attribute_unset_values_fallback_to_default` class option, default values will be returned for missing keys. For example:
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 = true
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 #=> 2022-03-19
164
+ user.expired_at #=> nil
158
165
  ```
159
166
 
160
- **IMPORTANT:** Due to implementation limitations, it's not recommended to toggle the value of `store_attribute_unset_values_fallback_to_default` in sub-classes. We recommend to set this value in base classes (e.g., `ApplicationRecord`).
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
- false
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
- elsif respond_to?(:decorate_attributes)
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, :defaults, :subtype, :owner
144
+ attr_reader :accessor_types, :subtype, :owner
144
145
  end
145
146
  end
146
147
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module StoreAttribute # :nodoc:
4
- VERSION = "1.3.1"
4
+ VERSION = "2.0.0"
5
5
  end
@@ -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: 1.3.1
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-09-19 00:00:00.000000000 Z
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: 2.7.0
102
+ version: 3.0.0
103
103
  required_rubygems_version: !ruby/object:Gem::Requirement
104
104
  requirements:
105
105
  - - ">="