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

Fix helpful errors for after_* with block #282

Merged
merged 3 commits into from
Feb 9, 2020
Merged
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
18 changes: 11 additions & 7 deletions src/avram/callbacks.cr
Original file line number Diff line number Diff line change
Expand Up @@ -106,31 +106,35 @@ module Avram::Callbacks
end
end

{% for callbacks_without_block in [:after_save, :after_commit] %}
{% for callback_without_block in [:after_save, :after_commit] %}
# :nodoc:
macro {{ callbacks_without_block.id }}
macro {{ callback_without_block.id }}
\{% raise <<-ERROR
'#{callbacks_without_block.id}' does not accept a block. Instead give it a method name to run.
'{{callback_without_block.id}}' does not accept a block. Instead give it a method name to run.

Example:

#{callbacks_without_block.id} run_something
{{callback_without_block.id}} run_something

def run_something(newly_saved_record)
# Do something
end
ERROR
%}
# Will never be called but must be there so that the macro accepts a block
# Will never be called but must be there so that the macro accepts a block:
\{{ yield }}
end
{% end %}

{% for removed_callback in [:create, :update] %}
# :nodoc:
macro after_{{ removed_callback.id }}(method_name)
\{% raise "'after_#{removed_callback}' has been removed" %}
\{% raise "'after_{{removed_callback.id}}' has been removed" %}
end

# :nodoc:
macro before_{{ removed_callback.id }}(method_name)
\{% raise "'before_#{removed_callback.id}' has been removed" %}
\{% raise "'before_{{removed_callback.id}}' has been removed" %}
end
{% end %}

Expand Down