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

Rename DeleteOperation#destroy to #delete #707

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion spec/operations/delete_operation_callbacks_spec.cr
Expand Up @@ -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"
Expand Down
22 changes: 11 additions & 11 deletions spec/operations/delete_operation_spec.cr
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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

Expand All @@ -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")
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions spec/operations/operation_needs_spec.cr
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
16 changes: 16 additions & 0 deletions src/avram/delete_operation.cr
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/avram/errors.cr
Expand Up @@ -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))

Expand Down
36 changes: 11 additions & 25 deletions src/avram/needy_initializer_and_delete_methods.cr
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Comment on lines -95 to -107
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should leave this in, but name it destroy with a message like "destroy has been renamed to delete". Maybe with the type name or something so it's easy to find which ones you have to replace?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added it in, unfortunately I had to move the compilation error because @type was not the inherited type and it was a mess of macro code in the backtrace.

This is something that I hope can be removed in the future so I don't think it's a big deal.


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
Expand All @@ -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 %}
Expand Down Expand Up @@ -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)
Expand Down