Class: Alchemy::Tidy
- Inherits:
-
Object
show all
- Extended by:
- Shell
- Defined in:
- lib/alchemy/tasks/tidy.rb
Class Method Summary
collapse
Methods included from Shell
add_todo, desc, display_todos, log, silence!, silenced?, todo, todos, verbose!
Class Method Details
.remove_orphaned_contents ⇒ Object
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
# File 'lib/alchemy/tasks/tidy.rb', line 64
def remove_orphaned_contents
puts "\n## Removing orphaned contents"
contents = Alchemy::Content.unscoped.all
if contents.any?
orphaned_contents = contents.select do |content|
content.essence.nil? && content.essence_id.present? ||
content.element.nil? && content.element_id.present?
end
if orphaned_contents.any?
log "Found #{orphaned_contents.size} orphaned contents"
destroy_orphaned_records(orphaned_contents, 'content')
else
log "No orphaned contents found", :skip
end
else
log "No contents found", :skip
end
end
|
.remove_orphaned_elements ⇒ Object
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
# File 'lib/alchemy/tasks/tidy.rb', line 46
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
|
.update_content_positions ⇒ Object
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
# File 'lib/alchemy/tasks/tidy.rb', line 27
def update_content_positions
Alchemy::Element.all.each do |element|
if element.contents.any?
puts "\n## Updating content positions of element `#{element.name}`"
end
element.contents.group_by(&:element_id).each do |_, contents|
contents.each_with_index do |content, idx|
position = idx + 1
if content.position != position
log "Updating position for content ##{content.id} to #{position}"
content.update_column(:position, position)
else
log "Position for content ##{content.id} is already correct (#{position})", :skip
end
end
end
end
end
|
.update_element_positions ⇒ Object
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
# File 'lib/alchemy/tasks/tidy.rb', line 8
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
|