Skip to content

Commit

Permalink
Return nil when getting a version
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
kpg-ideon authored and palkan committed Apr 5, 2023
1 parent df941f4 commit fc891d8
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/logidze/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions spec/logidze/model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit fc891d8

Please sign in to comment.