Skip to content

Commit fa10113

Browse files
Persist default values of aliased attributes
1f150f6 made it so that default values are always persisted. This commit adds support for aliased attributes to that change.
1 parent 62cc449 commit fa10113

3 files changed

Lines changed: 29 additions & 7 deletions

File tree

lib/active_record/typed_store/behavior.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ module Behavior
55
extend ActiveSupport::Concern
66

77
module ClassMethods
8+
def typed_store_attribute_names
9+
typed_stores.keys.map do |name|
10+
name = name.to_s
11+
attribute_aliases[name] || name
12+
end
13+
end
14+
815
def define_attribute_methods
916
super
1017
define_typed_store_attribute_methods
@@ -78,16 +85,16 @@ def attribute_names_for_partial_inserts
7885
# Contrary to all vanilla Rails types, typedstore attribute have an inherent default
7986
# value that doesn't match the database column default.
8087
# As such we need to insert them on partial inserts even if they weren't changed.
81-
super | self.class.typed_stores.keys.map(&:to_s)
88+
super | self.class.typed_store_attribute_names
8289
end
8390

8491
def attribute_names_for_partial_updates
8592
# On partial updates we shouldn't need to force stores to be persisted. However since
8693
# we weren't persisting them for a while on insertion, we now need to gracefully deal
8794
# with existing records that may have been persisted with a `NULL` store
8895
# We use `blank?` as an heuristic to detect these.
89-
super | self.class.typed_stores.keys.map(&:to_s).select do |store|
90-
@attributes.key?(store) && @attributes[store].value_before_type_cast.blank?
96+
super | self.class.typed_store_attribute_names.select do |store|
97+
read_attribute_before_type_cast(store).blank?
9198
end
9299
end
93100
end

spec/active_record/typed_store_spec.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -958,19 +958,26 @@
958958
describe DirtyTrackingModel do
959959
it 'stores the default on creation' do
960960
model = DirtyTrackingModel.create!
961+
model = DirtyTrackingModel.find(model.id)
962+
961963
expect(model.settings_before_type_cast).to_not be_blank
964+
expect(model.legacy_settings_before_type_cast).to_not be_blank
962965
end
963966

964967
it 'handles loaded records having uninitialized defaults' do
965968
model = DirtyTrackingModel.create!
966-
DirtyTrackingModel.update_all("settings = NULL") # bypass validation
969+
DirtyTrackingModel.update_all("settings = NULL, sttgs = NULL") # bypass validation
970+
967971
model = DirtyTrackingModel.find(model.id)
968972
expect(model.settings_changed?).to be false
969973
expect(model.changes).to be_empty
970974

971975
model.update!(title: "Hello")
972-
973976
expect(model.settings_changed?).to be false
974977
expect(model.changes).to be_empty
978+
979+
model = DirtyTrackingModel.find(model.id)
980+
expect(model.settings_before_type_cast).to_not be_blank
981+
expect(model.legacy_settings_before_type_cast).to_not be_blank
975982
end
976983
end

spec/support/models.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ def self.up
9393
create_table(:dirty_tracking_models, force: true) do |t|
9494
t.string :title
9595
t.text :settings
96+
t.text :sttgs
9697

9798
t.timestamps
9899
end
@@ -190,13 +191,20 @@ class MarshalTypedStoreModel < ActiveRecord::Base
190191
]
191192

192193
class DirtyTrackingModel < ActiveRecord::Base
193-
after_update :read_active
194+
after_update :read_stores
194195

195196
typed_store(:settings) do |f|
196197
f.boolean :active, default: false, null: false
197198
end
198199

199-
def read_active
200+
alias_attribute :legacy_settings, :sttgs
201+
202+
typed_store(:legacy_settings) do |f|
203+
f.boolean :migrated, default: false, null: false
204+
end
205+
206+
def read_stores
200207
active
208+
migrated
201209
end
202210
end

0 commit comments

Comments
 (0)