Skip to content

Commit

Permalink
Remove cell related tasks from tidy:up task
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdeyen committed Nov 12, 2018
1 parent eeca67f commit de3b05f
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 63 deletions.
43 changes: 3 additions & 40 deletions lib/alchemy/tasks/tidy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,12 @@ class Tidy
extend Shell

class << self
def create_missing_cells(page_layouts, cells)
page_layouts.each do |layout|
next if layout['cells'].blank?
cells_for_layout = cells.select { |cell| layout['cells'].include? cell['name'] }
Alchemy::Page.where(page_layout: layout['name']).each do |page|
cells_for_layout.each do |cell_for_layout|
cell = Alchemy::Cell.find_or_initialize_by(name: cell_for_layout['name'], page_id: page.id)
if cell.new_record?
log "Creating cell #{cell.name} for page #{page.name}"
else
log "Cell #{cell.name} for page #{page.name} already present", :skip
end
end
end
end
end

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(&:cell_id).each do |_cell_id, elements|
page.elements.group_by(&:parent_element_id).each do |_, elements|
elements.each_with_index do |element, idx|
position = idx + 1
if element.position != position
Expand All @@ -46,8 +29,7 @@ def update_content_positions
if element.contents.any?
puts "\n## Updating content positions of element `#{element.name}`"
end
element.contents.group_by(&:essence_type).each do |essence_type, contents|
puts "-> Contents of type `#{essence_type}`"
element.contents.group_by(&:element_id).each do |_, contents|
contents.each_with_index do |content, idx|
position = idx + 1
if content.position != position
Expand All @@ -61,31 +43,12 @@ def update_content_positions
end
end

def remove_orphaned_cells
puts "\n## Removing orphaned cells"
cells = Alchemy::Cell.unscoped.all
if cells.any?
orphaned_cells = cells.select do |cell|
cell.page.nil? && cell.page_id.present?
end
if orphaned_cells.any?
log "Found #{orphaned_cells.size} orphaned cells"
destroy_orphaned_records(orphaned_cells, 'cell')
else
log "No orphaned cells found", :skip
end
else
log "No cells found", :skip
end
end

def remove_orphaned_elements
puts "\n## Removing orphaned elements"
elements = Alchemy::Element.unscoped.all
if elements.any?
orphaned_elements = elements.select do |element|
element.page.nil? && element.page_id.present? ||
element.cell.nil? && element.cell_id.present?
element.page.nil? && element.page_id.present?
end
if orphaned_elements.any?
log "Found #{orphaned_elements.size} orphaned elements"
Expand Down
24 changes: 1 addition & 23 deletions lib/tasks/alchemy/tidy.rake
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,11 @@ namespace :alchemy do
namespace :tidy do
desc "Tidy up Alchemy database."
task :up do
Rake::Task['alchemy:tidy:cells'].invoke
Rake::Task['alchemy:tidy:element_positions'].invoke
Rake::Task['alchemy:tidy:content_positions'].invoke
Rake::Task['alchemy:tidy:remove_orphaned_records'].invoke
end

desc "Creates missing cells for pages."
task cells: :environment do
if !File.exist? Rails.root.join('config/alchemy/cells.yml')
puts "No page cell definitions found."
else
cells = Alchemy::Cell.definitions
page_layouts = Alchemy::PageLayout.all
if cells && page_layouts
Alchemy::Tidy.create_missing_cells(page_layouts, cells)
else
puts "No page layouts or cell definitions found."
end
end
end

desc "Fixes element positions."
task element_positions: [:environment] do
Alchemy::Tidy.update_element_positions
Expand All @@ -35,18 +19,12 @@ namespace :alchemy do
Alchemy::Tidy.update_content_positions
end

desc "Remove orphaned records (cells, elements, contents)."
desc "Remove orphaned records (elements & contents)."
task remove_orphaned_records: [:environment] do
Rake::Task['alchemy:tidy:remove_orphaned_cells'].invoke
Rake::Task['alchemy:tidy:remove_orphaned_elements'].invoke
Rake::Task['alchemy:tidy:remove_orphaned_contents'].invoke
end

desc "Remove orphaned cells."
task remove_orphaned_cells: [:environment] do
Alchemy::Tidy.remove_orphaned_cells
end

desc "Remove orphaned elements."
task remove_orphaned_elements: [:environment] do
Alchemy::Tidy.remove_orphaned_elements
Expand Down

0 comments on commit de3b05f

Please sign in to comment.