Class: Alchemy::Tidy

Inherits:
Object
  • Object
show all
Extended by:
Shell
Defined in:
lib/alchemy/tasks/tidy.rb

Constant Summary

Constants included from Shell

Shell::COLORS

Class Method Summary collapse

Methods included from Shell

add_todo, desc, display_todos, log, silence!, silenced?, todo, todos, verbose!

Class Method Details

.remove_duplicate_legacy_urlsObject



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/alchemy/tasks/tidy.rb', line 64

def remove_duplicate_legacy_urls
  puts "\n## Removing duplicate legacy URLs"
  sql = <<~SQL
    DELETE FROM alchemy_legacy_page_urls A USING alchemy_legacy_page_urls B
    WHERE A.page_id = B.page_id
      AND A.urlname = B.urlname
      AND A.id < B.id
  SQL
  count = ActiveRecord::Base.connection.exec_delete(sql)
  log "Deleted #{count} duplicate legacy URLs"
end

.remove_orphaned_elementsObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/alchemy/tasks/tidy.rb', line 29

def remove_orphaned_elements
  puts "\n## Removing orphaned elements"
  elements = Alchemy::Element.unscoped.not_nested
  if elements.any?
    orphaned_elements = elements.select do |element|
      element.page.nil? && element.page_id.present?
    end
    if orphaned_elements.any?
      log "Found #{orphaned_elements.size} orphaned elements"
      destroy_orphaned_records(orphaned_elements, "element")
    else
      log "No orphaned elements found", :skip
    end
  else
    log "No elements found", :skip
  end
end

.remove_trashed_elementsObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/alchemy/tasks/tidy.rb', line 47

def remove_trashed_elements
  puts "\n## Removing trashed elements"
  elements = Alchemy::Element.unscoped.where(position: nil)
  if elements.any?
    log "Destroying #{elements.size} trashed elements"
    nested_elements, parent_elements = elements.partition(&:parent_element_id)
    (nested_elements + parent_elements).each do |element|
      element.destroy
      print "."
    end
    puts "\n"
    log "Done", :message
  else
    log "No trashed elements found", :skip
  end
end

.update_element_positionsObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/alchemy/tasks/tidy.rb', line 10

def update_element_positions
  Alchemy::Page.all.each do |page|
    if page.elements.any?
      puts "\n## Updating element positions of page `#{page.name}`"
    end
    page.elements.group_by(&:parent_element_id).each do |_, elements|
      elements.each_with_index do |element, idx|
        position = idx + 1
        if element.position != position
          log "Updating position for element ##{element.id} to #{position}"
          element.update_column(:position, position)
        else
          log "Position for element ##{element.id} is already correct (#{position})", :skip
        end
      end
    end
  end
end