Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rake task to remove trashed elements #2025

Merged
merged 1 commit into from
Feb 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions lib/alchemy/tasks/tidy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions lib/tasks/alchemy/tidy.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down Expand Up @@ -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"
Expand Down