diff --git a/lib/alchemy/tasks/tidy.rb b/lib/alchemy/tasks/tidy.rb index e60c37547f..93c308cd2f 100644 --- a/lib/alchemy/tasks/tidy.rb +++ b/lib/alchemy/tasks/tidy.rb @@ -62,6 +62,23 @@ def remove_orphaned_elements end end + 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 + def remove_orphaned_contents puts "\n## Removing orphaned contents" contents = Alchemy::Content.unscoped.all diff --git a/lib/tasks/alchemy/tidy.rake b/lib/tasks/alchemy/tidy.rake index 29b390d026..097a6828e0 100644 --- a/lib/tasks/alchemy/tidy.rake +++ b/lib/tasks/alchemy/tidy.rake @@ -8,6 +8,7 @@ namespace :alchemy do Rake::Task["alchemy:tidy:element_positions"].invoke Rake::Task["alchemy:tidy:content_positions"].invoke Rake::Task["alchemy:tidy:remove_orphaned_records"].invoke + Rake::Task["alchemy:tidy:remove_trashed_elements"].invoke end desc "Fixes element positions." @@ -36,6 +37,11 @@ namespace :alchemy do Alchemy::Tidy.remove_orphaned_contents end + desc "Remove trashed elements." + task remove_trashed_elements: [:environment] do + Alchemy::Tidy.remove_trashed_elements + end + desc "List Alchemy elements usage" task elements_usage: :environment do puts "\n"