Sha256: 5a17efe7f5af204068deb49d22a59fde85c2e6a3884f533eb2fd497afdc373ee
Contents?: true
Size: 1.12 KB
Versions: 17
Compression:
Stored size: 1.12 KB
Contents
# frozen_string_literal: true module Alchemy class DeleteElements class WouldLeaveOrphansError < StandardError; end attr_reader :elements def initialize(elements) @elements = elements end def call if orphanable_records.present? raise WouldLeaveOrphansError end contents = Alchemy::Content.where(element_id: elements.map(&:id)) contents.group_by(&:essence_type) .transform_values! { |value| value.map(&:essence_id) } .each do |class_name, ids| class_name.constantize.where(id: ids).delete_all end contents.delete_all Gutentag::Tagging.where(taggable: elements).delete_all delete_elements end private def orphanable_records Alchemy::Element.where(parent_element_id: [elements]).where.not(id: elements) end def delete_elements case elements when ActiveRecord::Associations::CollectionProxy elements.delete_all(:delete_all) when ActiveRecord::Relation elements.delete_all else Alchemy::Element.where(id: elements).delete_all end end end end
Version data entries
17 entries across 17 versions & 1 rubygems