From fc891d83acbaf1179a98a5963994663f0a142eb8 Mon Sep 17 00:00:00 2001 From: Kendall Gifford Date: Wed, 5 Apr 2023 10:17:43 -0600 Subject: [PATCH] Return nil when getting a version If there's no log_data on a model instance and caller tries to get a version of the model using `#at_version(v)` or `#at(version: v)`, return `nil` instead of raising `NoMethodError` (undefined method `version' for nil:NilClass). --- lib/logidze/model.rb | 1 + spec/logidze/model_spec.rb | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/lib/logidze/model.rb b/lib/logidze/model.rb index dd0a8768..1d3089bb 100644 --- a/lib/logidze/model.rb +++ b/lib/logidze/model.rb @@ -114,6 +114,7 @@ def at!(time: nil, version: nil) # Return a dirty copy of specified version of record def at_version(version) + return nil unless log_data return self if log_data.version == version log_entry = log_data.find_by_version(version) diff --git a/spec/logidze/model_spec.rb b/spec/logidze/model_spec.rb index b44443c0..9d3c0310 100644 --- a/spec/logidze/model_spec.rb +++ b/spec/logidze/model_spec.rb @@ -96,6 +96,11 @@ expect(user_old.age).to eq 0 expect(user_old.active).to eq true end + + it "returns nil if log_data is nil" do + user.log_data = nil + expect(user.at(version: 1)).to be_nil + end end describe "#at!" do