From 03728d89e713b52d6fed402eec04663a32cb74d8 Mon Sep 17 00:00:00 2001 From: matthewmcgarvey Date: Tue, 13 Jul 2021 17:39:10 -0400 Subject: [PATCH 1/2] Rename destroy to delete --- .../delete_operation_callbacks_spec.cr | 2 +- spec/operations/delete_operation_spec.cr | 22 ++++++------ spec/operations/operation_needs_spec.cr | 6 ++-- src/avram/errors.cr | 2 +- .../needy_initializer_and_delete_methods.cr | 36 ++++++------------- 5 files changed, 27 insertions(+), 41 deletions(-) diff --git a/spec/operations/delete_operation_callbacks_spec.cr b/spec/operations/delete_operation_callbacks_spec.cr index 520a2bd90..9daf9cafd 100644 --- a/spec/operations/delete_operation_callbacks_spec.cr +++ b/spec/operations/delete_operation_callbacks_spec.cr @@ -35,7 +35,7 @@ describe "Avram::DeleteOperation callbacks" do it "runs before_delete and after_delete callbacks" do user = UserFactory.create &.name("Jerry") - DeleteOperationWithCallbacks.destroy(user) do |operation, deleted_user| + DeleteOperationWithCallbacks.delete(user) do |operation, deleted_user| deleted_user.not_nil!.name.should eq "Jerry" operation.callbacks_that_ran.should contain "before_delete_update_number" operation.callbacks_that_ran.should contain "before_delete_in_a_block" diff --git a/spec/operations/delete_operation_spec.cr b/spec/operations/delete_operation_spec.cr index 719857ff5..79d4c5464 100644 --- a/spec/operations/delete_operation_spec.cr +++ b/spec/operations/delete_operation_spec.cr @@ -24,11 +24,11 @@ private class DeleteOperationWithAccessToModelValues < Post::DeleteOperation end describe "Avram::DeleteOperation" do - describe "destroy" do + describe "delete" do it "deletes the specified record" do user = UserFactory.create - BasicDeleteUser.destroy(user) do |operation, deleted_user| + BasicDeleteUser.delete(user) do |operation, deleted_user| operation.valid?.should be_true operation.delete_status.should eq BasicDeleteUser::DeleteStatus::Deleted deleted_user.not_nil!.name.should eq user.name @@ -39,7 +39,7 @@ describe "Avram::DeleteOperation" do it "does not delete if the operation is invalid" do user = UserFactory.create - FailedToDeleteUser.destroy(user) do |operation, deleted_user| + FailedToDeleteUser.delete(user) do |operation, deleted_user| operation.valid?.should be_false operation.delete_status.should eq FailedToDeleteUser::DeleteStatus::DeleteFailed deleted_user.should eq nil @@ -49,11 +49,11 @@ describe "Avram::DeleteOperation" do end end - describe "destroy!" do + describe "delete!" do it "deletes the specified record" do user = UserFactory.create - deleted_user = BasicDeleteUser.destroy!(user) + deleted_user = BasicDeleteUser.delete!(user) deleted_user.name.should eq user.name UserQuery.new.select_count.should eq 0 end @@ -62,7 +62,7 @@ describe "Avram::DeleteOperation" do user = UserFactory.create expect_raises(Avram::InvalidOperationError) do - FailedToDeleteUser.destroy!(user) + FailedToDeleteUser.delete!(user) end end end @@ -71,7 +71,7 @@ describe "Avram::DeleteOperation" do it "returns a soft deleted object" do item = SoftDeletableItemFactory.create - deleted_item = SoftDeleteItem.destroy!(item) + deleted_item = SoftDeleteItem.delete!(item) deleted_item.soft_deleted?.should be_true SoftDeletableItem::BaseQuery.new.find(deleted_item.id).should eq item @@ -85,7 +85,7 @@ describe "Avram::DeleteOperation" do EmailAddress::BaseQuery.new.select_count.should eq(1) - DeleteWithCascade.destroy(business) do |operation, _deleted_business| + DeleteWithCascade.delete(business) do |operation, _deleted_business| operation.deleted?.should be_true EmailAddress::BaseQuery.new.select_count.should eq(0) end @@ -101,7 +101,7 @@ describe "Avram::DeleteOperation" do user = UserFactory.create - BasicDeleteUser.destroy!(user) + BasicDeleteUser.delete!(user) events.map(&.operation_class).should contain("BasicDeleteUser") end @@ -114,7 +114,7 @@ describe "Avram::DeleteOperation" do user = UserFactory.create expect_raises(Avram::InvalidOperationError) do - FailedToDeleteUser.destroy!(user) + FailedToDeleteUser.delete!(user) end events.map(&.operation_class).should contain("FailedToDeleteUser") @@ -128,7 +128,7 @@ describe "Avram::DeleteOperation" do it "adds the error and fails to save" do post = PostFactory.create &.title("sandbox") - DeleteOperationWithAccessToModelValues.destroy(post) do |operation, _deleted_post| + DeleteOperationWithAccessToModelValues.delete(post) do |operation, _deleted_post| operation.deleted?.should be_false operation.errors[:title].should contain("You can't delete your sandbox") end diff --git a/spec/operations/operation_needs_spec.cr b/spec/operations/operation_needs_spec.cr index 4f22d34e3..aa431adc9 100644 --- a/spec/operations/operation_needs_spec.cr +++ b/spec/operations/operation_needs_spec.cr @@ -103,11 +103,11 @@ describe "Avram::SaveOperation needs" do end describe "Avram::DeleteOperation needs" do - it "sets up a method arg for destroy" do + it "sets up a method arg for delete" do user = UserFactory.create post = PostFactory.create - NeedyDeleteOperation.destroy(post, user: user, notification_message: "is this thing on?") do |operation, _record| + NeedyDeleteOperation.delete(post, user: user, notification_message: "is this thing on?") do |operation, _record| operation.notification_message.should eq("is this thing on?") operation.no_number.should eq(4) operation.user.should eq(user) @@ -119,7 +119,7 @@ describe "Avram::DeleteOperation needs" do user = UserFactory.create post = PostFactory.create - NeedyDeleteOperation.destroy(post, params, user: user, notification_message: nil) do |operation, _record| + NeedyDeleteOperation.delete(post, params, user: user, notification_message: nil) do |operation, _record| operation.notification_message.should be_nil operation.no_number.should eq(4) operation.user.should eq(user) diff --git a/src/avram/errors.cr b/src/avram/errors.cr index d69db37f1..c927f648e 100644 --- a/src/avram/errors.cr +++ b/src/avram/errors.cr @@ -37,7 +37,7 @@ module Avram end end - # Raised when using the create!, update!, or destroy! methods on an operation when it does not have the proper attributes + # Raised when using the create!, update!, or delete! methods on an operation when it does not have the proper attributes class InvalidOperationError < AvramError getter errors : Hash(Symbol, Array(String)) diff --git a/src/avram/needy_initializer_and_delete_methods.cr b/src/avram/needy_initializer_and_delete_methods.cr index 5d56725ff..a7cc840ad 100644 --- a/src/avram/needy_initializer_and_delete_methods.cr +++ b/src/avram/needy_initializer_and_delete_methods.cr @@ -30,14 +30,14 @@ module Avram::NeedyInitializerAndDeleteMethods macro finished # This is called at the end so @type will be of the subclass, # and not the parent abstract class. - generate_initializer_and_destroy_methods + generate_initializer_and_delete_methods end end - macro generate_initializer_and_destroy_methods + macro generate_initializer_and_delete_methods # Build up a list of method arguments # - # These method arguments can be used in macros for generating destroy/new + # These method arguments can be used in macros for generating delete/new # # This way everything has a name and type and we don't have to rely on # **named_args. **named_args** are easy but you get horrible type errors. @@ -91,22 +91,8 @@ module Avram::NeedyInitializerAndDeleteMethods %} end - macro generate_destroy(attribute_method_args, attribute_params, with_params, with_bang) - def self.delete{% if with_bang %}!{% end %}(*args, **named_args{% if !with_bang %}, &block{% end %}) - {% verbatim do %} - {% raise <<-ERROR - DeleteOperations do not have a 'delete' method. - - Try this... - - ▸ Use 'destroy' to delete a record - - ERROR - %} - {% end %} - end - - def self.destroy{% if with_bang %}!{% end %}( + macro generate_delete(attribute_method_args, attribute_params, with_params, with_bang) + def self.delete{% if with_bang %}!{% end %}( record : T, params : Hash, **named_args @@ -115,10 +101,10 @@ module Avram::NeedyInitializerAndDeleteMethods {% else %} yield nil, nil {% end %} - hash_is_not_allowed_helpful_error(:destroy{% if with_bang %}!{% end %}, additional_args: "record, ") + hash_is_not_allowed_helpful_error(:delete{% if with_bang %}!{% end %}, additional_args: "record, ") end - def self.destroy{% if with_bang %}!{% end %}( + def self.delete{% if with_bang %}!{% end %}( record : T, {% if with_params %}params,{% end %} {% for type_declaration in OPERATION_NEEDS %} @@ -148,10 +134,10 @@ module Avram::NeedyInitializerAndDeleteMethods end macro generate_delete_methods(attribute_method_args, attribute_params) - generate_destroy({{ attribute_method_args }}, {{ attribute_params }}, with_params: true, with_bang: true) - generate_destroy({{ attribute_method_args }}, {{ attribute_params }}, with_params: true, with_bang: false) - generate_destroy({{ attribute_method_args }}, {{ attribute_params }}, with_params: false, with_bang: true) - generate_destroy({{ attribute_method_args }}, {{ attribute_params }}, with_params: false, with_bang: false) + generate_delete({{ attribute_method_args }}, {{ attribute_params }}, with_params: true, with_bang: true) + generate_delete({{ attribute_method_args }}, {{ attribute_params }}, with_params: true, with_bang: false) + generate_delete({{ attribute_method_args }}, {{ attribute_params }}, with_params: false, with_bang: true) + generate_delete({{ attribute_method_args }}, {{ attribute_params }}, with_params: false, with_bang: false) end macro generate_initializer(attribute_method_args, attribute_params) From 9ac4b1198624a2492c31b743b8ae20e768ecf026 Mon Sep 17 00:00:00 2001 From: matthewmcgarvey Date: Tue, 13 Jul 2021 22:14:47 -0400 Subject: [PATCH 2/2] Add compilation errors for SaveOperation.destroy method calls --- src/avram/delete_operation.cr | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/avram/delete_operation.cr b/src/avram/delete_operation.cr index a096e1032..6855e36dd 100644 --- a/src/avram/delete_operation.cr +++ b/src/avram/delete_operation.cr @@ -34,6 +34,22 @@ abstract class Avram::DeleteOperation(T) T.name.underscore end + def self.destroy(*args, **named_args) + {% raise "#{@type}.destroy has been renamed to #{@type}.delete" %} + end + + def self.destroy(*args, **named_args, &block) + {% raise "#{@type}.destroy has been renamed to #{@type}.delete" %} + end + + def self.destroy!(*args, **named_args) + {% raise "#{@type}.destroy! has been renamed to #{@type}.delete!" %} + end + + def self.destroy!(*args, **named_args, &block) + {% raise "#{@type}.destroy! has been renamed to #{@type}.delete!" %} + end + def delete : Bool before_delete