Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add updated? and created? methods on SaveOperations #904

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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