Class: Alchemy::DeleteElements

Inherits:
Object
  • Object
show all
Defined in:
app/services/alchemy/delete_elements.rb

Defined Under Namespace

Classes: WouldLeaveOrphansError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(elements) ⇒ DeleteElements

Returns a new instance of DeleteElements.


8
9
10
# File 'app/services/alchemy/delete_elements.rb', line 8

def initialize(elements)
  @elements = elements
end

Instance Attribute Details

#elementsObject (readonly)

Returns the value of attribute elements.


6
7
8
# File 'app/services/alchemy/delete_elements.rb', line 6

def elements
  @elements
end

Instance Method Details

#callObject


12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/services/alchemy/delete_elements.rb', line 12

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
  delete_elements
end