store_attribute 1.1.0 → 1.2.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: 2134f240621f2f546e5521fe4db4b66e3b6ec0a4a76ebb832fbcae8654c0904e
4
- data.tar.gz: 862ce5201d0c1ad548035161b0f51b50a9a540a85ae8be0a9c550976f918701c
3
+ metadata.gz: 601846a5ef07068e0777b17cfb7896a6bcd1fe32ef93ac107f957c568a6598de
4
+ data.tar.gz: 7ebee7679babde4188b07aa0504310001edbe31758070112b6f5c0e48eadc83d
5
5
  SHA512:
6
- metadata.gz: efe4992702c419f392f4fcbfd16c0ab32c862acf4679e841a7a8c28d40e0fb2eb66c2b72b2ac0156517a49bce4ee7308ec75ffa64604d4b879c735da0201d7f3
7
- data.tar.gz: a9b8d3683abcd37f99a7c0ef68431e80e32a79537c5dab426e150a85b7d15c4b8ce1cb92e0a6c1c24031418286223e854fb2e2c73083c801781553f6559cd2c1
6
+ metadata.gz: 64d0e3b562bd62b6e8b65b993f049b3af805d0a38f075218eaa05993f7229befbf10f21e59dd1709ddd1faa5397e20ca4f02c8800a703cee7f3db79b12a28a98
7
+ data.tar.gz: 29623e721a38b1bf646f3a45c00c605c4162236716624e3cb8a0dceee69641d254145baa84b58008e3fe61084b1eb76249a34775f186bbe86861eca827ba2856
data/CHANGELOG.md CHANGED
@@ -2,6 +2,16 @@
2
2
 
3
3
  ## master
4
4
 
5
+ ## 1.2.0 (2023-11-29)
6
+
7
+ - Support Rails >7.1. ([@palkan][])
8
+
9
+ - Fix handling of store attributes for not-yet-defined columns. ([@palkan][])
10
+
11
+ ## 1.1.1 (2023-06-27)
12
+
13
+ - Lookup store attribute types only after schema load.
14
+
5
15
  ## 1.1.0 (2023-03-08) 🌷
6
16
 
7
17
  - Add configuration option to return default values when attribute key is not present in the serialized value ([@markedmondson][], [@palkan][]).
@@ -124,7 +124,7 @@ module ActiveRecord
124
124
  _define_predicate_method(name, prefix: prefix, suffix: suffix) if type == :boolean
125
125
 
126
126
  _define_store_attribute(store_name) if !_local_typed_stored_attributes? || _local_typed_stored_attributes[store_name].empty?
127
- _store_local_stored_attribute(store_name, name, type, **options)
127
+ _local_typed_stored_attributes[store_name][name] = [type, options]
128
128
  end
129
129
 
130
130
  def store_attribute_unset_values_fallback_to_default
@@ -138,11 +138,6 @@ module ActiveRecord
138
138
  end
139
139
  end
140
140
 
141
- def _store_local_stored_attribute(store_name, key, cast_type, default: Type::TypedStore::UNDEFINED, **options) # :nodoc:
142
- cast_type = ActiveRecord::Type.lookup(cast_type, **options) if cast_type.is_a?(Symbol)
143
- _local_typed_stored_attributes[store_name][key] = [cast_type, default]
144
- end
145
-
146
141
  def _local_typed_stored_attributes?
147
142
  instance_variable_defined?(:@local_typed_stored_attributes)
148
143
  end
@@ -162,28 +157,45 @@ module ActiveRecord
162
157
 
163
158
  def _define_store_attribute(store_name)
164
159
  attr_name = store_name.to_s
165
- was_type = attributes_to_define_after_schema_loads[attr_name]&.first
166
-
167
- # For Rails <6.1
168
- use_decorator = respond_to?(:decorate_attribute_type) && method(:decorate_attribute_type).parameters.count { |type, _| type == :req } == 2
169
160
 
170
161
  defaultik = Type::TypedStore::Defaultik.new
171
162
 
172
163
  owner = self
173
164
 
174
- if use_decorator
165
+ # For Rails <6.1
166
+ if respond_to?(:decorate_attribute_type) && method(:decorate_attribute_type).parameters.count { |type, _| type == :req } == 2
175
167
  decorate_attribute_type(attr_name, "typed_accessor_for_#{attr_name}") do |subtype|
176
168
  subtypes = _local_typed_stored_attributes[attr_name]
177
169
  type = Type::TypedStore.create_from_type(subtype)
178
170
  type.owner = owner
179
171
  defaultik.type = type
180
- subtypes.each { |name, (cast_type, default)| type.add_typed_key(name, cast_type, default: default) }
172
+ subtypes.each do |name, (cast_type, options)|
173
+ type.add_typed_key(name, cast_type, **options.symbolize_keys)
174
+ end
181
175
 
182
176
  define_default_attribute(attr_name, defaultik.proc, type, from_user: true)
183
177
 
184
178
  type
185
179
  end
180
+ # Rails >7.1
181
+ elsif respond_to?(:decorate_attributes)
182
+ decorate_attributes([attr_name]) do |_, subtype|
183
+ subtypes = _local_typed_stored_attributes[attr_name]
184
+ type = Type::TypedStore.create_from_type(subtype)
185
+ type.owner = owner
186
+ defaultik.type = type
187
+ subtypes.each do |name, (cast_type, options)|
188
+ type.add_typed_key(name, cast_type, **options.symbolize_keys)
189
+ end
190
+
191
+ type
192
+ end
193
+
194
+ attribute(attr_name, default: defaultik.proc)
195
+ # Rails >=6.1, <=7.1
186
196
  else
197
+ was_type = attributes_to_define_after_schema_loads[attr_name]&.first
198
+
187
199
  attribute(attr_name, default: defaultik.proc) do |subtype|
188
200
  subtypes = _local_typed_stored_attributes[attr_name]
189
201
  subtype = _lookup_cast_type(attr_name, was_type, {}) if defined?(_lookup_cast_type)
@@ -191,7 +203,9 @@ module ActiveRecord
191
203
  type = Type::TypedStore.create_from_type(subtype)
192
204
  type.owner = owner
193
205
  defaultik.type = type
194
- subtypes.each { |name, (cast_type, default)| type.add_typed_key(name, cast_type, default: default) }
206
+ subtypes.each do |name, (cast_type, options)|
207
+ type.add_typed_key(name, cast_type, **options.symbolize_keys)
208
+ end
195
209
 
196
210
  type
197
211
  end
@@ -30,7 +30,7 @@ module ActiveRecord
30
30
  def initialize(subtype)
31
31
  @accessor_types = {}
32
32
  @defaults = {}
33
- @store_accessor = subtype.accessor
33
+ @subtype = subtype
34
34
  super(subtype)
35
35
  end
36
36
 
@@ -118,7 +118,7 @@ module ActiveRecord
118
118
  def key_to_cast(val, key)
119
119
  return key if val.key?(key)
120
120
  return key.to_sym if val.key?(key.to_sym)
121
- return key if defaults.key?(key)
121
+ key if defaults.key?(key)
122
122
  end
123
123
 
124
124
  def typed?(key)
@@ -133,7 +133,11 @@ module ActiveRecord
133
133
  owner&.store_attribute_unset_values_fallback_to_default && defaults.key?(key)
134
134
  end
135
135
 
136
- attr_reader :accessor_types, :defaults, :store_accessor, :owner
136
+ def store_accessor
137
+ subtype.accessor
138
+ end
139
+
140
+ attr_reader :accessor_types, :defaults, :subtype, :owner
137
141
  end
138
142
  end
139
143
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module StoreAttribute # :nodoc:
4
- VERSION = "1.1.0"
4
+ VERSION = "1.2.0"
5
5
  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.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - palkan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-03-08 00:00:00.000000000 Z
11
+ date: 2023-11-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -106,7 +106,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
106
106
  - !ruby/object:Gem::Version
107
107
  version: '0'
108
108
  requirements: []
109
- rubygems_version: 3.4.6
109
+ rubygems_version: 3.4.20
110
110
  signing_key:
111
111
  specification_version: 4
112
112
  summary: ActiveRecord extension which adds typecasting to store accessors