Skip to content

Commit

Permalink
Add updated? and created? methods on SaveOperations
Browse files Browse the repository at this point in the history
Implements luckyframework#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)
  • Loading branch information
davidepaolotua committed Oct 25, 2022
1 parent 1ba99fa commit 4ce8af4
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
58 changes: 56 additions & 2 deletions spec/avram/operations/save_operation_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions src/avram/save_operation.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4ce8af4

Please sign in to comment.