From 0294a75d879210054974acc07ebfa1441a977691 Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Sun, 11 Nov 2018 23:33:43 +0100 Subject: [PATCH] Remove cell related tasks from tidy:up task --- lib/alchemy/tasks/tidy.rb | 43 +++---------------------------------- lib/tasks/alchemy/tidy.rake | 24 +-------------------- 2 files changed, 4 insertions(+), 63 deletions(-) diff --git a/lib/alchemy/tasks/tidy.rb b/lib/alchemy/tasks/tidy.rb index 61051c45a1..523e30182b 100644 --- a/lib/alchemy/tasks/tidy.rb +++ b/lib/alchemy/tasks/tidy.rb @@ -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 @@ -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 @@ -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" diff --git a/lib/tasks/alchemy/tidy.rake b/lib/tasks/alchemy/tidy.rake index e052b7993a..bdad39b7be 100644 --- a/lib/tasks/alchemy/tidy.rake +++ b/lib/tasks/alchemy/tidy.rake @@ -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 @@ -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