@@ -146,37 +146,43 @@ def store_accessor(store_attribute, *keys, prefix: nil, suffix: nil)
146
146
define_method ( "#{ accessor_key } _changed?" ) do
147
147
return false unless attribute_changed? ( store_attribute )
148
148
prev_store , new_store = changes [ store_attribute ]
149
- prev_store &.dig ( key ) != new_store &.dig ( key )
149
+ accessor = store_accessor_for ( store_attribute )
150
+ accessor . get ( prev_store , key ) != accessor . get ( new_store , key )
150
151
end
151
152
152
153
define_method ( "#{ accessor_key } _change" ) do
153
154
return unless attribute_changed? ( store_attribute )
154
155
prev_store , new_store = changes [ store_attribute ]
155
- [ prev_store &.dig ( key ) , new_store &.dig ( key ) ]
156
+ accessor = store_accessor_for ( store_attribute )
157
+ [ accessor . get ( prev_store , key ) , accessor . get ( new_store , key ) ]
156
158
end
157
159
158
160
define_method ( "#{ accessor_key } _was" ) do
159
161
return unless attribute_changed? ( store_attribute )
160
162
prev_store , _new_store = changes [ store_attribute ]
161
- prev_store &.dig ( key )
163
+ accessor = store_accessor_for ( store_attribute )
164
+ accessor . get ( prev_store , key )
162
165
end
163
166
164
167
define_method ( "saved_change_to_#{ accessor_key } ?" ) do
165
168
return false unless saved_change_to_attribute? ( store_attribute )
166
169
prev_store , new_store = saved_changes [ store_attribute ]
167
- prev_store &.dig ( key ) != new_store &.dig ( key )
170
+ accessor = store_accessor_for ( store_attribute )
171
+ accessor . get ( prev_store , key ) != accessor . get ( new_store , key )
168
172
end
169
173
170
174
define_method ( "saved_change_to_#{ accessor_key } " ) do
171
175
return unless saved_change_to_attribute? ( store_attribute )
172
176
prev_store , new_store = saved_changes [ store_attribute ]
173
- [ prev_store &.dig ( key ) , new_store &.dig ( key ) ]
177
+ accessor = store_accessor_for ( store_attribute )
178
+ [ accessor . get ( prev_store , key ) , accessor . get ( new_store , key ) ]
174
179
end
175
180
176
181
define_method ( "#{ accessor_key } _before_last_save" ) do
177
182
return unless saved_change_to_attribute? ( store_attribute )
178
183
prev_store , _new_store = saved_changes [ store_attribute ]
179
- prev_store &.dig ( key )
184
+ accessor = store_accessor_for ( store_attribute )
185
+ accessor . get ( prev_store , key )
180
186
end
181
187
end
182
188
end
@@ -225,39 +231,58 @@ def store_accessor_for(store_attribute)
225
231
end
226
232
227
233
class HashAccessor # :nodoc:
234
+ def self . get ( store_object , key )
235
+ if store_object
236
+ store_object [ key ]
237
+ end
238
+ end
239
+
228
240
def self . read ( object , attribute , key )
229
- prepare ( object , attribute )
230
- object . public_send ( attribute ) [ key ]
241
+ store_object = prepare ( object , attribute )
242
+ store_object [ key ]
231
243
end
232
244
233
245
def self . write ( object , attribute , key , value )
234
- prepare ( object , attribute )
235
- object . public_send ( attribute ) [ key ] = value if value != read ( object , attribute , key )
246
+ store_object = prepare ( object , attribute )
247
+ store_object [ key ] = value if value != store_object [ key ]
236
248
end
237
249
238
250
def self . prepare ( object , attribute )
239
- object . public_send :"#{ attribute } =" , { } unless object . send ( attribute )
251
+ store_object = object . public_send ( attribute )
252
+
253
+ if store_object . nil?
254
+ store_object = { }
255
+ object . public_send ( :"#{ attribute } =" , store_object )
256
+ end
257
+
258
+ store_object
240
259
end
241
260
end
242
261
243
262
class StringKeyedHashAccessor < HashAccessor # :nodoc:
263
+ def self . get ( store_object , key )
264
+ super store_object , Symbol === key ? key . name : key . to_s
265
+ end
266
+
244
267
def self . read ( object , attribute , key )
245
- super object , attribute , key . to_s
268
+ super object , attribute , Symbol === key ? key . name : key . to_s
246
269
end
247
270
248
271
def self . write ( object , attribute , key , value )
249
- super object , attribute , key . to_s , value
272
+ super object , attribute , Symbol === key ? key . name : key . to_s , value
250
273
end
251
274
end
252
275
253
276
class IndifferentHashAccessor < ActiveRecord ::Store ::HashAccessor # :nodoc:
254
- def self . prepare ( object , store_attribute )
255
- attribute = object . send ( store_attribute )
256
- unless attribute . is_a? ( ActiveSupport ::HashWithIndifferentAccess )
257
- attribute = IndifferentCoder . as_indifferent_hash ( attribute )
258
- object . public_send :"#{ store_attribute } =" , attribute
277
+ def self . prepare ( object , attribute )
278
+ store_object = object . public_send ( attribute )
279
+
280
+ unless store_object . is_a? ( ActiveSupport ::HashWithIndifferentAccess )
281
+ store_object = IndifferentCoder . as_indifferent_hash ( store_object )
282
+ object . public_send :"#{ attribute } =" , store_object
259
283
end
260
- attribute
284
+
285
+ store_object
261
286
end
262
287
end
263
288
0 commit comments