From 4ce8af496d4b32949d34963d015872130b396f19 Mon Sep 17 00:00:00 2001 From: Davide Paolo Tua Date: Tue, 25 Oct 2022 11:31:23 +0200 Subject: [PATCH] Add updated? and created? methods on SaveOperations Implements #888. Adds two new boolean methods to SaveOperation. They work similarly to new_record? but always return false if the record was not persisted (e.g: failed save or not-yet-performed operation) --- spec/avram/operations/save_operation_spec.cr | 58 +++++++++++++++++++- src/avram/save_operation.cr | 8 +++ 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/spec/avram/operations/save_operation_spec.cr b/spec/avram/operations/save_operation_spec.cr index f22c62248..4348e1b0e 100644 --- a/spec/avram/operations/save_operation_spec.cr +++ b/spec/avram/operations/save_operation_spec.cr @@ -220,7 +220,9 @@ describe "Avram::SaveOperation" do nickname: nil, age: 30, joined_at: joined_at - ) do |_operation, user| + ) do |operation, user| + operation.created?.should be_false + operation.updated?.should be_true UserQuery.new.select_count.should eq(1) user = user.not_nil! user.id.should eq(existing_user.id) @@ -241,7 +243,9 @@ describe "Avram::SaveOperation" do nickname: "R.", age: 30, joined_at: joined_at - ) do |_operation, user| + ) do |operation, user| + operation.created?.should be_true + operation.updated?.should be_false UserQuery.new.select_count.should eq(2) # Keep existing user the same user_with_different_nickname.age.should eq(20) @@ -333,6 +337,8 @@ describe "Avram::SaveOperation" do operation.save_failed?.should be_true operation.save_status.should eq(SaveUser::OperationStatus::SaveFailed) operation.valid?.should be_false + operation.created?.should be_false + operation.updated?.should be_false end it "is false if the object is not marked as saved but no action was performed" do @@ -342,6 +348,8 @@ describe "Avram::SaveOperation" do operation.save_status.should eq(SaveUser::OperationStatus::Unperformed) operation.saved?.should be_false operation.valid?.should be_false + operation.created?.should be_false + operation.updated?.should be_false end end @@ -849,6 +857,52 @@ describe "Avram::SaveOperation" do end end + describe "#created?" do + context "after creating" do + it "returns 'true'" do + operation = SaveUser.new(name: "Dan", age: 34, joined_at: Time.utc) + + operation.created?.should be_false + operation.save.should be_true + operation.created?.should be_true + end + end + + context "after updating" do + it "returns 'false'" do + user = UserFactory.create &.name("Dan").age(34).joined_at(Time.utc) + operation = SaveUser.new(user, name: "Tom") + + operation.created?.should be_false + operation.save.should be_true + operation.created?.should be_false + end + end + end + + describe "#updated?" do + context "after creating" do + it "returns 'false'" do + operation = SaveUser.new(name: "Dan", age: 34, joined_at: Time.utc) + + operation.updated?.should be_false + operation.save.should be_true + operation.updated?.should be_false + end + end + + context "after updating" do + it "returns 'true'" do + user = UserFactory.create &.name("Dan").age(34).joined_at(Time.utc) + operation = SaveUser.new(user, name: "Tom") + + operation.updated?.should be_false + operation.save.should be_true + operation.updated?.should be_true + end + end + end + describe "skip_default_validations" do it "allows blank strings to be saved" do post = PostFactory.create diff --git a/src/avram/save_operation.cr b/src/avram/save_operation.cr index d6d5dff87..c2e23c4cb 100644 --- a/src/avram/save_operation.cr +++ b/src/avram/save_operation.cr @@ -132,6 +132,14 @@ abstract class Avram::SaveOperation(T) save_status == OperationStatus::Saved end + def created? + saved? && new_record? + end + + def updated? + saved? && !new_record? + end + # Return true if the operation has run and the record failed to save def save_failed? save_status == OperationStatus::SaveFailed