Sha256: aa4a8e910052e5416ed7c2d903bef318b6e4e4ffe3a2fac64d84abd8cbf63522

Contents?: true

Size: 836 Bytes

Versions: 48

Compression:

Stored size: 836 Bytes

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

      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

48 entries across 48 versions & 1 rubygems

Version Path
alchemy_cms-7.3.4 app/services/alchemy/delete_elements.rb
alchemy_cms-7.3.3 app/services/alchemy/delete_elements.rb
alchemy_cms-7.3.2 app/services/alchemy/delete_elements.rb
alchemy_cms-7.2.7 app/services/alchemy/delete_elements.rb
alchemy_cms-7.3.1 app/services/alchemy/delete_elements.rb
alchemy_cms-7.3.0 app/services/alchemy/delete_elements.rb
alchemy_cms-7.2.6 app/services/alchemy/delete_elements.rb
alchemy_cms-7.2.5 app/services/alchemy/delete_elements.rb
alchemy_cms-7.1.12 app/services/alchemy/delete_elements.rb
alchemy_cms-7.0.15 app/services/alchemy/delete_elements.rb
alchemy_cms-7.2.4 app/services/alchemy/delete_elements.rb
alchemy_cms-7.1.11 app/services/alchemy/delete_elements.rb
alchemy_cms-7.2.3 app/services/alchemy/delete_elements.rb
alchemy_cms-7.1.10 app/services/alchemy/delete_elements.rb
alchemy_cms-7.2.2 app/services/alchemy/delete_elements.rb
alchemy_cms-7.1.9 app/services/alchemy/delete_elements.rb
alchemy_cms-7.0.14 app/services/alchemy/delete_elements.rb
alchemy_cms-7.2.1 app/services/alchemy/delete_elements.rb
alchemy_cms-7.0.13 app/services/alchemy/delete_elements.rb
alchemy_cms-7.1.8 app/services/alchemy/delete_elements.rb