Skip to content

Commit 35961e6

Browse files
authored
Reject nil overrides of state_at fields (#38)
1 parent 97a7993 commit 35961e6

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

lib/stator/integration.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ def _attempt_to_track_change(field_name)
158158
return unless @record.respond_to?(field_name)
159159
return unless @record.respond_to?("#{field_name}=")
160160
return unless @record.send(field_name.to_s).nil? || state_changed?
161-
return if @record.will_save_change_to_attribute?(field_name)
161+
return if @record.will_save_change_to_attribute?(field_name) && !@record.send(field_name.to_s).nil?
162162

163163
@record.send("#{field_name}=", (Time.zone || Time).now)
164164
end

lib/stator/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
module Stator
44

55
MAJOR = 0
6-
MINOR = 8
6+
MINOR = 9
77
PATCH = 0
88
PRERELEASE = nil
99

spec/model_spec.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,35 @@
396396
u.semiactivated_state_at.should eql(t)
397397
end
398398

399+
it "should not respect an explicitly provided nil value for a timestamp field in create" do
400+
u = User.create!(
401+
email: "doug@example.com",
402+
state: "semiactivated",
403+
semiactivated_state_at: nil
404+
)
405+
406+
u.state.should eql("semiactivated")
407+
u.semiactivated_state_at.should_not be_nil
408+
end
409+
410+
it "should not allow an explicitly provided nil value for a timestamp field to override a state transition" do
411+
u = User.create!(
412+
email: "doug@example.com",
413+
state: "semiactivated",
414+
)
415+
416+
u.state.should eql("semiactivated")
417+
418+
u.activated_state_at = Time.now
419+
u.save!
420+
421+
u.assign_attributes(activated_state_at: nil)
422+
u.activate!
423+
424+
u.state.should eql("activated")
425+
u.activated_state_at.should_not be_nil
426+
end
427+
399428
it "should allow opting into track by namespace" do
400429
z = ZooKeeper.new(name: "Doug")
401430
z.employment_state.should eql("hired")

0 commit comments

Comments
 (0)