|
| 1 | +require "./after_commit_callback" |
| 2 | + |
| 3 | +module Avram::DeleteCallbacks |
| 4 | + include Avram::AfterCommitCallback |
| 5 | + |
| 6 | + # Same as `before_save`, but with a different name |
| 7 | + macro before_delete(method_name, if _if = nil, unless _unless = nil) |
| 8 | + {% unless _if.is_a?(SymbolLiteral) || _if.is_a?(NilLiteral) %} |
| 9 | + conditional_error_for_inline_callbacks(:before_delete, {{ method_name }}, :if) |
| 10 | + {% end %} |
| 11 | + {% unless _unless.is_a?(SymbolLiteral) || _unless.is_a?(NilLiteral) %} |
| 12 | + conditional_error_for_inline_callbacks(:before_delete, {{ method_name }}, :unless) |
| 13 | + {% end %} |
| 14 | + before_delete(if: {{ _if }}, unless: {{ _unless }}) do |
| 15 | + {{ method_name.id }} |
| 16 | + end |
| 17 | + end |
| 18 | + |
| 19 | + macro before_delete(if _if = nil, unless _unless = nil) |
| 20 | + {% if _if != nil && _unless != nil %} |
| 21 | + {% raise "Your before_delete callbacks should only specify `if` or `unless`, but not both." %} |
| 22 | + {% end %} |
| 23 | + {% unless _if.is_a?(SymbolLiteral) || _if.is_a?(NilLiteral) %} |
| 24 | + conditional_error_for_block_callbacks(:before_delete, :if) |
| 25 | + {% end %} |
| 26 | + {% unless _unless.is_a?(SymbolLiteral) || _unless.is_a?(NilLiteral) %} |
| 27 | + conditional_error_for_block_callbacks(:before_delete, :unless) |
| 28 | + {% end %} |
| 29 | + |
| 30 | + def before_delete |
| 31 | + {% if @type.methods.map(&.name).includes?(:before_delete.id) %} |
| 32 | + previous_def |
| 33 | + {% else %} |
| 34 | + super |
| 35 | + {% end %} |
| 36 | + |
| 37 | + {% if _if %} |
| 38 | + if {{ _if.id }} |
| 39 | + {{ yield }} |
| 40 | + end |
| 41 | + {% elsif _unless %} |
| 42 | + unless {{ _unless.id }} |
| 43 | + {{ yield }} |
| 44 | + end |
| 45 | + {% else %} |
| 46 | + {{ yield }} |
| 47 | + {% end %} |
| 48 | + end |
| 49 | + end |
| 50 | + |
| 51 | + # Same as `after_save` but with a different name |
| 52 | + macro after_delete(method_name, if _if = nil, unless _unless = nil) |
| 53 | + {% unless _if.is_a?(SymbolLiteral) || _if.is_a?(NilLiteral) %} |
| 54 | + conditional_error_for_inline_callbacks(:after_delete, {{ method_name }}, :if) |
| 55 | + {% end %} |
| 56 | + {% unless _unless.is_a?(SymbolLiteral) || _unless.is_a?(NilLiteral) %} |
| 57 | + conditional_error_for_inline_callbacks(:after_delete, {{ method_name }}, :unless) |
| 58 | + {% end %} |
| 59 | + after_delete(if: {{ _if }}, unless: {{ _unless }}) do |object| |
| 60 | + {{ method_name.id }}(object) |
| 61 | + end |
| 62 | + end |
| 63 | + |
| 64 | + macro after_delete(if _if = nil, unless _unless = nil, &block) |
| 65 | + {% if _if != nil && _unless != nil %} |
| 66 | + {% raise "Your after_delete callbacks should only specify `if` or `unless`, but not both." %} |
| 67 | + {% end %} |
| 68 | + {% unless _if.is_a?(SymbolLiteral) || _if.is_a?(NilLiteral) %} |
| 69 | + conditional_error_for_block_callbacks(:after_delete, :if) |
| 70 | + {% end %} |
| 71 | + {% unless _unless.is_a?(SymbolLiteral) || _unless.is_a?(NilLiteral) %} |
| 72 | + conditional_error_for_block_callbacks(:after_delete, :unless) |
| 73 | + {% end %} |
| 74 | + {% |
| 75 | + if block.args.size != 1 |
| 76 | + raise <<-ERR |
| 77 | + The 'after_delete' callback requires exactly 1 block arg to be passed. |
| 78 | + Example: |
| 79 | + after_delete do |deleted_user| |
| 80 | + some_method(deleted_user) |
| 81 | + end |
| 82 | + ERR |
| 83 | + end |
| 84 | + %} |
| 85 | + def after_delete(%object : T) |
| 86 | + {% if @type.methods.map(&.name).includes?(:after_delete.id) %} |
| 87 | + previous_def |
| 88 | + {% else %} |
| 89 | + super |
| 90 | + {% end %} |
| 91 | + |
| 92 | + {% if _if %} |
| 93 | + if {{ _if.id }} |
| 94 | + {{ block.args.first }} = %object |
| 95 | + {{ block.body }} |
| 96 | + end |
| 97 | + {% elsif _unless %} |
| 98 | + unless {{ _unless.id }} |
| 99 | + {{ block.args.first }} = %object |
| 100 | + {{ block.body }} |
| 101 | + end |
| 102 | + {% else %} |
| 103 | + {{ block.args.first }} = %object |
| 104 | + {{ block.body }} |
| 105 | + {% end %} |
| 106 | + end |
| 107 | + end |
| 108 | +end |
0 commit comments