lib/active_manageable/methods/destroy.rb in active_manageable-0.1.2 vs lib/active_manageable/methods/destroy.rb in active_manageable-0.2.0

- old
+ new

@@ -3,21 +3,36 @@ module Destroy extend ActiveSupport::Concern included do include ActiveManageable::Methods::Auxiliary::Includes + end - def destroy(id:, options: {}) - initialize_state(options: options) + def destroy(id:, options: {}) + initialize_state(options: options) - @target = model_class - includes(@options[:includes]) + @target = action_scope + includes(@options[:includes]) - @target = @target.find(id) - authorize(record: @target) + @target = find_object_for_destroy(id: id) + authorize(record: @target) - @target.destroy + model_class.transaction do + yield if block_given? + destroy_object + rescue ActiveRecord::RecordNotDestroyed + false end + end + + private + + def find_object_for_destroy(id:) + @target.find(id) + end + + def destroy_object + @target.destroy! end end end end