module SnFoil::Contexts::DestroyContext

Public Instance Methods

after_destroy(method = nil, **options, &block) click to toggle source
# File lib/sn_foil/contexts/destroy_context.rb, line 37
def after_destroy(method = nil, **options, &block)
  raise ArgumentError, '#after_destroy requires either a method name or a block' if method.nil? && block.nil?

  (@i_after_destroy_hooks ||= []) << { method: method, block: block, if: options[:if], unless: options[:unless] }
end
after_destroy_failure(method = nil, **options, &block) click to toggle source
# File lib/sn_foil/contexts/destroy_context.rb, line 49
def after_destroy_failure(method = nil, **options, &block)
  raise ArgumentError, '#after_destroy_failure requires either a method name or a block' if method.nil? && block.nil?

  (@i_after_destroy_failure_hooks ||= []) << { method: method, block: block, if: options[:if], unless: options[:unless] }
end
after_destroy_failure_hooks() click to toggle source
# File lib/sn_foil/contexts/destroy_context.rb, line 107
def after_destroy_failure_hooks
  self.class.i_after_destroy_failure_hooks || []
end
after_destroy_hooks() click to toggle source
# File lib/sn_foil/contexts/destroy_context.rb, line 99
def after_destroy_hooks
  self.class.i_after_destroy_hooks || []
end
after_destroy_success(method = nil, **options, &block) click to toggle source
# File lib/sn_foil/contexts/destroy_context.rb, line 43
def after_destroy_success(method = nil, **options, &block)
  raise ArgumentError, '#after_destroy_success requires either a method name or a block' if method.nil? && block.nil?

  (@i_after_destroy_success_hooks ||= []) << { method: method, block: block, if: options[:if], unless: options[:unless] }
end
after_destroy_success_hooks() click to toggle source
# File lib/sn_foil/contexts/destroy_context.rb, line 103
def after_destroy_success_hooks
  self.class.i_after_destroy_success_hooks || []
end
before_destroy(method = nil, **options, &block) click to toggle source
# File lib/sn_foil/contexts/destroy_context.rb, line 31
def before_destroy(method = nil, **options, &block)
  raise ArgumentError, '#before_destroy requires either a method name or a block' if method.nil? && block.nil?

  (@i_before_destroy_hooks ||= []) << { method: method, block: block, if: options[:if], unless: options[:unless] }
end
before_destroy_hooks() click to toggle source
# File lib/sn_foil/contexts/destroy_context.rb, line 95
def before_destroy_hooks
  self.class.i_before_destroy_hooks || []
end
destroy(id:, entity: nil, **options) click to toggle source
# File lib/sn_foil/contexts/destroy_context.rb, line 21
def destroy(id:, entity: nil, **options)
  new(entity).destroy(**options, id: id)
end
setup_destroy(method = nil, **options, &block) click to toggle source
# File lib/sn_foil/contexts/destroy_context.rb, line 25
def setup_destroy(method = nil, **options, &block)
  raise ArgumentError, '#setup_destroy requires either a method name or a block' if method.nil? && block.nil?

  (@i_setup_destroy_hooks ||= []) << { method: method, block: block, if: options[:if], unless: options[:unless] }
end
setup_destroy_hooks() click to toggle source
# File lib/sn_foil/contexts/destroy_context.rb, line 91
def setup_destroy_hooks
  self.class.i_setup_destroy_hooks || []
end
setup_destroy_object(id: nil, object: nil, **options) click to toggle source
# File lib/sn_foil/contexts/destroy_context.rb, line 56
def setup_destroy_object(id: nil, object: nil, **options)
  raise ArgumentError, 'one of the following keywords is required: id, object' unless id || object

  options.merge! object: wrap_object(object || scope.resolve.find(id))
end

Private Instance Methods

after_destroy_save(options) click to toggle source
# File lib/sn_foil/contexts/destroy_context.rb, line 142
def after_destroy_save(options)
  options = after_change(**options)
  options = after_change_hooks.reduce(options) { |opts, hook| run_hook(hook, **opts) }
  options = after_destroy(**options)
  after_destroy_hooks.reduce(options) { |opts, hook| run_hook(hook, **opts) }
end
after_destroy_save_failure(options) click to toggle source
# File lib/sn_foil/contexts/destroy_context.rb, line 156
def after_destroy_save_failure(options)
  options = after_change_failure(**options)
  options = after_change_failure_hooks.reduce(options) { |opts, hook| run_hook(hook, **opts) }
  options = after_destroy_failure(**options)
  after_destroy_failure_hooks.reduce(options) { |opts, hook| run_hook(hook, **opts) }
end
after_destroy_save_success(options) click to toggle source
# File lib/sn_foil/contexts/destroy_context.rb, line 149
def after_destroy_save_success(options)
  options = after_change_success(**options)
  options = after_change_success_hooks.reduce(options) { |opts, hook| run_hook(hook, **opts) }
  options = after_destroy_success(**options)
  after_destroy_success_hooks.reduce(options) { |opts, hook| run_hook(hook, **opts) }
end
before_destroy_save(options) click to toggle source
# File lib/sn_foil/contexts/destroy_context.rb, line 135
def before_destroy_save(options)
  options = before_change(**options)
  options = before_change_hooks.reduce(options) { |opts, hook| run_hook(hook, **opts) }
  options = before_destroy(**options)
  before_destroy_hooks.reduce(options) { |opts, hook| run_hook(hook, **opts) }
end
before_setup_destroy_object(**options) click to toggle source
# File lib/sn_foil/contexts/destroy_context.rb, line 113
def before_setup_destroy_object(**options)
  options = setup(**options)
  options = setup_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
  options = setup_change(**options)
  options = setup_change_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
  options = setup_destroy(**options)
  setup_destroy_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
end
destroy_hooks(options) click to toggle source

This method is private to help protect the order of execution of hooks

# File lib/sn_foil/contexts/destroy_context.rb, line 123
def destroy_hooks(options)
  options = before_destroy_save(options)
  destroy_successful = options[:object].destroy
  options[:object] = unwrap_object(options[:object])
  options = if destroy_successful
              after_destroy_save_success(options)
            else
              after_destroy_save_failure(options)
            end
  after_destroy_save(options)
end